Codeforces Round #286 (Div. 2) B. Mr. Kitayuta's Colorful Graph +foyd算法的应用

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.

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.

解决方案:题目求的是先建立一个图,有很多种不同颜色的边,每次询问两条边的不同颜色的路径的个数。由此联想的floyd算法,可以枚举边的颜色,求每种颜色的最短路,然后在统计不同颜色的最短路的个数。

code:

<pre name="code" class="cpp">#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int Mat[102][102][102];
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        memset(Mat,0,sizeof(Mat));
        for(int i=1; i<=m; i++)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            Mat[a][b][c]=1;
            Mat[b][a][c]=1;
        }
        for(int c=1;c<=m;c++){
            for(int k=1;k<=n;k++){
                for(int i=1;i<=n;i++)
                for(int j=1;j<=n;j++){
                    if(Mat[i][k][c]&&Mat[k][j][c]){
                        Mat[i][j][c]=1;
                        Mat[j][i][c]=1;
                    }
                }
            }
        }
        int q;
        scanf("%d",&q);
        for(int i=0;i<q;i++){
            int a,b;
            int sum=0;
            scanf("%d%d",&a,&b);
            for(int i=1;i<=m;i++){
                sum+=Mat[a][b][i];
            }
            printf("%d\n",sum);
        }
    }
    return 0;
}

Codeforces Round #286 (Div. 2) B. Mr. Kitayuta's Colorful Graph +foyd算法的应用

时间: 2025-01-02 18:05:43

Codeforces Round #286 (Div. 2) B. Mr. Kitayuta's Colorful Graph +foyd算法的应用的相关文章

Codeforces Round #286 (Div. 2)B. Mr. Kitayuta&#39;s Colorful Graph(dfs,暴力)

数据规模小,所以就暴力枚举每一种颜色的边就行了. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include<set> #include<vector> #include<algorithm>

DFS/并查集 Codeforces Round #286 (Div. 2) B - Mr. Kitayuta&#39;s Colorful Graph

题目传送门 1 /* 2 题意:两点之间有不同颜色的线连通,问两点间单一颜色连通的路径有几条 3 DFS:暴力每个颜色,以u走到v为结束标志,累加条数 4 注意:无向图 5 */ 6 #include <cstdio> 7 #include <iostream> 8 #include <algorithm> 9 #include <cstring> 10 #include <string> 11 #include <vector> 1

Codeforces Round #286 (Div. 1) D. Mr. Kitayuta&#39;s Colorful Graph

D - Mr. Kitayuta's Colorful Graph 思路:我是暴力搞过去没有将答案离线,感觉将答案的离线的方法很巧妙.. 对于一个不大于sqrt(n) 的块,我们n^2暴力枚举, 对于大于sqrt(n)的块,我们暴力枚举答案. 这样就能做到严格sqrt(n) * n #include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk make_pair #defin

Codeforces Round #286 (Div. 2)A. Mr. Kitayuta&#39;s Gift(暴力,string的应用)

由于字符串的长度很短,所以就暴力枚举每一个空每一个字母,出现行的就输出.这么简单的思路我居然没想到,临场想了很多,以为有什么技巧,越想越迷...是思维方式有问题,遇到问题先分析最简单粗暴的办法,然后一步一步的优化,不能盲目的想. 这道题要AC的快需要熟悉string的各种用法.这里做个简单总结:C++中string的常见用法. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstrin

水题 Codeforces Round #286 (Div. 2) A Mr. Kitayuta&#39;s Gift

题目传送门 1 /* 2 水题:vector容器实现插入操作,暴力进行判断是否为回文串 3 */ 4 #include <cstdio> 5 #include <iostream> 6 #include <algorithm> 7 #include <cstring> 8 #include <string> 9 #include <vector> 10 using namespace std; 11 12 const int MAXN

Codeforces Round #286 (Div. 2) C. Mr. Kitayuta, the Treasure Hunter+dp+优化

C. Mr. Kitayuta, the Treasure Hunter time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output The Shuseki Islands are an archipelago of 30001 small islands in the Yutampo Sea. The islands are evenly

Codeforces Round #286 (Div. 1) A. Mr. Kitayuta, the Treasure Hunter DP

链接: http://codeforces.com/problemset/problem/506/A 题意: 给出30000个岛,有n个宝石分布在上面,第一步到d位置,每次走的距离与上一步的差距不大于1,问走完一路最多捡到多少块宝石. 题解: 容易想到DP,dp[i][j]表示到达 i 处,现在步长为 j 时最多收集到的财富,转移也不难,cnt[i]表示 i 处的财富. dp[i+step-1] = max(dp[i+step-1],dp[i][j]+cnt[i+step+1]) dp[i+st

Codeforces Round #286 div.1 D 506D D. Mr. Kitayuta&#39;s Colorful Graph【并查集】

题目链接:http://codeforces.com/problemset/problem/506/D 题目大意: 给出n个顶点,m条边,每条边上有一个数字,代表某个颜色.不同数字代表不同的颜色.有很多个询问,每个询问问有多少条纯种颜色的路径使得某两个点联通. 分析: 这个题一看就想用并查集来搞,每种颜色用一个并查集处理.对于输入的每条边,我们只需要将这两个点在这条边的颜色对应的并查集中合并就好了.查询的时候,我们只需要检测这两个点在多少个颜色对应的并查集中是在统一集合中的,那么就有多少条纯种颜

Codeforces Round #286 div.2 D 505D. Mr. Kitayuta&#39;s Technology【强连通分量,弱联通分量】

题目链接:http://codeforces.com/contest/505/problem/D 题目大意: 在一个图中,有n个顶点,给出m对数字(u,v)表示顶点u和顶点v必须直接或者间接相连,让你构造一个这样的图,输出最少需要多少条边. 分析: 毫无疑问,n个顶点的话,我们最多可以用n条边,使得n个顶点构成一个环,满足所有的情况(任意两点都是联通的),但是这并不一定是最少的边. 于是我们还需要找一个方法确定最少需要多少条边. 首先我们利用题目给出的点对建图,得到图G.对于G中的弱联通分量来说