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


    转载请注明出处: 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 Round #535 (Div. 3)
    2019 CCPC-Wannafly Winter Camp Day4(Div2, onsite)
    Codeforces Round #534 (Div. 2)
    2019 CCPC-Wannafly Winter Camp Day3(Div2, onsite)
    2019 CCPC-Wannafly Winter Camp Day2(Div2, onsite)
    2019 CCPC-Wannafly Winter Camp Day1 (Div2, onsite)
    codeforces1097D Makoto and a Blackboard 数学+期望dp
    【NOIP2016】换教室
    ICPC2019徐州站游记
    【Codeforces】Orz Panda Cup
  • 原文地址:https://www.cnblogs.com/fraud/p/4338504.html
Copyright © 2020-2023  润新知