Q - Simple Path
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
Submit Status Practice ZOJ 3583
Appoint description:
System Crawler (2015-04-16)
Description
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 简单路上的点一定与s,d联通
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<cstdlib> #include<algorithm> #include<queue> #include<vector> #include<stack> #include<set> using namespace std; int n,m,s,d,mp[105][105],vis[105],mark[105]; void dfs(int x) { for(int i=0;i<n;i++) { if(!vis[i]&&mp[x][i]) { vis[i]=1; dfs(i); } } } int main() { int x,y; while(scanf("%d%d%d%d",&n,&m,&s,&d)!=EOF) { int ans=0; memset(mp,0,sizeof(mp)); memset(mark,0,sizeof(mark)); for(int i=1;i<=m;i++) { scanf("%d%d",&x,&y); mp[x][y]=mp[y][x]=1; } for(int i=0;i<n;i++) { memset(vis,0,sizeof(vis)); vis[i]=1; if(!vis[s]) dfs(s); if(!vis[d]) dfs(d); for(int j=0;j<n;j++) if(!vis[j]&&j!=s&&j!=d) mark[j]=1; } for(int i=0;i<n;i++) if(mark[i]) ans++; printf("%d\n",ans); } return 0; }