ZOJ 3583 Simple Path[并查集]
2012-03-15 13:01
260 查看
A path with no repeated vertices of an undirected graph is called a simple path. Given an undirected graph and two verteices S and D, return the number of vertics which don't lie on any simple paths between S and D.
Input
The input contains multiple test cases.
Each case starts with a line of four integers, N(1 < N ≤ 100), M(1 ≤ M ≤ N(N - 1) / 2), S(0 ≤ S < N), D(0 ≤ D < N). N is the number of vertices, M is the number of edges, S and D are two different vertices. Then M lines follow, each line contains two different integers A(0 ≤ A < N) and B(0 ≤ B < N), which represents an edge of the graph. It's ensure that there is at least one simple path between S and D.
Output
Output the number of such vertics, one line per case.
Sample Input
4 3 0 2
0 1
1 2
1 3
4 4 0 2
0 1
1 2
1 3
2 3
Sample Output
1
0
深搜找不在任意简单路径上出现的点,超时了。
View Code
Input
The input contains multiple test cases.
Each case starts with a line of four integers, N(1 < N ≤ 100), M(1 ≤ M ≤ N(N - 1) / 2), S(0 ≤ S < N), D(0 ≤ D < N). N is the number of vertices, M is the number of edges, S and D are two different vertices. Then M lines follow, each line contains two different integers A(0 ≤ A < N) and B(0 ≤ B < N), which represents an edge of the graph. It's ensure that there is at least one simple path between S and D.
Output
Output the number of such vertics, one line per case.
Sample Input
4 3 0 2
0 1
1 2
1 3
4 4 0 2
0 1
1 2
1 3
2 3
Sample Output
1
0
深搜找不在任意简单路径上出现的点,超时了。
View Code
如果是简单路径上的点,不管去掉哪一个点,都能使改点和s,或 t属于同一个集合,如果不是简单路径上的点,一定能找到一个点删掉以后使它不与s,t属于同一个集合。
#include<stdio.h> #include<string.h> #define clr(x)memset(x,0,sizeof(x)) int f[102]; int g[102][102]; int find(int x) { int r=x; while(r!=f[r]) r=f[r]; int i=x,j; while(i!=r) { j=f[i]; f[j]=r; i=j; } return r; } void join(int x,int y) { int fx=find(x); int fy=find(y); if(fx!=fy) { if(fx<fy) f[fy]=fx; else f[fx]=fy; } } int main() { int p,q,s,t,n,m,i,j,k; int res[102]; while(scanf("%d%d%d%d",&n,&m,&s,&t)!=EOF) { clr(g); clr(res); for(i=0;i<m;i++) { scanf("%d%d",&p,&q); g[p][q]=g[q][p]=1; } for(i=0;i<n;i++) { for(j=0;j<n;j++) f[j]=j; for(j=0;j<n;j++) for(k=j+1;k<n;k++) { if(j!=i&&k!=i&&g[j][k]) join(j,k); } for(j=0;j<n;j++) if(j!=i&&find(j)!=find(s)&&find(j)!=find(t)) res[j]=1; } int tot=0; for(i=0;i<n;i++) if(res[i]) tot++; printf("%d\n",tot); } return 0; }
相关文章推荐
- zoj 3583 simple path
- zoj 3583 Simple Path
- [并查集+路径压缩]zoj 3261:Connections in Galaxy War
- zoj 3261 逆向并查集+离线处理
- ZOJ Problem Set - 3656 Bit Magic 并查集
- ZOJ 3811 dfs&并查集
- zoj 2833 Friendship(并查集)
- ZOJ 3761 —— Easy billiards(并查集+深搜)
- zoj 3641 并查集
- hdu 4424 & zoj 3659 Conquer a New Region (并查集 + 贪心)
- ZOJ 3261 Connections in Galaxy War(逆向并查集)
- ZOJ Information Sharing 【并查集+set】
- ZOJ-2833Friendship(简单并查集)
- ZOJ 3261 Connections in Galaxy War (并查集)
- ZOJ 3216(并查集)
- ZOJ 3789 Gears(并查集)
- ZOJ 3261 - Connections in Galaxy War ,并查集删边
- 【并查集】 ZOJ 3789 Gears
- ZOJ 3080 ChiBi(SPFA,并查集)
- zoj 3583