标签:des com http style blog class div img code c log
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 7295 | Accepted: 2778 |
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
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <stack> 6 #include <vector> 7 8 using namespace std; 9 10 const int MAX_N = (1 << 11) + 5; 11 int N,M; 12 int low[MAX_N],pre[MAX_N],cmp[MAX_N]; 13 int dfs_clock,scc_cnt; 14 stack <int > S; 15 int no[MAX_N],x[3000],y[3000]; 16 vector<int> G[MAX_N]; 17 18 19 void dfs(int u) { 20 pre[u] = low[u] = ++dfs_clock; 21 S.push(u); 22 for(int i = 0; i < G[u].size(); ++i) { 23 int v = G[u][i]; 24 if(!pre[v]) { 25 dfs(v); 26 low[u] = min(low[u],low[v]); 27 } else if(!cmp[v]) { 28 low[u] = min(low[u],pre[v]); 29 } 30 } 31 32 if(low[u] == pre[u]) { 33 ++scc_cnt; 34 for(;;) { 35 int x = S.top(); S.pop(); 36 cmp[x] = scc_cnt; 37 if(x == u) break; 38 } 39 } 40 } 41 void scc() { 42 dfs_clock = scc_cnt = 0; 43 memset(cmp,0,sizeof(cmp)); 44 memset(pre,0,sizeof(pre)); 45 46 for(int i = 0; i < 2 * N; ++i) { 47 if(!pre[i]) dfs(i); 48 } 49 } 50 bool check(int m) { 51 for(int i = 0; i < 2 * N; ++i) { 52 G[i].clear(); 53 } 54 for(int i = 1; i <= m; ++i) { 55 int a = x[i],b = y[i]; 56 G[no[a]].push_back(b); 57 G[no[b]].push_back(a); 58 } 59 scc(); 60 for(int i = 0; i < 2 * N; ++i) { 61 if(cmp[i] == 0) continue; 62 if(cmp[i] == cmp[ no[i] ]) { 63 //printf("i = %d no = %d %d %d\n",i,no[i],cmp[i],cmp[no[i]]); 64 return false; 65 } 66 } 67 68 return true; 69 } 70 void solve() { 71 int l = 0,r = M; 72 while(l < r) { 73 int mid = (l + r + 1) / 2; 74 if(check(mid)) l = mid; 75 else r = mid - 1; 76 } 77 printf("%d\n",l); 78 } 79 int main() 80 { 81 //freopen("sw.in","r",stdin); 82 while(~scanf("%d%d",&N,&M)) { 83 if(N == 0 && M == 0) break; 84 for(int i = 1; i <= N; ++i) { 85 int a,b; 86 scanf("%d%d",&a,&b); 87 no[a] = b; 88 no[b] = a; 89 } 90 for(int i = 1; i <= M; ++i) { 91 scanf("%d%d",&x[i],&y[i]); 92 } 93 94 solve(); 95 96 } 97 return 0; 98 }
标签:des com http style blog class div img code c log
原文地址:http://www.cnblogs.com/hyxsolitude/p/3702067.html