diff --git a/OnlineJudges/lydsy/1053.cpp b/OnlineJudges/lydsy/1053.cpp new file mode 100644 index 0000000..053b218 --- /dev/null +++ b/OnlineJudges/lydsy/1053.cpp @@ -0,0 +1,26 @@ +#include +int a[] = { + 1, 2, 4, 6, 12, 24, + 36, 48, 60, 120, 180, 240, + 360, 720, 840, 1260, 1680, 2520, + 5040, 7560, 10080, 15120, 20160, 25200, + 27720, 45360, 50400, 55440, 83160, 110880, + 166320, 221760, 277200, 332640, 498960, 554400, + 665280, 720720, 1081080, 1441440, 2162160, 2882880, + 3603600, 4324320, 6486480, 7207200, 8648640, 10810800, + 14414400, 17297280, 21621600, 32432400, 36756720, 43243200, + 61261200, 73513440, 110270160, 122522400, 147026880, 183783600, + 245044800, 294053760, 367567200, 551350800, 698377680, 735134400, + 1102701600, 1396755360 }; +int main() +{ + int x; + scanf("%d", &x); + for (int i = 67; i >= 0; i--) + if (a[i] <= x) + { + printf("%d", a[i]); + break; + } + return 0; +} \ No newline at end of file diff --git a/OnlineJudges/lydsy/1221.cpp b/OnlineJudges/lydsy/1221.cpp new file mode 100644 index 0000000..81e0833 --- /dev/null +++ b/OnlineJudges/lydsy/1221.cpp @@ -0,0 +1,63 @@ +#include +#include +inline int min(int a, int b) { return a < b ? a : b; } +const int inf = 0x3f3f3f3f, maxn = 2010, maxm = maxn << 4; +int head[maxn], next[maxm], to[maxm], cap[maxm], cost[maxm], ecnt; +int que[maxn << 4], curFlow[maxn], dis[maxn], fa[maxn]; +bool inq[maxn]; +inline void addEdge_impl_(int f, int t, int ca, int co) +{ + next[ecnt] = head[f]; + head[f] = ecnt; + to[ecnt] = t; + cap[ecnt] = ca; + cost[ecnt] = co; + ecnt++; +} +inline void addEdge(int f, int t, int ca, int co) +{ + addEdge_impl_(f, t, ca, co); + addEdge_impl_(t, f, 0, -co); +} +bool MCMF(int S, int T, int &C, int &F) +{ + memset(inq, 0, sizeof(inq)); + memset(dis, inf, sizeof(dis)); + int h = 0, t = 0; + que[t++] = S, inq[S] = true, curFlow[S] = inf, dis[S] = 0; + for (int x; h != t; inq[que[h++]] = false) + for (int i = head[x = que[h]]; ~i; i = next[i]) + if (cap[i] && dis[to[i]] > dis[x] + cost[i]) + { + dis[to[i]] = dis[x] + cost[i]; + curFlow[to[i]] = min(curFlow[x], cap[i]); + fa[to[i]] = i; + if (!inq[to[i]]) inq[que[t++] = to[i]] = true; + } + if (dis[T] == inf) return false; + for (int i = T; i != S; i = to[fa[i] ^ 1]) + cap[fa[i]] -= curFlow[T], cap[fa[i] ^ 1] += curFlow[T]; + C += dis[T] * curFlow[T], F += curFlow[T]; + return true; +} +int main() +{ + memset(head, -1, sizeof(head)); + int n, a, b, f, fA, fB; + scanf("%d%d%d%d%d%d", &n, &a, &b, &f, &fA, &fB); + int S = 0, T = n << 1 | 1; + for (int i = 1, x; i <= n; i++) + { + scanf("%d", &x); + addEdge(S, i, x, 0); + addEdge(i + n, T, x, 0); + addEdge(S, i + n, inf, f); + if (i + a + 1 <= n) addEdge(i, i + n + a + 1, inf, fA); + if (i + b + 1 <= n) addEdge(i, i + n + b + 1, inf, fB); + if (i + 1 <= n) addEdge(i, i + 1, inf, 0); + } + int F = 0, C = 0; + while (MCMF(S, T, C, F)); + printf("%d", C); + return 0; +} \ No newline at end of file diff --git a/OnlineJudges/lydsy/1927.cpp b/OnlineJudges/lydsy/1927.cpp new file mode 100644 index 0000000..3a5b574 --- /dev/null +++ b/OnlineJudges/lydsy/1927.cpp @@ -0,0 +1,69 @@ +#include +#include +inline int min(int a, int b) { return a < b ? a : b; } +const int inf = 0x3f3f3f3f, maxn = 4010, maxm = 50010; +int head[maxn], next[maxm], to[maxm], cap[maxm], cost[maxm], ecnt; +int que[maxn << 2], dis[maxn], curFlow[maxn], fa[maxn]; +bool inq[maxn]; +inline void addEdge_impl_(int u, int v, int ca, int co) +{ + next[ecnt] = head[u]; + head[u] = ecnt; + to[ecnt] = v; + cap[ecnt] = ca; + cost[ecnt] = co; + ecnt++; +} +inline void addEdge(int u, int v, int ca, int co) +{ + addEdge_impl_(u, v, ca, co); + addEdge_impl_(v, u, 0, -co); +} +bool MCMF(int S, int T, int &F, int &C) +{ + memset(inq, 0, sizeof(inq)); + memset(dis, inf, sizeof(dis)); + int h = 0, t = 0; + que[t++] = S; + inq[S] = true; + curFlow[S] = inf; + dis[S] = 0; + for (int x; h != t; inq[que[h++]] = false) + for (int i = head[x = que[h]]; ~i; i = next[i]) + if (cap[i] > 0 && dis[to[i]] > dis[x] + cost[i]) + { + dis[to[i]] = dis[x] + cost[i]; + curFlow[to[i]] = min(curFlow[x], cap[i]); + fa[to[i]] = i; + if (!inq[to[i]]) inq[que[t++] = to[i]] = true; + } + if (dis[T] == inf) return false; + F += curFlow[T], C += curFlow[T] * dis[T]; + for (int i = T; i != S; i = to[fa[i] ^ 1]) + cap[fa[i]] -= curFlow[T], cap[fa[i] ^ 1] += curFlow[T]; + return true; +} +int main() +{ + memset(head, -1, sizeof(head)); + int n, m; + scanf("%d%d", &n, &m); + int S = 0, T = n << 1 | 1; + for (int i = 1, x; i <= n; i++) + { + scanf("%d", &x); + addEdge(S, i, 1, 0); + addEdge(S, n + i, 1, x); + addEdge(n + i, T, 1, 0); + } + for (int i = 1, x, y, z, t; i <= m; i++) + { + scanf("%d%d%d", &x, &y, &z); + if (x > y) t = x, x = y, y = t; + addEdge(x, n + y, 1, z); + } + int C = 0, F = 0; + while (MCMF(S, T, F, C)); + printf("%d", C); + return 0; +} \ No newline at end of file diff --git a/OnlineJudges/lydsy/3996.cpp b/OnlineJudges/lydsy/3996.cpp new file mode 100644 index 0000000..f8b9db7 --- /dev/null +++ b/OnlineJudges/lydsy/3996.cpp @@ -0,0 +1,79 @@ +#include +#include +inline int min(int a, int b) { return a < b ? a : b; } +const int inf = 0x3f3f3f3f, maxn = 260000, maxm = maxn << 3, cnt = maxn - 1; +int head[maxn], next[maxm], to[maxm], cap[maxm], ecnt; +int que[maxn << 2], num[maxn], fa[maxn], dis[maxn], cur[maxn]; +inline void addEdge_impl_(int f, int t, int c) +{ + next[ecnt] = head[f]; + head[f] = ecnt; + to[ecnt] = t; + cap[ecnt] = c; + ecnt++; +} +inline void addEdge(int f, int t, int c) +{ + addEdge_impl_(f, t, c); + addEdge_impl_(t, f, 0); +} +int ISAP(int S, int T) +{ + int h = 0, t = 0, x, flow = 0; + for (int i = 0; i <= cnt; i++) dis[i] = cnt; + dis[que[t++] = T] = 0; + while (h != t) for (int i = head[x = que[h++]]; ~i; i = next[i]) + if (cap[i ^ 1] && dis[to[i]] > dis[x] + 1) + dis[que[t++] = to[i]] = dis[x] + 1; + memset(num, 0, sizeof(num)); + for (int i = 0; i <= cnt; i++) num[dis[i]]++, cur[i] = head[i]; + x = S; + while (dis[S] < cnt) + { + if (x == T) + { + int curFlow = inf; + for (x = T; x != S; x = to[fa[x] ^ 1]) curFlow = min(curFlow, cap[fa[x]]); + for (x = T; x != S; x = to[fa[x] ^ 1]) cap[fa[x]] -= curFlow, cap[fa[x] ^ 1] += curFlow; + flow += curFlow, x = S; + } + bool needRetreat = true; + for (int i = cur[x]; needRetreat && ~i; i = next[i]) + if (cap[i] && dis[to[i]] == dis[x] - 1) + needRetreat = false, cur[x] = i, fa[x = to[i]] = i; + if (needRetreat) + { + int mn = cnt - 1; + for (int i = head[x]; ~i; i = next[i]) + if (cap[i]) mn = min(mn, dis[to[i]]); + if (--num[dis[x]] == 0) break; + ++num[dis[x] = mn + 1]; + cur[x] = head[x]; + if (x != S) x = to[fa[x] ^ 1]; + } + } + return flow; +} +int main() +{ + memset(head, -1, sizeof(head)); + int n, S, T, idx, ans; + scanf("%d", &n); + S = 0, T = n + n * n + 1, idx = n, ans = 0; + for (int i = 1; i <= n; i++) + for (int j = 1, w; j <= n; j++) + { + scanf("%d", &w); + addEdge(S, ++idx, w); + addEdge(idx, i, inf); + addEdge(idx, j, inf); + ans += w; + } + for (int i = 1, w; i <= n; i++) + { + scanf("%d", &w); + addEdge(i, T, w); + } + printf("%d", ans - ISAP(S, T)); + return 0; +} \ No newline at end of file diff --git a/OnlineJudges/poj/1236.cpp b/OnlineJudges/poj/1236.cpp new file mode 100644 index 0000000..614cbff --- /dev/null +++ b/OnlineJudges/poj/1236.cpp @@ -0,0 +1,51 @@ +#include +inline int min(int a, int b) { return a < b ? a : b; } +const int maxn = 110, maxm = 10100; +int head[maxn], next[maxm], to[maxm], ecnt, f[maxn], g[maxn]; +inline void addEdge(int f, int t) +{ + ecnt++; + next[ecnt] = head[f]; + head[f] = ecnt; + to[ecnt] = t; +} +int dfn[maxn], low[maxn], stk[maxn], scc[maxn], scccnt, top, idx; +void tarjan(int x) +{ + dfn[x] = low[x] = ++idx; + stk[top++] = x; + for (int i = head[x]; i; i = next[i]) + if (!dfn[to[i]]) + tarjan(to[i]), low[x] = min(low[x], low[to[i]]); + else if (!scc[to[i]]) + low[x] = min(low[x], dfn[to[i]]); + if (dfn[x] == low[x]) + { + scccnt++; + do scc[stk[--top]] = scccnt; + while (stk[top] != x); + } +} +int main() +{ + int n; + scanf("%d", &n); + for (int i = 1, x; i <= n; i++) + for (scanf("%d", &x); x; scanf("%d", &x)) + addEdge(i, x); + for (int i = 1; i <= n; i++) + if (!dfn[i]) tarjan(i); + for (int i = 1; i <= n; i++) + for (int j = head[i]; j; j = next[j]) + if (scc[i] != scc[to[j]]) + f[scc[i]]++, g[scc[to[j]]]++; + int ans1 = 0, ans2 = 0; + if (scccnt == 1) printf("1\n0"); + else + { + for (int i = 1; i <= scccnt; i++) + ans1 += f[i] == 0, ans2 += g[i] == 0; + printf("%d\n%d", ans2, ans1 > ans2 ? ans1 : ans2); + } + return 0; +} \ No newline at end of file diff --git a/OnlineJudges/poj/2186.cpp b/OnlineJudges/poj/2186.cpp new file mode 100644 index 0000000..c613c9f --- /dev/null +++ b/OnlineJudges/poj/2186.cpp @@ -0,0 +1,63 @@ +#include +inline int min(int a, int b) { return a < b ? a : b; } +int head[10010], next[50010], to[50010], ecnt; +int dfn[10010], low[10010], stk[10010], scc[10010], top, idx, scccnt; +bool instk[10010]; +int deg[10010]; +inline void addEdge(int f, int t) +{ + ecnt++; + next[ecnt] = head[f]; + head[f] = ecnt; + to[ecnt] = t; +} +void tarjan(int x) +{ + dfn[x] = low[x] = ++idx; + instk[stk[top++] = x] = true; + for (int cur = head[x]; cur; cur = next[cur]) + if (!dfn[to[cur]]) tarjan(to[cur]), low[x] = min(low[x], low[to[cur]]); + else if (instk[to[cur]]) + low[x] = min(low[x], dfn[to[cur]]); + if (dfn[x] == low[x]) + { + scccnt++; + do + { + top--; + scc[stk[top]] = scccnt; + instk[stk[top]] = false; + } while (stk[top] != x); + } +} +int main() +{ + int n, m; + scanf("%d%d", &n, &m); + for (int i = 0, x, y; i < m; i++) + { + scanf("%d%d", &x, &y); + addEdge(x, y); + } + for (int i = 1; i <= n; i++) + if (!dfn[i]) + tarjan(i); + for (int i = 1; i <= n; i++) + for (int cur = head[i]; cur; cur = next[cur]) + if (scc[i] != scc[to[cur]]) + deg[scc[i]]++; + int zcnt = 0, id = 0; + for (int i = 1; i <= scccnt; i++) + if (deg[i] == 0) + zcnt++, id = i; + if (zcnt != 1) putchar('0'); + else + { + int ans = 0; + for (int i = 1; i <= n; i++) + if (scc[i] == id) + ans++; + printf("%d", ans); + } + return 0; +} \ No newline at end of file diff --git a/OnlineJudges/poj/2749.cpp b/OnlineJudges/poj/2749.cpp new file mode 100644 index 0000000..ba971d5 --- /dev/null +++ b/OnlineJudges/poj/2749.cpp @@ -0,0 +1,97 @@ +#include +#include +inline int abs(int x) { return x >= 0 ? x : -x; } +inline int min(int a, int b) { return a < b ? a : b; } +const int inf = 0x3f3f3f3f, maxn = 10010, maxm = 1200010; +int head[maxn], next[maxm], to[maxm], ecnt, n, A, B; +int dfn[maxn], low[maxn], stk[maxn], scc[maxn], top, idx, scccnt; +int sx1, sy1, sx2, sy2, sLen, X[maxn], Y[maxn], hate[maxn][2], like[maxn][2], d[maxn]; +inline void addEdge(int f, int t) +{ + next[ecnt] = head[f]; + head[f] = ecnt; + to[ecnt] = t; + ecnt++; +} +void tarjan(int x) +{ + dfn[x] = low[x] = ++idx; + stk[top++] = x; + for (int i = head[x]; ~i; i = next[i]) + if (!dfn[to[i]]) + tarjan(to[i]), low[x] = min(low[x], low[to[i]]); + else if (!scc[to[i]]) + low[x] = min(low[x], dfn[to[i]]); + if (dfn[x] == low[x]) + { + scccnt++; + do scc[stk[--top]] = scccnt; + while (stk[top] != x); + } +} +bool check(int x) +{ + memset(dfn, 0, sizeof(dfn)); + memset(low, 0, sizeof(low)); + memset(scc, 0, sizeof(scc)); + memset(head, -1, sizeof(head)); + ecnt = top = idx = scccnt = 0; + for (int i = 1; i <= n; i++) + for (int j = i + 1; j <= n; j++) + { + int l1 = d[i << 1], l2 = d[i << 1 | 1]; + int r1 = d[j << 1], r2 = d[j << 1 | 1]; + if (l1 + r1 > x) + addEdge(i << 1, j << 1 | 1), + addEdge(j << 1, i << 1 | 1); + if (l1 + r2 + sLen > x) + addEdge(i << 1, j << 1), + addEdge(j << 1 | 1, i << 1 | 1); + if (l2 + r1 + sLen > x) + addEdge(i << 1 | 1, j << 1 | 1), + addEdge(j << 1, i << 1); + if (l2 + r2 > x) + addEdge(i << 1 | 1, j << 1), + addEdge(j << 1 | 1, i << 1); + } + for (int i = 1, a, b; i <= A; i++) + { + a = hate[i][0], b = hate[i][1]; + addEdge(a << 1, b << 1 | 1); + addEdge(a << 1 | 1, b << 1); + addEdge(b << 1, a << 1 | 1); + addEdge(b << 1 | 1, a << 1); + } + for (int i = 1, a, b; i <= B; i++) + { + a = like[i][0], b = like[i][1]; + addEdge(a << 1, b << 1); + addEdge(a << 1 | 1, b << 1 | 1); + addEdge(b << 1, a << 1); + addEdge(b << 1 | 1, a << 1 | 1); + } + for (int i = 1; i <= (n << 1); i++) + if (!dfn[i]) tarjan(i); + for (int i = 1; i <= n; i++) + if (scc[i << 1] == scc[i << 1 | 1]) + return false; + return true; +} +int main() +{ + scanf("%d%d%d%d%d%d%d", &n, &A, &B, &sx1, &sy1, &sx2, &sy2); + sLen = abs(sx1 - sx2) + abs(sy1 - sy2); + for (int i = 1; i <= n; i++) + scanf("%d%d", X + i, Y + i); + for (int i = 1; i <= n; i++) + d[i << 1] = abs(X[i] - sx1) + abs(Y[i] - sy1), + d[i << 1 | 1] = abs(X[i] - sx2) + abs(Y[i] - sy2); + for (int i = 1; i <= A; i++) + scanf("%d%d", &hate[i][0], &hate[i][1]); + for (int i = 1; i <= B; i++) + scanf("%d%d", &like[i][0], &like[i][1]); + int l = 0, r = 8000000, m, ans = -1; + while (l <= r) check(m = (l + r) >> 1) ? r = (ans = m) - 1 : l = m + 1; + printf("%d\n", ans); + return 0; +} diff --git a/OnlineJudges/poj/3180.cpp b/OnlineJudges/poj/3180.cpp new file mode 100644 index 0000000..d678f03 --- /dev/null +++ b/OnlineJudges/poj/3180.cpp @@ -0,0 +1,44 @@ +#include +inline int min(int a, int b) { return a < b ? a : b; } +const int maxn = 100010; +int head[maxn], next[maxn << 1], to[maxn << 1], ecnt, n, m; +int dfn[maxn], scc[maxn], cnt[maxn], scccnt, stk[maxn], low[maxn], idx, top; +inline void addEdge(int f, int t) +{ + ecnt++; + next[ecnt] = head[f]; + head[f] = ecnt; + to[ecnt] = t; +} +void tarjan(int x) +{ + dfn[x] = low[x] = ++idx; + stk[top++] = x; + for (int i = head[x]; i; i = next[i]) + if (!dfn[to[i]]) + tarjan(to[i]), low[x] = min(low[x], low[to[i]]); + else if (!scc[to[i]]) + low[x] = min(low[x], dfn[to[i]]); + if (dfn[x] == low[x]) + { + scccnt++; + do scc[stk[--top]] = scccnt; + while (stk[top] != x); + } +} +int main() +{ + scanf("%d%d", &n, &m); + for (int i = 0, x, y; i < m; i++) + { + scanf("%d%d", &x, &y); + addEdge(x, y); + } + for (int i = 1; i <= n; i++) + if (!dfn[i]) tarjan(i); + int ans = 0; + for (int i = 1; i <= n; i++) cnt[scc[i]]++; + for (int i = 1; i <= scccnt; i++) if (cnt[i] > 1) ans++; + printf("%d", ans); + return 0; +} \ No newline at end of file diff --git a/OnlineJudges/poj/3648.cpp b/OnlineJudges/poj/3648.cpp new file mode 100644 index 0000000..a316c91 --- /dev/null +++ b/OnlineJudges/poj/3648.cpp @@ -0,0 +1,62 @@ +#include +#include +inline int min(int a, int b) { return a < b ? a : b; } +const int maxn = 2010, maxm = 500010; +int head[maxn], next[maxm], to[maxm], ecnt; +inline void addEdge(int f, int t) +{ + next[ecnt] = head[f]; + head[f] = ecnt; + to[ecnt] = t; + ecnt++; +} +int dfn[maxn], low[maxn], stk[maxn], scc[maxn], top, idx, scccnt; +void tarjan(int x) +{ + dfn[x] = low[x] = ++idx; + stk[top++] = x; + for (int i = head[x]; ~i; i = next[i]) + if (!dfn[to[i]]) + tarjan(to[i]), low[x] = min(low[x], low[to[i]]); + else if (!scc[to[i]]) + low[x] = min(low[x], dfn[to[i]]); + if (dfn[x] == low[x]) + { + scccnt++; + do scc[stk[--top]] = scccnt; + while (stk[top] != x); + } +} +int main() +{ + int m, n; + while (scanf("%d%d", &n, &m) && (m + n)) + { + memset(head, -1, sizeof(head)); + memset(dfn, 0, sizeof(dfn)); + memset(low, 0, sizeof(low)); + memset(scc, 0, sizeof(scc)); + idx = top = ecnt = scccnt = 0; + int a1, a2; + char c1, c2; + for (int i = 0; i < m; i++) + { + scanf("%d%c %d%c", &a1, &c1, &a2, &c2); + a1 = a1 << 1 | (c1 == 'h'), a2 = a2 << 1 | (c2 == 'h'); + addEdge(a1, a2 ^ 1), addEdge(a2, a1 ^ 1); + } + addEdge(0, 1); + for (int i = 0; i < (n << 1); i++) + if (!dfn[i]) tarjan(i); + bool flag = true; + for (int i = 0; i < n && flag; i++) + if (scc[i << 1] == scc[i << 1 | 1]) + flag = false; + if (!flag) puts("bad luck"); + else + if (n < 1) putchar('\n'); + else for (int i = 1; i < n; i++) + printf("%d%c%c", i, (scc[i << 1] > scc[i << 1 | 1]) ? 'w' : 'h', " \n"[i == n - 1]); + } + return 0; +} diff --git a/OnlineJudges/poj/3678.cpp b/OnlineJudges/poj/3678.cpp new file mode 100644 index 0000000..385ea54 --- /dev/null +++ b/OnlineJudges/poj/3678.cpp @@ -0,0 +1,87 @@ +#include +#include +inline int min(int a, int b) { return a < b ? a : b; } +const int maxn = 10010, maxm = 4000010; +int head[maxn], next[maxm], to[maxm], ecnt; +inline void addEdge(int f, int t) +{ + next[ecnt] = head[f]; + head[f] = ecnt; + to[ecnt] = t; + ecnt++; +} +int dfn[maxn], low[maxn], stk[maxn], scc[maxn], top, idx, scccnt; +void tarjan(int x) +{ + dfn[x] = low[x] = ++idx; + stk[top++] = x; + for (int i = head[x]; ~i; i = next[i]) + if (!dfn[to[i]]) + tarjan(to[i]), low[x] = min(low[x], low[to[i]]); + else if (!scc[to[i]]) + low[x] = min(low[x], dfn[to[i]]); + if (dfn[x] == low[x]) + { + scccnt++; + do scc[stk[--top]] = scccnt; + while (stk[top] != x); + } +} +int main() +{ + int n, m; + while (~scanf("%d%d", &n, &m)) + { + memset(dfn, 0, sizeof(dfn)); + memset(low, 0, sizeof(low)); + memset(scc, 0, sizeof(scc)); + memset(head, -1, sizeof(head)); + ecnt = top = idx = scccnt = 0; + for (int i = 0, u, v, w; i < m; ++i) + { + char op[5]; + scanf("%d%d%d%s", &u, &v, &w, op); + if (op[0] == 'A') + if (w) + { + addEdge(u << 1, v << 1 | 1), addEdge(v << 1, u << 1 | 1); + addEdge(u << 1, v << 1), addEdge(v << 1 | 1, u << 1 | 1); + addEdge(u << 1 | 1, v << 1 | 1), addEdge(v << 1, u << 1); + } + else + { + addEdge(u << 1 | 1, v << 1), addEdge(v << 1 | 1, u << 1); + } + if (op[0] == 'O') + if (w) + { + addEdge(u << 1, v << 1 | 1), addEdge(v << 1, u << 1 | 1); + } + else + { + addEdge(u << 1, v << 1), addEdge(v << 1 | 1, u << 1 | 1); + addEdge(u << 1 | 1, v << 1 | 1), addEdge(v << 1, u << 1); + addEdge(u << 1 | 1, v << 1), addEdge(v << 1 | 1, u << 1); + } + if (op[0] == 'X') + if (w) + { + addEdge(u << 1, v << 1 | 1), addEdge(v << 1, u << 1 | 1); + addEdge(u << 1 | 1, v << 1), addEdge(v << 1 | 1, u << 1); + } + else + { + addEdge(u << 1, v << 1), addEdge(v << 1 | 1, u << 1 | 1); + addEdge(u << 1 | 1, v << 1 | 1), addEdge(v << 1, u << 1); + } + } + for (int i = 0; i < (n << 1); i++) + if (!dfn[i]) tarjan(i); + bool flag = true; + for (int i = 0; i < n && flag; i++) + if (scc[i << 1] == scc[i << 1 | 1]) + flag = false; + puts(flag ? "YES" : "NO"); + } + return 0; +} \ No newline at end of file