1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| #include <iostream> #include <cstdio> #include <stack> using namespace std; const int N = 2e6 + 10, M = 2e6 + 10; int n, m; int idx, hd[N], nxt[M], edg[M]; bool vis[N]; int cnt, stmp, id[N], dfn[N], low[N]; stack<int> stk; void tarjan(int x) { dfn[x] = low[x] = ++stmp; stk.push(x); vis[x] = true; for (int i = hd[x]; ~i; i = nxt[i]) if (!dfn[edg[i]]) { tarjan(edg[i]); low[x] = min(low[x], low[edg[i]]); } else if (vis[edg[i]]) low[x] = min(low[x], dfn[edg[i]]); if (low[x] == dfn[x]) { int y; cnt++; do { y = stk.top(); stk.pop(); vis[y] = false; id[y] = cnt; } while (y != x); } } void add(int a, int b) { nxt[++idx] = hd[a]; hd[a] = idx; edg[idx] = b; } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < (n << 1); i++) hd[i] = -1; while (m--) { int i, a, j, b; scanf("%d%d%d%d", &i, &a, &j, &b); i--, j--; add(i << 1 | !a, j << 1 | b); add(j << 1 | !b, i << 1 | a); } for (int i = 0; i < n << 1; i++) if (!dfn[i]) tarjan(i); for (int i = 0; i < n; i++) if (id[i << 1] == id[i << 1 | 1]) { printf("IMPOSSIBLE\n"); return 0; } printf("POSSIBLE\n"); for (int i = 0; i < n; i++) printf("%d ", id[i << 1] > id[i << 1 | 1]); return 0; }
|