标签:off possible tput width can through const test start
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 9022 | Accepted: 3503 |
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 <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <stack> using namespace std; const int maxn = 10010,maxm = 200010; int n,m,key[maxn],a[maxn],b[maxn],pre[maxn],low[maxn],scc[maxn],dfs_clock,head[maxn],nextt[maxm],to[maxm],tot = 1,top; stack<int> s; void add(int x,int y) { to[tot] = y; nextt[tot] = head[x]; head[x] = tot++; } void tarjan(int u) { pre[u] = low[u] = ++dfs_clock; s.push(u); for (int i = head[u];i;i = nextt[i]) { int v = to[i]; if (!pre[v]) { tarjan(v); low[u] = min(low[u],low[v]); } else if (!scc[v]) low[u] = min(low[u],pre[v]); } if (low[u] == pre[u]) { top++; while (1) { int t = s.top(); s.pop(); scc[t] = top; if (t == u) break; } } } bool solve() { for (int i = 0; i < n * 2; i++) if (!pre[i]) tarjan(i); for (int i = 0; i < n * 2; i += 2) if (scc[i] == scc[i ^ 1]) return false; return true; } int main() { while (scanf("%d%d",&n,&m) && (n || m)) { memset(head,0,sizeof(head)); tot = 1; for (int i = 0; i < n; i++) { int u,v; scanf("%d%d",&u,&v); key[u] = i * 2; key[v] = i * 2 + 1; } for (int i = 0; i < m; i++) scanf("%d%d",&a[i],&b[i]); for (int i = 0; i < m; i++) { if (a[i] == b[i]) add(key[a[i]] ^ 1,key[a[i]]); //必须选,所以表示不选的点要连向选的点 else { add(key[a[i]] ^ 1,key[b[i]]); add(key[b[i]] ^ 1,key[a[i]]); } memset(pre,0,sizeof(pre)); memset(low,0,sizeof(low)); memset(scc,0,sizeof(scc)); top = 0; dfs_clock = 0; if (!solve()) { printf("%d\n",i); break; } if (i == m - 1) printf("%d\n",m); } } return 0; }
标签:off possible tput width can through const test start
原文地址:http://www.cnblogs.com/zbtrs/p/7528328.html