标签:
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 7758 | Accepted: 2969 |
Description
Input
Output
Sample Input
3 6 0 3 1 2 4 5 0 1 0 2 4 1 4 2 3 5 2 2 0 0
Sample Output
4
Source
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<cstdlib> #include<algorithm> #include<queue> #include<vector> #include<stack> using namespace std; vector<int> mp[8000]; stack<int> s; int n,m,key1[8000],key2[8000],door1[8000],door2[8000]; int Dfs[8000],low[8000],use[8000],isstack[8000]; int top,newflag; void init() { for(int i=0;i<4*n;i++) mp[i].clear(); while(!s.empty()) s.pop(); memset(Dfs,0,sizeof(Dfs)); memset(low,0,sizeof(low)); memset(use,0,sizeof(use)); memset(isstack,0,sizeof(isstack)); top=0; newflag=0; } void tarjan(int u) { Dfs[u]=low[u]=++top; s.push(u); isstack[u]=1; for(int i=0;i<mp[u].size();i++) { int v=mp[u][i]; if(!Dfs[v]) { tarjan(v); low[u]=min(low[u],low[v]); } else if(isstack[v]) { low[u]=min(low[u],Dfs[v]); } } if(low[u]==Dfs[u]) { newflag++; int x; do { x=s.top(); s.pop(); use[x]=newflag; isstack[x]=0; }while(x!=u); } } bool check(int x) { init(); for(int i=1;i<=n;i++) { mp[key1[i]*2].push_back(key2[i]*2+1); mp[key2[i]*2].push_back(key1[i]*2+1); } for(int i=1;i<=x;i++) { mp[door1[i]*2+1].push_back(door2[i]*2); mp[door2[i]*2+1].push_back(door1[i]*2); } for(int i=0;i<4*n;i++) { if(!Dfs[i]) tarjan(i); } for(int i=0;i<2*n;i++) { if(use[i*2]==use[i*2+1]) { return false; } } return true; } int main() { while(scanf("%d%d",&n,&m)!=EOF) { if(n==0&&m==0) break; for(int i=1;i<=n;i++) { scanf("%d%d",&key1[i],&key2[i]); } for(int i=1;i<=m;i++) { scanf("%d%d",&door1[i],&door2[i]); } int l,r,ans=0; l=0,r=m; while(l<=r) { int mid=(l+r)>>1; if(check(mid)) { ans=mid; l=mid+1; } else { r=mid-1; } } printf("%d\n",ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/a972290869/p/4439681.html