转载请注明出处: 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.

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

 #include <iostream>
using namespace std;
#define MAXN 110
int pa[][],ra[][];
void init()
{
for(int i=;i<MAXN;i++)
{
for(int j=;j<MAXN;j++){
pa[i][j]=j;
ra[i][j]=;
}
}
}
int find(int x,int c){
if(pa[c][x]!=x)pa[c][x]=find(pa[c][x],c);
return pa[c][x];
}
int unite(int x,int y,int c){
x=find(x,c);
y=find(y,c);
if(x==y)return ;
if(ra[c][x]<ra[c][y])
{
pa[c][x]=y;
}else{
pa[c][y]=x;
if(ra[c][x]==ra[c][y])ra[c][x]++;
}
return ;
}
bool same(int x,int y,int c){
return find(x,c)==find(y,c);
} int main()
{
ios::sync_with_stdio(false);
int n,m;
init();
cin>>n>>m;
int u,v,c;
for(int i=;i<m;i++){
cin>>u>>v>>c;
u--,v--,c--;
unite(u,v,c);
}
int q;
cin>>q;
for(int i=;i<q;i++){
cin>>u>>v;
u--;v--;
int ans=;
for(int i=;i<m;i++)
{
if(same(u,v,i))ans++;
}
cout<<ans<<endl;
} return ;
}

代码君

codeforces 505B Mr. Kitayuta's Colorful Graph(水题)的更多相关文章

  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 二维并查集

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

  3. Codeforces 506D Mr. Kitayuta's Colorful Graph(分块 + 并查集)

    题目链接  Mr. Kitayuta's Colorful Graph 把每种颜色分开来考虑. 所有的颜色分为两种:涉及的点的个数 $> \sqrt{n}$    涉及的点的个数 $<= ...

  4. CodeForces 506D Mr. Kitayuta's Colorful Graph

    brute force ? 其实是平方分解.很容易想到的是每一个颜色建一个图,然后并查集维护一下连通性. 问题在于颜色有O(m)种,每种颜色的图点数都是O(n)的,因此并查集的空间只能重复利用. 但是 ...

  5. Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph 并查集

    D. Mr. Kitayuta's Colorful Graph Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/ ...

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

    题目传送门 /* 题意:两点之间有不同颜色的线连通,问两点间单一颜色连通的路径有几条 DFS:暴力每个颜色,以u走到v为结束标志,累加条数 注意:无向图 */ #include <cstdio& ...

  7. Codeforces Round #286 (Div. 2) B. Mr. Kitayuta's Colorful Graph dfs

    B. Mr. Kitayuta's Colorful Graph time limit per test 1 second memory limit per test 256 megabytes in ...

  8. Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph

    D - Mr. Kitayuta's Colorful Graph 思路:我是暴力搞过去没有将答案离线,感觉将答案的离线的方法很巧妙.. 对于一个不大于sqrt(n) 的块,我们n^2暴力枚举, 对于 ...

  9. B. Mr. Kitayuta's Colorful Graph,二维并查集,一个简单变形就可以水过了~~

    B. Mr. Kitayuta's Colorful Graph ->  Link  <- 题目链接在上面,题目比较长,就不贴出来了,不过这是道很好的题,很多方法都可以做,真心邀请去A了这 ...

随机推荐

  1. Truck History--poj1789

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 21534   Accepted: 8379 De ...

  2. 选址问题lingo求解

    大家好,我是小鸭酱,博客地址为:http://www.cnblogs.com/xiaoyajiang model : sets : H/h1..h2/:x , y , e ; L/l1..l6/: a ...

  3. [转]学DSP、FPGA、ARM,哪个更有前途?

    1.这世界真是疯了,貌似有人连FPGA原理是什么都不知道就开始来学习FPGA了. 2.DSP就是一个指令比较独特的处理器.它虽然是通用处理器,但是实际上不怎么“通用”.技术很牛的人可以用DSP做一台电 ...

  4. BZOJ 2324 营救皮卡丘

    http://www.lydsy.com/JudgeOnline/problem.php?id=2324 思路:最小费用最大流 考虑设数组d[k][i][j],代表只用前k个城市,i到j的最短路 然后 ...

  5. 逻辑数据库设计 - 需要ID(谈主键Id)

    本文的目标就是要确认那些使用了主键,却混淆了主键的本质而造成的一种反模式. 一.确立主键规范 每个了解数据库设计的人都知道,主键对于一张表来说是一个很重要,甚至必需的部分.这确实是事实,主键是好的数据 ...

  6. Redhat Enterprise Linux中如何关闭SELinux?

    转自http://www.cnitblog.com/lywaml/archive/2005/06/21/468.html 红帽企业 Linux 4 包括了一个 SELinux 的实现.SELinux ...

  7. c++ 09

    一.数据结构 程序设计=数据结构+算法 1.逻辑结构 1)集合:元素之间没有联系. 2)线性结构:元素之间存在前后顺序. 3)树形结构:元素之间存在一对多的父子关系. 4)图状结构:元素之间存在多对多 ...

  8. poj3299

                                                                                                         ...

  9. SoftLayerDebug

  10. web应用的发布

    将web应用打包成.war类型的...因为将其发布到服务器时,其自动解压...非常方便