您的位置:首页 > 其它

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
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: