首页 > 其他 > 详细

codeforces 505B Mr. Kitayuta's Colorful Graph(水题)

时间:2015-03-15 00:25:41      阅读:349      评论:0      收藏:0      [点我收藏+]

转载请注明出处: http://www.cnblogs.com/fraud/           ——by fraud

 

Mr. Kitayuta‘s Colorful Graph

Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The vertices of the graph are numbered from 1 to n. Each edge, namely edge i, 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 two integers — ui and vi.

Find the number of the colors that satisfy the following condition: the edges of that color connect vertex ui and vertex vi directly or indirectly.

Input

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

The next m lines contain space-separated three integers — ai, bi (1 ≤ ai < bi ≤ n) and ci (1 ≤ ci ≤ m). Note that there can be multiple edges between two vertices. However, there are no multiple edges of the same color between two vertices, that is, 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 follows q lines, containing space-separated two integers — ui and vi (1 ≤ ui, vi ≤ n). It is guaranteed that ui ≠ vi.

Output

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

Sample test(s)
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
Note

Let‘s consider the first sample.

技术分享 The figure above shows the first sample.
  • Vertex 1 and vertex 2 are connected by color 1 and 2.
  • Vertex 3 and vertex 4 are connected by color 3.
  • Vertex 1 and vertex 4 are not connected by any single color.

 

并查集,暴力都可以过,大水题

技术分享
 1 #include <iostream>
 2 using namespace std;
 3 #define MAXN 110
 4 int pa[110][110],ra[110][110];
 5 void init()
 6 {
 7     for(int i=0;i<MAXN;i++)
 8     {
 9         for(int j=0;j<MAXN;j++){
10             pa[i][j]=j;
11             ra[i][j]=0;
12         }
13     }
14 }
15 int find(int x,int c){
16     if(pa[c][x]!=x)pa[c][x]=find(pa[c][x],c);
17     return pa[c][x];
18 }
19 int unite(int x,int y,int c){
20     x=find(x,c);
21     y=find(y,c);
22     if(x==y)return 0;
23     if(ra[c][x]<ra[c][y])
24     {
25         pa[c][x]=y;
26     }else{
27         pa[c][y]=x;
28         if(ra[c][x]==ra[c][y])ra[c][x]++;
29     }
30     return 1;
31 }
32 bool same(int x,int y,int c){
33     return find(x,c)==find(y,c);
34 }
35 
36 int main()
37 {
38     ios::sync_with_stdio(false);
39     int n,m;
40     init();
41     cin>>n>>m;
42     int u,v,c;
43     for(int i=0;i<m;i++){
44         cin>>u>>v>>c;
45         u--,v--,c--;
46         unite(u,v,c);
47     }
48     int q;
49     cin>>q;
50     for(int i=0;i<q;i++){
51         cin>>u>>v;
52         u--;v--;
53         int ans=0;
54         for(int i=0;i<m;i++)
55         {
56             if(same(u,v,i))ans++;
57         }
58         cout<<ans<<endl;
59     }
60 
61     return 0;
62 }
代码君

 

codeforces 505B Mr. Kitayuta's Colorful Graph(水题)

原文:http://www.cnblogs.com/fraud/p/4338504.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!