• codeforces 505B——DFS——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 ton. 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 — aibi (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 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 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.
    /*
    题意:求从i到j有几条能通过的(颜色相同)的路径
    多开一维保存颜色,暴力搜索
    对节点i,j对不同颜色进行dfs
    */
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    using namespace std;
    int map[110][110];
    int vis[110];
    int vis1[110][110];
    int color[110][110][110];
    vector<int> G[110];
    int sum;
    
    bool dfs(int u,int vv,int color1)
    {
        if(!vis[u]){
            vis[u] = 1;
        if(u == vv) return true;
        for(int i = 0 ; i < G[u].size(); i++){
            int v = G[u][i];
            for(int j = 1 ; j <= vis1[u][v]; j++){
            if(color[u][v][j] == color1){
               if( dfs(v, vv, color1)) return true;
            }
            }
        }
        }
        return false ;
    }
    
    int main()
    {
    int n, m, k;
    int a, b, c;
    while(~scanf("%d %d", &n, &m)){
        memset(vis, 0, sizeof(vis1));
        for(int  i= 1; i <= m; i++){
            scanf("%d%d%d", &a, &b, &c);
            G[a].push_back(b);
            G[b].push_back(a);
            vis1[a][b]++;
            vis1[b][a]++;
            color[a][b][vis1[a][b]] = c;
            color[b][a][vis1[b][a]] = c;
        }
        scanf("%d", &k);
        int v1, v2;
        while(k--){
            sum = 0;
            scanf("%d%d", &v1, &v2);
            for(int i = 1; i <= m; i++){
                memset(vis, 0, sizeof(vis));
                if(dfs(v1, v2, i)){
                    sum++;
                }
            }
            printf("%d
    ", sum);
        }
    }
      return 0;
    }
    

      

  • 相关阅读:
    自增主键强制修改
    网页速度优化
    JS 获取字符串长度, 区别中英文
    SQL时间相关 SQL日期,时间比较
    关于document.cookie的使用
    php文件头部空白影响CSS布局 2
    FlvPlayer 播放器代码
    JAVA——继承、多态、重载和重写转
    JS星级评分,带提示(转)
    web 启动 本地应用程序 Activity
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4667486.html
Copyright © 2020-2023  润新知