题目连接:http://codeforces.com/contest/505/problem/B

B. Mr. Kitayuta's Colorful Graph
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 — 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.

Examples
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

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.
题目大意:给定一个图,可能存在重边,各边有不同权值,给定任意两点,求两点之间有多少种相同权值的边相连。解题思路:题目很直白,有多种解法,因为数据范围很小,所以可以直接对于每种颜色dfs遍历一遍,因为有多次查询,所以综合考虑为节省时间直接全部dfs一遍,再将结果储存在一个二维数组中,每次查询输出数组中的结果即可。因为可能存在重边,所以使用vector存边。dfs很简单,每次寻找一种颜色即可。代码如下:
#include<bits/stdc++.h>

using namespace std;

vector <][];
int n;
]= {};

bool dfs(int now,int color,int e)
{
    if(now==e)
        return true;
    ; i<=n; i++)
    {
        )
            continue;
        ; j<g[now][i].size(); j++)
        {
            if(g[now][i][j]!=color)
                continue;
            else
            {
                vis[i]=;
                if(dfs(i,color,e))
                    return true;
            }
        }
    }
    return false;
}

int main()
{
    int m,u,v,c,q,ans;
    ][];
    scanf("%d%d",&n,&m);
    ; i<m; i++)
    {
        scanf("%d%d%d",&u,&v,&c);
        g[u][v].push_back(c);
        g[v][u].push_back(c);
    }
    ; i <= n; i++)
    {
        ; j <= n; j++)
        {
            ans = ;
            ; k <= m; k++)
            {
                memset(vis,,sizeof(vis));
                if (dfs(i, k, j))
                    ans++;
                mp[i][j] = mp[j][i] = ans;
            }
        }
    }
    scanf("%d",&q);
    ; i<q; i++)
    {
        scanf("%d%d",&u,&v);
        cout<<mp[u][v]<<endl;
    }
}

此题还可以用并查集来解,构造一个二维并查集,每个颜色分别记录,更简单而且更快,但是第一时间想到的就是dfs,以后做题思维应当更灵活,不能定式思维,要熟悉各个算法可以实现的各种功能,再多加思考选用最优解。

codeforces-505B的更多相关文章

  1. CodeForces 505B Mr. Kitayuta's Colorful Graph

    Mr. Kitayuta's Colorful Graph Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d ...

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

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Mr. Kitayuta's Colorful Graph Mr. Kitayut ...

  3. codeforces#505--B Weakened Common Divisor

    B. Weakened Common Divisor time limit per test 1.5 seconds memory limit per test 256 megabytes input ...

  4. CodeForces - 505B Mr. Kitayuta's Colorful Graph 二维并查集

    Mr. Kitayuta's Colorful Graph Mr. Kitayuta has just bought an undirected graph consisting of n verti ...

  5. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  6. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  7. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  8. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  9. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  10. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

随机推荐

  1. 《Cracking the Coding Interview》——第18章:难题——题目11

    2014-04-29 04:30 题目:给定一个由‘0’或者‘1’构成的二维数组,找出一个四条边全部由‘1’构成的正方形(矩形中间可以有‘0’),使得矩形面积最大. 解法:用动态规划思想,记录二维数组 ...

  2. 《Cracking the Coding Interview》——第9章:递归和动态规划——题目6

    2014-03-20 03:27 题目:输出所有由N对括号组成的合法的括号序列.比如n=2,“()()”.“(())”等等. 解法:动态规划配合DFS,应该也叫记忆化搜索吧.一个整数N总可以拆成若干个 ...

  3. 去除文件夹中的.svn

    一.在Dos窗口中运行如下命令 for/r <你项目的路径> %i in (.svn) do rd /s /q %i 二.将“Delete SVN Folders”操作添加到右击菜单中 建 ...

  4. 用Jenkins自动化搭建测试环境

    1-1 课程介绍 2-1 Jenkins安装 2-2 Jenkins插件 2-3 Jenkins基础设置 3-1 Linux系统准备 3-2 安装Java环境 3-3 安装并配置Git 3-4 安装并 ...

  5. Linux常用命令及工具记录(持续更新)

    一.命令 convmv   作用:文件名的编码转换   安装:sudo apt-get install convmv   使用:convmv * -f gbk -t utf8 --notest   c ...

  6. neutron floating ip 限速

    查看浮动ip的id [root@10e131e69e14 oz]# openstack floating ip show 36.111.0.197 +---------------------+--- ...

  7. ./configure, make, sudo make install 的含义

    一般编译安装会用到. 将压缩包example.tar.gz解压到onePackage下example, 在onePackage下新建install文件夹. 在终端中执行 1) 配置sudo ./con ...

  8. c++知识点总结--new的一些用法

    new operator 将对象产生与heap,不但分配内存而且为该对象调用一个constructor   operator new只是分配内存,没有constructor被调用 有个一个特殊版本,称 ...

  9. Java -- Matrix的一点认识

    例如在如下一段代码中: Matrix m = new Matrix(2,3); for(int i=0; i<m.getRowDimension(); i++) { for(int j=0; j ...

  10. [洛谷P3807]【模板】卢卡斯定理

    题目大意:给你$n,m,p(p \in \rm prime)$,求出$C_{n + m}^m\bmod p(可能p\leqslant n,m)$ 题解:卢卡斯$Lucas$定理,$C_B^A\bmod ...