bool vis[N]; queue<int> Q; int a[N], bl[N], w[N], scc, deg[N], f[N]; vector<int> e[N], re[N], post, g[N];
voiddfs1(int u){ vis[u] = 1; for (auto v : re[u]) if (!vis[v]) dfs1(v); post.push_back(u); }
voiddfs2(int u){ bl[u] = scc; w[scc] += a[u]; for (auto v : e[u]) if (!bl[v]) dfs2(v); }
intmain(){ int n = rd(), m = rd(); for (int i = 1; i <= n; ++i) a[i] = rd(); for (int i = 1; i <= m; ++i) { int u = rd(), v = rd(); e[u].push_back(v); re[v].push_back(u); } for (int u = 1; u <= n; ++u) if (!vis[u]) dfs1(u); reverse(post.begin(), post.end()); for (auto u : post) if (!bl[u]) {++scc; dfs2(u);} for (int u = 1; u <= n; ++u) for (auto v : e[u]) if (bl[u] != bl[v]) {g[bl[u]].push_back(bl[v]); ++deg[bl[v]];} for (int u = 1; u <= scc; ++u) if (!deg[u]) {f[u] = w[u]; Q.push(u);} while (!Q.empty()) { int u = Q.front(); Q.pop(); for (auto v : g[u]) { f[v] = max(f[v], f[u] + w[v]); --deg[v]; if (!deg[v]) Q.push(v); } } int ans = 0; for (int u = 1; u <= scc; ++u) ans = max(ans, f[u]); printf("%d\n", ans); return0; }
bool vis[N]; int bl[N], scc; vector<int> e[N], re[N], post;
voiddfs1(int u){ vis[u] = 1; for (auto v : e[u]) if (!vis[v]) dfs1(v); post.push_back(u); }
voiddfs2(int u){ bl[u] = scc; for (auto v : re[u]) if (!bl[v]) dfs2(v); }
intmain(){ int n = rd(), m = rd(); for (int i = 1; i <= m; ++i) { int u = rd(), a = rd(); int v = rd(), b = rd(); e[u + a * n].push_back(v + (1 - b) * n); e[v + b * n].push_back(u + (1 - a) * n); re[v + (1 - b) * n].push_back(u + a * n); re[u + (1 - a) * n].push_back(v + b * n); } for (int u = 1; u <= (n << 1); ++u) if (!vis[u]) dfs1(u); reverse(post.begin(), post.end()); for (auto u : post) if (!bl[u]) {++scc; dfs2(u);} for (int u = 1; u <= n; ++u) if (bl[u] == bl[u + n]) {puts("IMPOSSIBLE"); return0;} puts("POSSIBLE"); for (int u = 1; u <= n; ++u) { if (bl[u] < bl[n + u]) printf("0 "); elseprintf("1 "); } return0; }
洛谷P5782
每个党派有两个人 ( 和 ),两个人里选一个出席委员会。在所有人中,存在限制条件。条件形式如下 a b 表示 a 和 b 不能同时出席。问是否有解,如果有,给出一种委员会的成员表。
bool vis[N]; int bl[N], scc, deg[N]; vector<int> e[N], re[N], post;
voiddfs1(int u){ vis[u] = 1; for (auto v : e[u]) if (!vis[v]) dfs1(v); post.push_back(u); }
voiddfs2(int u){ bl[u] = scc; for (auto v : re[u]) if (!bl[v]) dfs2(v); }
intmain(){ int n = (rd() << 1), m = rd(); for (int i = 1; i <= m; ++i) { int u = rd(), v = rd(); e[u].push_back(v + n); e[v].push_back(u + n); re[v + n].push_back(u); re[u + n].push_back(v); } for (int i = 1; i <= n; ++i) { e[2 * i - 1].push_back(2 * i + n); e[2 * i].push_back(2 * i - 1 + n); e[2 * i + n].push_back(2 * i - 1); e[2 * i - 1 + n].push_back(2 * i); re[2 * i - 1].push_back(2 * i + n); re[2 * i].push_back(2 * i - 1 + n); re[2 * i + n].push_back(2 * i - 1); re[2 * i - 1 + n].push_back(2 * i); } for (int u = 1; u <= (n << 1); ++u) if (!vis[u]) dfs1(u); reverse(post.begin(), post.end()); for (auto u : post) if (!bl[u]) {++scc; dfs2(u);} for (int u = 1; u <= n; ++u) if (bl[u] == bl[u + n]) {puts("NIE"); return0;} for (int u = 1; u <= n; ++u) if (bl[u] > bl[n + u]) printf("%d\n", u); return0; }
bool vis[N]; int bl[N], scc; vector<int> e[N], re[N], post;
voiddfs1(int u){ vis[u] = 1; for (auto v : e[u]) if (!vis[v]) dfs1(v); post.push_back(u); }
voiddfs2(int u){ bl[u] = scc; for (auto v : re[u]) if (!bl[v]) dfs2(v); }
inlinevoidwork(){ memset(vis, 0, sizeof(vis)); memset(bl, 0, sizeof(bl)); scc = 0; post.clear(); int n = rd(), m = rd(); for (int i = 1; i <= (n << 1); ++i) {e[i].clear(); re[i].clear();} for (int i = 1; i <= m; ++i) { char c1 = getchar(); for (; c1 != 'm' && c1 != 'h'; c1 = getchar()); int u = rd(); char c2 = getchar(); for (; c2 != 'm' && c2 != 'h'; c2 = getchar()); int v = rd(); int a = (c1 == 'm'), b = (c2 == 'm'); e[u + a * n].push_back(v + (1 - b) * n); e[v + b * n].push_back(u + (1 - a) * n); re[v + (1 - b) * n].push_back(u + a * n); re[u + (1 - a) * n].push_back(v + b * n); } for (int u = 1; u <= (n << 1); ++u) if (!vis[u]) dfs1(u); reverse(post.begin(), post.end()); for (auto u : post) if (!bl[u]) {++scc; dfs2(u);} for (int u = 1; u <= n; ++u) if (bl[u] == bl[u + n]) {puts("BAD"); return;} puts("GOOD"); }
intmain(){ for (int t = rd(); t; --t) work(); return0; }