Codeforces 505B Mr Kitayuta ' s colorful Graph

Source: Internet
Author: User

Mr Kitayuta ' s colorful GraphTime limit:1000MS Memory Limit:262144KB 64bit IO Format:%i64d &%i6 4u SubmitStatusPracticecodeforces 505B

Description

Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The vertices of the graph is numbered from 1 to n. Each edge, namely Edge I, have a color ci, connecting vertex a i and bi.

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

in the I-th query, he gives you and integers- ui and vI.

Find the number of the colors that satisfy the following condition:the edges of this color connect vertex ui 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,b i   (1≤ a I < b i n ) and  c  Sub class= "Lower-index" > i   (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 II vertices, that's is, If  I J , ( a i , b i , c i ) ≠ ( a J , b J , C J ).

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

Then follows Q lines, containing space-separated II integers- ui and V i (1≤ ui, vin). It's guaranteed that uivi.

Output

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

Sample Input

Input
4 5
1 2 1
1 2 2
2 3 1
2 3 3
2 4 3
3
1 2
3 4
1 4
Output
2
1
0
Input
5 7
1 5 1
2 5 1
3 5 1
4 5 1
1 2 2
2 3 2
3 4 2
5
1 5
5 1
2 5
1 5
1 4
Output
1
1
1
1
2

Hint

Let ' s consider the first sample.

The figure above shows the first sample.
    • Vertex 1 and Vertex 2 is connected by color 1 and 2.
    • Vertex 3 and Vertex 4 is connected by Color 3.
    • Vertex 1 and Vertex 4 is not connected by any single color.
#include <bits/stdc++.h>#defineMaxx 105using namespaceStd;vector<int>Edg[maxx][maxx];BOOLVis[maxx];intans;intx, y;BOOLBFsintc) {Queue<int>Q;    Q.push (x); intNow ; memset (Vis,0,sizeof(VIS));  while(!Q.empty ()) { Now=Q.front (); if(now==y)return true;        Q.pop ();  for(intI=0; I<edg[c][now].size (); i++)        {            intthen=Edg[c][now][i]; if(Vis[then])Continue; Vis[then]=1;        Q.push (then); }    }    return false;}intMain () {intn,m; scanf ("%d%d",&n,&m); intA,b,c;  for(intI=0; i<m;i++) {scanf ("%d%d%d",&a,&b,&c);        Edg[c][b].push_back (a);    Edg[c][a].push_back (b); }    intQ; scanf ("%d",&q);  for(intI=0; i<q;i++) {ans=0; scanf ("%d%d",&x,&y);  for(intI=0; i<m;i++)            if(BFS (i+1)) ans++; printf ("%d\n", ans); }}
View Code

This problem is not difficult to understand, with BFS, save each color edge of the associated edge, and then for each color of the edge of BFS, if you can find the end of the edge to match the beginning, the result is added one, know to find out all the color of the side.

It is said that the problem can also be used and check the collection. Have a chance to take a look. The dregs are now at the learning stage.

Codeforces 505B Mr Kitayuta ' s colorful Graph

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.