Submission #868498
Source Code Expand
/**
* author: [itmo] enot.1.10
* created: 04.09.2016 15:53:18
**/
#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define forn(i, n) for(int i = 0 ; (i) < (n) ; ++i)
#define eprintf(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(),a.end()
#define pw(x) (1LL<<(x))
using namespace std;
typedef long long ll;
typedef double dbl;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = 1.01e9;
const dbl eps = 1e-9;
/* --- main part --- */
int n, m;
namespace task1
{
const int N = 1e5 + 10;
int mark[N];
vi v[N];
int d[N][2];
ll res = 0;
void dfs(int x)
{
mark[x] = 1;
d[x][0] = 1;
d[x][1] = 0;
for (int y : v[x])
{
if (mark[y] == 1) continue;
dfs(y);
d[x][0] += d[y][1];
d[x][1] += d[y][0];
}
res += abs(d[x][0] - d[x][1]);
}
void run()
{
forn(i, n - 1)
{
int x, y;
scanf("%d%d", &x, &y);
--x, --y;
v[x].pb(y);
v[y].pb(x);
}
dfs(0);
if (d[0][0] != d[0][1]) res = -1;
printf("%lld\n", res);
exit(0);
}
}
const int N = 1e5 + 10;
vi v[N];
vi cycle;
int cyc[N];
int mark[N];
int st[N], stc = 0;
int found = 0;
void dfs(int x, int pr = -1)
{
mark[x] = 1;
st[stc++] = x;
for (int y : v[x])
{
if (y == pr) continue;
if (mark[y] == 1)
{
if (found) continue;
found = 1;
for (int i = stc - 1; i >= 0; --i)
{
int z = st[i];
cycle.pb(z);
cyc[z] = 1;
if (z == y) break;
}
}
else
{
dfs(y, x);
}
}
stc--;
}
ll res = 0;
int d[N][2];
void calc(int x)
{
mark[x] = 1;
d[x][0] = 1;
d[x][1] = 0;
for (int y : v[x])
{
if (!cyc[y] && mark[y] == 0)
{
calc(y);
d[x][0] += d[y][1];
d[x][1] += d[y][0];
}
}
if (!cyc[x]) res += abs(d[x][0] - d[x][1]);
}
int cnt[N];
ll f(int x, vi &v1, vi &v2)
{
ll r = 0;
forn(i, sz(v1)) r += abs(v1[i] - x);
forn(i, sz(v2)) r += abs(v2[i] + x);
return r;
}
int main()
{
#ifdef home
assert(freopen("1.in", "r", stdin));
assert(freopen("1.out", "w", stdout));
#endif
scanf("%d%d", &n, &m);
if (n == m + 1)
{
task1::run();
return 0;
}
forn(i, m)
{
int x, y;
scanf("%d%d", &x, &y);
--x, --y;
v[x].pb(y);
v[y].pb(x);
}
dfs(0);
memset(mark, 0, sizeof mark);
ll val = 0;
forn(i, n) if (cyc[i])
{
calc(i);
val += abs(d[i][0] - d[i][1]);
}
int cc = sz(cycle);
/*forn(i, sz(cycle))
{
eprintf("%d%c", cycle[i], " \n"[i + 1 == sz(cycle)]);
} */
forn(i, cc)
{
cnt[i] = d[cycle[i]][0] - d[cycle[i]][1];
}
int ok = val % 2 == 0;
if (!ok) return 0 * printf("-1\n");
int a = 1, b = 0;
for (int i = 1; i < cc; ++i)
{
int a2 = -a;
int b2 = cnt[i] - b;
a = a2, b = b2;
}
if (a == 1)
{
assert((cnt[0] - b) % 2 == 0);
int x = (cnt[0] - b) / 2;
res += abs(x);
for (int i = 1; i < cc; ++i)
{
x = cnt[i] - x;
res += abs(x);
}
printf("%lld\n", res);
}
else
{
if (b != cnt[0]) return 0 * printf("-1\n");
vi v1, v2;
int x = 0;
v1.pb(x);
for (int i = 1; i < cc; ++i)
{
x = cnt[i] - x;
if (i % 2 == 0) v1.pb(x);
else v2.pb(x);
}
int L = -1e8;
int R = 1e8;
while (R - L > 10)
{
int m1 = (2 * L + R) / 3;
int m2 = (L + 2 * R) / 3;
ll f1 = f(m1, v1, v2);
ll f2 = f(m2, v1, v2);
if (f1 < f2) R = m2;
else L = m1;
}
ll ans = 1e18;
for (int i = L; i <= R; ++i) ans = min(ans, f(i, v1, v2));
res += ans;
printf("%lld\n", res);
}
#ifdef home
eprintf("time = %d ms\n", (int)(clock() * 1000. / CLOCKS_PER_SEC));
#endif
return 0;
}
Submission Info
Submission Time |
|
Task |
F - Namori |
User |
enot |
Language |
C++14 (GCC 5.4.1) |
Score |
2200 |
Code Size |
4610 Byte |
Status |
AC |
Exec Time |
123 ms |
Memory |
19320 KB |
Compile Error
./Main.cpp: In function ‘void task1::run()’:
./Main.cpp:62:30: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &x, &y);
^
./Main.cpp: In function ‘int main()’:
./Main.cpp:148:26: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &m);
^
./Main.cpp:157:30: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &x, &y);
^
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 |
11 ms |
4992 KB |
0_01.txt |
AC |
11 ms |
4992 KB |
0_02.txt |
AC |
11 ms |
5376 KB |
0_03.txt |
AC |
11 ms |
5376 KB |
1_00.txt |
AC |
11 ms |
4992 KB |
1_01.txt |
AC |
91 ms |
13696 KB |
1_02.txt |
AC |
75 ms |
10752 KB |
1_03.txt |
AC |
63 ms |
9592 KB |
1_04.txt |
AC |
83 ms |
11392 KB |
1_05.txt |
AC |
85 ms |
11136 KB |
1_06.txt |
AC |
86 ms |
11136 KB |
1_07.txt |
AC |
82 ms |
10752 KB |
1_08.txt |
AC |
85 ms |
11520 KB |
1_09.txt |
AC |
74 ms |
9600 KB |
1_10.txt |
AC |
77 ms |
9472 KB |
1_11.txt |
AC |
79 ms |
9344 KB |
1_12.txt |
AC |
82 ms |
9344 KB |
1_13.txt |
AC |
81 ms |
9728 KB |
1_14.txt |
AC |
85 ms |
9344 KB |
1_15.txt |
AC |
81 ms |
9344 KB |
1_16.txt |
AC |
82 ms |
9344 KB |
1_17.txt |
AC |
84 ms |
9344 KB |
2_00.txt |
AC |
12 ms |
5376 KB |
2_01.txt |
AC |
123 ms |
19320 KB |
2_02.txt |
AC |
95 ms |
14592 KB |
2_03.txt |
AC |
65 ms |
9720 KB |
2_04.txt |
AC |
102 ms |
14588 KB |
2_05.txt |
AC |
105 ms |
14464 KB |
2_06.txt |
AC |
104 ms |
14460 KB |
2_07.txt |
AC |
102 ms |
14588 KB |
2_08.txt |
AC |
103 ms |
14460 KB |
2_09.txt |
AC |
81 ms |
9600 KB |
2_10.txt |
AC |
86 ms |
9472 KB |
2_11.txt |
AC |
86 ms |
9344 KB |
2_12.txt |
AC |
89 ms |
9344 KB |
2_13.txt |
AC |
96 ms |
13820 KB |
2_14.txt |
AC |
91 ms |
10112 KB |
2_15.txt |
AC |
90 ms |
9728 KB |
2_16.txt |
AC |
89 ms |
9472 KB |
2_17.txt |
AC |
89 ms |
9344 KB |
2_18.txt |
AC |
11 ms |
5376 KB |
2_19.txt |
AC |
85 ms |
14588 KB |
2_20.txt |
AC |
65 ms |
9592 KB |
2_21.txt |
AC |
122 ms |
19320 KB |
2_22.txt |
AC |
93 ms |
14464 KB |
2_23.txt |
AC |
110 ms |
14332 KB |
2_24.txt |
AC |
98 ms |
14204 KB |
2_25.txt |
AC |
93 ms |
14588 KB |
2_26.txt |
AC |
97 ms |
14336 KB |
2_27.txt |
AC |
80 ms |
9600 KB |
2_28.txt |
AC |
86 ms |
9472 KB |
2_29.txt |
AC |
85 ms |
9472 KB |
2_30.txt |
AC |
92 ms |
9344 KB |
2_31.txt |
AC |
97 ms |
12796 KB |
2_32.txt |
AC |
90 ms |
9984 KB |
2_33.txt |
AC |
96 ms |
9728 KB |
2_34.txt |
AC |
90 ms |
9472 KB |
2_35.txt |
AC |
90 ms |
9344 KB |
2_36.txt |
AC |
67 ms |
9720 KB |
2_37.txt |
AC |
66 ms |
9592 KB |
2_38.txt |
AC |
67 ms |
9592 KB |
2_39.txt |
AC |
67 ms |
9592 KB |
2_40.txt |
AC |
67 ms |
9720 KB |
2_41.txt |
AC |
68 ms |
9720 KB |
2_42.txt |
AC |
65 ms |
9720 KB |
2_43.txt |
AC |
65 ms |
9592 KB |