CodeForces - 505B 图的连同性,多维并查集或用搜索写
2017-08-14 09:02
465 查看
4000
Mr. Kitayuta has just bought an undirected graph consisting of n vertices and medges. 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.
Example
Input
Output
Input
Output
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.
题意:
输入你q个问题,下面q行,每行有两个点,找取两个点有几条路连通,每种颜色代表一条路;
小编因为英语的硬伤,没有理解清题意,以为两个颜色相同就行了,谁知道还要判断连通性;
在这里小编提醒一下各位博友,做题时一定要审清题;
方法一:搜索 bfs
方法二:
多维 并查集
Mr. Kitayuta has just bought an undirected graph consisting of n vertices and medges. 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.
Example
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.
题意:
输入你q个问题,下面q行,每行有两个点,找取两个点有几条路连通,每种颜色代表一条路;
小编因为英语的硬伤,没有理解清题意,以为两个颜色相同就行了,谁知道还要判断连通性;
在这里小编提醒一下各位博友,做题时一定要审清题;
方法一:搜索 bfs
#include<iostream> //知道前面为啥一直错吗,题意不理解 ,颜色相同,还得必须连通; #include<stdio.h> // 这是用搜索做的; #include<string.h> // (1)以后做题先申清题 ,审不清题,就别做; #include<vector> #include<queue> #include<algorithm> using namespace std; struct node { int e,num; }; vector<node> v[110]; int n,m,sum,x,y,book[110][110]; void init() { for(int i=0;i<=n;i++) v[i].clear(); } void bfs() { queue<node> q; int i; for(i=0;i<v[x].size();i++) { node st=v[x][i]; book[st.e][st.num]=1; q.push(st); } while(!q.empty()) { node st=q.front(); q.pop(); if(st.e==y) { sum++; continue; } for(i=0;i<v[st.e].size();i++) { node end=v[st.e][i]; if(!book[end.e][end.num]&&st.num==end.num) { book[end.e][end.num]=1; q.push(end); } } } } int main() { int i,j,k,t; while(~scanf("%d%d",&n,&m)) { init(); int x1,y1,v1; for(i=0;i<m;i++) { scanf("%d%d%d",&x1,&y1,&v1); node st; st.e=y1; st.num=v1; v[x1].push_back(st); st.e=x1; v[y1].push_back(st); } scanf("%d",&t); while(t--) { sum=0; memset(book,0,sizeof(book)); scanf("%d%d",&x,&y); fill(book[x],book[x]+101,1); bfs(); printf("%d\n",sum); } } return 0; }
方法二:
多维 并查集
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int n,m,f[110][110]; void init() { for(int i=0;i<=m;i++) for(int j=0;j<=n;j++) f[i][j]=j; } int find(int x,int v) { if(f[v][x]==x) return x; else { f[v][x]=find(f[v][x],v); return f[v][x]; } } int main() { int i,j,k,t; while(~scanf("%d%d",&n,&m)) { init(); int x1,y1,v1; for(i=0;i<m;i++) { scanf("%d%d%d",&x1,&y1,&v1); int t1=find(x1,v1); int t2=find(y1,v1); if(t1<t2) f[v1][t2]=t1; else f[v1][t1]=t2; } int x,y; scanf("%d",&t); for(i=0;i<t;i++) { scanf("%d%d",&x,&y); int sum=0; for(j=1;j<=m;j++) { if(find(x,j)==find(y,j)) sum++; } printf("%d\n",sum); } } return 0; }
相关文章推荐
- CodeForces - 505B 并查集或搜索
- CodeForces 510B 无向图找环的两种方法(搜索与并查集)
- Codeforces 505B - Mr. Kitayuta's Colorful Graph DFS或并查集
- CodeForces - 505B Mr. Kitayuta's Colorful Graph —— 多维并查集
- CodeForces 505B(并查集)
- 【搜索】【并查集】Codeforces 691D Swaps in Permutation
- 【搜索】【并查集】Codeforces 691D Swaps in Permutation
- CodeForces 292D Connected Components(并查集 前后缀)
- codeforces 91c 并查集
- CodeForces - 731C Socks(并查集)(贪心)
- [Codeforces 325 D. Reclamation]并查集
- Codeforces 870E Points, Lines and Ready-made Titles 并查集
- 并查集 codeforces party
- CodeForces - 698B Fix a Tree(并查集)
- 【并查集】Codeforces 566D Restructuring Company
- CodeForces 722C Destroying Array(并查集和我奇怪的二叉树)
- CodeForces 716B Complete The Graph(二分搜索)(最短路)
- codeforces 455C C. Civilization(树形dp+树的直径+并查集)
- Codeforces 633D Fibonacci-ish(搜索)
- CodeForces 445B - DZY Loves Chemistry(并查集)