Submission #2560893
Source Code Expand
#include <bits/stdc++.h> using namespace std; const int MAXN = 100000 + 10; struct Edge { int v, n; } e[MAXN << 1]; int G[MAXN], edgeCnt; void _add_edge(int u, int v) { e[++edgeCnt].n = G[u]; G[u] = edgeCnt; e[edgeCnt].v = v; } void add_edge(int u, int v) { _add_edge(u, v); _add_edge(v, u); } int n, m; int f[MAXN]; int g[MAXN]; int h[MAXN]; int size[MAXN]; bool vis[MAXN]; int foo, bar; void dfs1(int u, int fa) { size[u] = f[u] ? -1 : 1; size[u] += f[u] ? -h[u] : h[u]; for (int i = G[u]; i; i = e[i].n) if (e[i].v != fa) { if (u == bar && e[i].v == foo || u == foo && e[i].v == bar) { continue; } f[e[i].v] = f[u] ^ 1; dfs1(e[i].v, u); size[u] += size[e[i].v]; g[u] += g[e[i].v]; } } bool dfs2(int u, int fa) { if (vis[u]) return false; vis[u] = true; for (int i = G[u]; i; i = e[i].n) if (e[i].v != fa) { if (!dfs2(e[i].v, u)) foo = u, bar = e[i].v; } return true; } int main() { // freopen("sapphire.in", "r", stdin); // freopen("sapphire.out", "w", stdout); ios::sync_with_stdio(0); cin >> n >> m; for (int i = 1; i <= m; ++i) { int u, v; cin >> u >> v; add_edge(u, v); } if (m == n - 1) { dfs1(1, -1); long long ans = 0; for (int i = 1; i <= n; ++i) ans += abs(size[i]); cout << (size[1] == 0 ? ans : -1) << endl; } else { dfs2(1, 0); g[foo] = 1; g[bar] = -1; dfs1(1, 0); long long ans = 0; if (f[foo] == f[bar]) { if (abs(size[1]) & 1) cout << "-1\n"; else { ans += abs(size[1]) / 2; h[foo] = h[bar] = ans; for (int i = 1; i <= n; ++i) ans += abs(size[i]); dfs1(1, 0); cout << ans << endl; } } else { if (size[1] != 0) cout << "-1\n"; else { static int a[MAXN], cnt = 0; for (int i = 1; i <= n; ++i) { if (g[i] == 0) { ans += abs(size[i]); } else { a[++cnt] = -g[i] * size[i]; } } nth_element(a + 1, a + (1 + cnt) / 2, a + cnt + 1); int temp = a[(1 + cnt) / 2]; for (int i = 1; i <= n; ++i) if (g[i] != 0) { ans += abs(size[i] + g[i] * temp); } ans += (f[foo] ^ 1 ? -1 : 1) * temp; cout << ans << endl; } } } }
Submission Info
Submission Time | |
---|---|
Task | F - Namori |
User | QQ_kotori |
Language | C++14 (GCC 5.4.1) |
Score | 1500 |
Code Size | 2951 Byte |
Status | WA |
Exec Time | 38 ms |
Memory | 8576 KB |
Judge Result
Set Name | Sample | Subtask1 | All | ||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 1500 / 1500 | 0 / 700 | ||||||||
Status |
|
|
|
Set Name | Test Cases |
---|---|
Sample | 0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt |
Subtask1 | 0_00.txt, 0_01.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt |
All | 0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 2_00.txt, 2_01.txt, 2_02.txt, 2_03.txt, 2_04.txt, 2_05.txt, 2_06.txt, 2_07.txt, 2_08.txt, 2_09.txt, 2_10.txt, 2_11.txt, 2_12.txt, 2_13.txt, 2_14.txt, 2_15.txt, 2_16.txt, 2_17.txt, 2_18.txt, 2_19.txt, 2_20.txt, 2_21.txt, 2_22.txt, 2_23.txt, 2_24.txt, 2_25.txt, 2_26.txt, 2_27.txt, 2_28.txt, 2_29.txt, 2_30.txt, 2_31.txt, 2_32.txt, 2_33.txt, 2_34.txt, 2_35.txt, 2_36.txt, 2_37.txt, 2_38.txt, 2_39.txt, 2_40.txt, 2_41.txt, 2_42.txt, 2_43.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
0_00.txt | AC | 1 ms | 256 KB |
0_01.txt | AC | 1 ms | 256 KB |
0_02.txt | AC | 1 ms | 256 KB |
0_03.txt | AC | 1 ms | 256 KB |
1_00.txt | AC | 1 ms | 256 KB |
1_01.txt | AC | 28 ms | 7936 KB |
1_02.txt | AC | 32 ms | 4736 KB |
1_03.txt | AC | 20 ms | 3072 KB |
1_04.txt | AC | 26 ms | 5504 KB |
1_05.txt | AC | 26 ms | 5248 KB |
1_06.txt | AC | 26 ms | 5376 KB |
1_07.txt | AC | 26 ms | 4736 KB |
1_08.txt | AC | 27 ms | 5632 KB |
1_09.txt | AC | 23 ms | 3456 KB |
1_10.txt | AC | 23 ms | 3456 KB |
1_11.txt | AC | 24 ms | 3456 KB |
1_12.txt | AC | 24 ms | 3456 KB |
1_13.txt | AC | 25 ms | 3840 KB |
1_14.txt | AC | 24 ms | 3456 KB |
1_15.txt | AC | 24 ms | 3456 KB |
1_16.txt | AC | 24 ms | 3456 KB |
1_17.txt | AC | 24 ms | 3456 KB |
2_00.txt | AC | 1 ms | 256 KB |
2_01.txt | AC | 34 ms | 8576 KB |
2_02.txt | WA | 28 ms | 6016 KB |
2_03.txt | WA | 21 ms | 3072 KB |
2_04.txt | WA | 31 ms | 6016 KB |
2_05.txt | WA | 32 ms | 6016 KB |
2_06.txt | AC | 32 ms | 6016 KB |
2_07.txt | AC | 32 ms | 6016 KB |
2_08.txt | AC | 32 ms | 6016 KB |
2_09.txt | AC | 25 ms | 3456 KB |
2_10.txt | WA | 26 ms | 3456 KB |
2_11.txt | WA | 27 ms | 3456 KB |
2_12.txt | AC | 28 ms | 3456 KB |
2_13.txt | AC | 30 ms | 5632 KB |
2_14.txt | AC | 28 ms | 3712 KB |
2_15.txt | AC | 28 ms | 3584 KB |
2_16.txt | AC | 28 ms | 3456 KB |
2_17.txt | AC | 28 ms | 3456 KB |
2_18.txt | AC | 1 ms | 256 KB |
2_19.txt | WA | 31 ms | 5888 KB |
2_20.txt | WA | 21 ms | 3072 KB |
2_21.txt | AC | 33 ms | 8576 KB |
2_22.txt | WA | 35 ms | 5888 KB |
2_23.txt | WA | 35 ms | 5888 KB |
2_24.txt | WA | 38 ms | 5888 KB |
2_25.txt | WA | 34 ms | 5888 KB |
2_26.txt | WA | 36 ms | 5888 KB |
2_27.txt | WA | 30 ms | 3456 KB |
2_28.txt | WA | 31 ms | 3456 KB |
2_29.txt | WA | 31 ms | 3456 KB |
2_30.txt | AC | 33 ms | 3456 KB |
2_31.txt | AC | 30 ms | 4992 KB |
2_32.txt | WA | 33 ms | 3712 KB |
2_33.txt | WA | 33 ms | 3456 KB |
2_34.txt | AC | 28 ms | 3456 KB |
2_35.txt | AC | 33 ms | 3456 KB |
2_36.txt | AC | 22 ms | 3072 KB |
2_37.txt | WA | 21 ms | 3072 KB |
2_38.txt | AC | 22 ms | 3200 KB |
2_39.txt | WA | 21 ms | 3072 KB |
2_40.txt | AC | 21 ms | 3072 KB |
2_41.txt | WA | 21 ms | 3072 KB |
2_42.txt | AC | 21 ms | 3072 KB |
2_43.txt | WA | 21 ms | 3072 KB |