B. Mr Kitayuta ' s colorful Graph (CF #286 (Div. 2) and check set)

Source: Internet
Author: User

B. Mr Kitayuta ' s colorful graphtime limit per test1 secondmemory limit per test256 megabytesinputstandard Inputoutputstan Dard output

Mr. Kitayuta have just bought an undirected graph consisting ofNVertices andmEdges. The vertices of the graph is numbered from 1 toN. Each edge, namely edgeI, has a color Ci , connecting Vertex ai and bi .

Mr. Kitayuta wants you to process the Following  q  queries.

In The  i -th Query, he gives you and Integers-  u i  and  v i .

Find the number of the colors that satisfy the following condition:the edges of this color connect vertex u C2>i and vertex vi directly or indirectly.

Input

The first line of the input contains space-separated, integers- n and m (2?≤? n? ≤?100,?1?≤? m≤?100), denoting the number of the vertices and the number of the edges, respectively.

The nextmLines contain space-separated three integers- ai , bi (1?≤? a i? < b i? ≤? N ) and Ci (1?≤? C i? ≤? m ). Note that there can is multiple edges between the vertices. However, there is no multiple edges of the same color between-vertices, that's, if i? ≠? J ,(aI,?bI,?CI)? ≠? (aJ,?bJ,?CJ).

The next line contains a integer- q (1?≤? Q≤?100), denoting the number of the queries.

Then followsQLines, containing space-separated, integers- ui and vi (1?≤? u i,? v i? ≤? N ). It's Guaranteed that ui? ≠? v I .

Output

For each query, print the answer-a separate line.

Sample Test (s) input
4 51 2 11 2 22 3 12 3 32 4 331 23 41 4
Output
210
Input
5 71 5 12 5 13 5 14 5 11 2 22 3 23 4 251 55 12 51 51 4
Output
11112
Note

Let ' s consider the first sample.

The figure above shows the first sample.
  • vertex 1  and 
  • Vertex 3 and Vertex 4 is connected by Color 3.
  • Vertex 1 and Vertex 4 is not connected by any single color.
Test instructions: The existing n-point M-edge of the graph, each edge has a color, and then there is a Q-query, x y Ask the point x to y a total of several colors of the side to connect them (different colors of the side can not be mixed together, to separate see)

Idea: two-dimensional and check set, each color maintenance of a and check set, when looking at a certain color under the two points whether there is a common father.

Code:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set > #include <queue> #pragma comment (linker, "/stack:102400000,102400000") #define MAXN 1005#define MAXN 2005# Define mod 1000000009#define INF 0x3f3f3f3f#define pi ACOs ( -1.0) #define EPS 1e-6#define Lson rt<<1,l,mid#define RSO    N rt<<1|1,mid+1,rtypedef Long Long ll;using namespace Std;int n,m;int color[110];int father[110][110];void init () { for (int i=0;i<=110;i++) for (int j=0;j<=110;j++) father[i][j]=j;}    int find_father (int c,int x)//recursive notation {if (x!=father[c][x]) Father[c][x]=find_father (c,father[c][x]); return father[c][x];} int find_father (int c,int x)//non-recursive notation//{//int r=x,i,j;//while (r!=father[c][r])//r=father[c][r];//I=x ;//while (I!=R)//{//j=father[c][i];//father[c][i]=r;//i=j;//}//return r;//}int main () {while (~scanf ("%d%d", &n,&m)) {init ();        int a,b,c;  memset (color,0,sizeof (color));            Record which colors are used for (int i=0;i<m;i++) {scanf ("%d%d%d", &a,&b,&c);            color[c]=1;//printf ("+++\n");            int Fa=find_father (C,A);            int Fb=find_father (C,B);        if (FA!=FB) FATHER[C][FA]=FB;        } int q,x,y;        scanf ("%d", &q);            while (q--) {scanf ("%d%d", &x,&y);            int ans=0; for (int i=1;i<=m;i++) {if (Color[i]) {if (Find_father (i,                x) ==find_father (i,y)) ans++;        }} printf ("%d\n", ans); }} return 0;}



B. Mr Kitayuta ' s colorful Graph (CF #286 (Div. 2) and check set)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.