Submission #1136012
Source Code Expand
#ifdef DEBUG #define _GLIBCXX_DEBUG #endif #include <bits/stdc++.h> using namespace std; mt19937 mrand(random_device{} ()); int rnd(int x) { return mrand() % x; } typedef long double ld; typedef long long ll; #ifdef DEBUG #define eprintf(...) fprintf(stderr, __VA_ARGS__), fflush(stderr) #else #define eprintf(...) ; #endif #define pb push_back #define mp make_pair #define sz(x) ((int) (x).size()) #define TASK "text" const int inf = (int) 1.01e9; const ld eps = 1e-9; const ld pi = acos((ld) -1.0); const int mod = (int) 1e9 + 7; void add(int &x, int y) { if ((x += y) >= mod) { x -= mod; } } int mult(int x, int y) { return (long long) x * y % mod; } int myPower(int x, int pw) { int res = 1; for (; pw; pw >>= 1) { if (pw & 1) { res = mult(res, x); } x = mult(x, x); } return res; } void precalc() { } const int maxn = (int) 1e5 + 10; int n, m; vector<vector<int> > es; int read() { if (scanf("%d%d", &n, &m) < 2) { return 0; } es = vector<vector<int> >(n); for (int i = 0; i < m; ++i) { int s, t; scanf("%d%d", &s, &t); --s, --t; es[s].pb(t), es[t].pb(s); } return 1; } int used[maxn]; vector<int> cycle; vector<int> st; void dfs0(int v, int p) { used[v] = 1; st.pb(v); for (int u : es[v]) { if (used[u]) { if (u != p && used[u] == 1) { int x = find(st.begin(), st.end(), u) - st.begin(); cycle.clear(); for (int i = x; i < sz(st); ++i) { cycle.pb(st[i]); } } continue; } dfs0(u, v); } st.pop_back(); used[v] = 2; } long long res; int dfs(int v, int p) { int sum = 1; for (int u : es[v]) { if (u == p || used[u]) { continue; } int got = dfs(u, v); res += abs(got); sum -= got; } return sum; } void solve() { for (int i = 0; i < n; ++i) { used[i] = 0; } cycle.clear(); if (m != n - 1) { st.clear(); dfs0(0, -1); assert(sz(cycle)); } else { cycle.pb(0); } res = 0; for (int i = 0; i < n; ++i) { used[i] = 0; } for (int i = 0; i < sz(cycle); ++i) { used[cycle[i]] = 1; } vector<int> a; for (int i = 0; i < sz(cycle); ++i) { a.pb(dfs(cycle[i], -1)); if (i & 1) { a.back() *= -1; } } int sum = accumulate(a.begin(), a.end(), 0); if (sz(a) == 1 || !(sz(a) & 1)) { if (sum) { printf("-1\n"); return; } /*for (int i = 0; i < sz(a); ++i) { eprintf("%d%c", a[i], " \n"[i == sz(a) - 1]); }*/ vector<int> ps; int sum = 0; for (int i = 0; i < sz(a); ++i) { sum += a[i]; ps.pb(sum); } sort(ps.begin(), ps.end()); int x = ps[sz(ps) / 2]; for (int i = 0; i < sz(ps); ++i) { res += abs(ps[i] - x); } } else { if (1 & sum) { printf("-1\n"); return; } a[0] -= sum / 2; a.back() -= sum / 2; res += abs(sum) / 2; int cur = 0; for (int i = 0; i < sz(a); ++i) { res += abs(cur); cur += a[i]; } assert(!cur); } printf("%lld\n", res); } int main() { precalc(); #ifdef LOCAL freopen(TASK ".out", "w", stdout); assert(freopen(TASK ".in", "r", stdin)); #endif while (1) { if (!read()) { break; } solve(); #ifdef DEBUG eprintf("Time %.2f\n", (double) clock() / CLOCKS_PER_SEC); #endif } return 0; }
Submission Info
Submission Time | |
---|---|
Task | F - Namori |
User | XraY |
Language | C++14 (GCC 5.4.1) |
Score | 2200 |
Code Size | 3561 Byte |
Status | AC |
Exec Time | 50 ms |
Memory | 14328 KB |
Compile Error
./Main.cpp: In function ‘int read()’: ./Main.cpp:72:26: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] scanf("%d%d", &s, &t); ^
Judge Result
Set Name | Sample | Subtask1 | All | ||||||
---|---|---|---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 1500 / 1500 | 700 / 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 | 42 ms | 10624 KB |
1_02.txt | AC | 35 ms | 7552 KB |
1_03.txt | AC | 29 ms | 6520 KB |
1_04.txt | AC | 37 ms | 8320 KB |
1_05.txt | AC | 40 ms | 8064 KB |
1_06.txt | AC | 40 ms | 8064 KB |
1_07.txt | AC | 39 ms | 7552 KB |
1_08.txt | AC | 40 ms | 8448 KB |
1_09.txt | AC | 35 ms | 6400 KB |
1_10.txt | AC | 37 ms | 6272 KB |
1_11.txt | AC | 36 ms | 6272 KB |
1_12.txt | AC | 39 ms | 6144 KB |
1_13.txt | AC | 39 ms | 6528 KB |
1_14.txt | AC | 39 ms | 6144 KB |
1_15.txt | AC | 39 ms | 6144 KB |
1_16.txt | AC | 39 ms | 6144 KB |
1_17.txt | AC | 39 ms | 6144 KB |
2_00.txt | AC | 1 ms | 256 KB |
2_01.txt | AC | 50 ms | 14328 KB |
2_02.txt | AC | 42 ms | 10364 KB |
2_03.txt | AC | 31 ms | 6520 KB |
2_04.txt | AC | 47 ms | 10364 KB |
2_05.txt | AC | 49 ms | 10236 KB |
2_06.txt | AC | 48 ms | 10236 KB |
2_07.txt | AC | 44 ms | 10364 KB |
2_08.txt | AC | 48 ms | 10236 KB |
2_09.txt | AC | 39 ms | 6400 KB |
2_10.txt | AC | 45 ms | 6272 KB |
2_11.txt | AC | 43 ms | 6272 KB |
2_12.txt | AC | 45 ms | 6144 KB |
2_13.txt | AC | 44 ms | 9596 KB |
2_14.txt | AC | 45 ms | 6528 KB |
2_15.txt | AC | 45 ms | 6144 KB |
2_16.txt | AC | 45 ms | 6144 KB |
2_17.txt | AC | 45 ms | 6144 KB |
2_18.txt | AC | 1 ms | 256 KB |
2_19.txt | AC | 42 ms | 10236 KB |
2_20.txt | AC | 30 ms | 6520 KB |
2_21.txt | AC | 50 ms | 14328 KB |
2_22.txt | AC | 45 ms | 10236 KB |
2_23.txt | AC | 42 ms | 10108 KB |
2_24.txt | AC | 49 ms | 9980 KB |
2_25.txt | AC | 43 ms | 10236 KB |
2_26.txt | AC | 43 ms | 10108 KB |
2_27.txt | AC | 39 ms | 6528 KB |
2_28.txt | AC | 39 ms | 6272 KB |
2_29.txt | AC | 42 ms | 6272 KB |
2_30.txt | AC | 45 ms | 6144 KB |
2_31.txt | AC | 47 ms | 8828 KB |
2_32.txt | AC | 42 ms | 6400 KB |
2_33.txt | AC | 45 ms | 6144 KB |
2_34.txt | AC | 45 ms | 6144 KB |
2_35.txt | AC | 45 ms | 6144 KB |
2_36.txt | AC | 30 ms | 6520 KB |
2_37.txt | AC | 30 ms | 6520 KB |
2_38.txt | AC | 30 ms | 6520 KB |
2_39.txt | AC | 30 ms | 6520 KB |
2_40.txt | AC | 29 ms | 6520 KB |
2_41.txt | AC | 30 ms | 6520 KB |
2_42.txt | AC | 30 ms | 6520 KB |
2_43.txt | AC | 29 ms | 6520 KB |