Submission #1691938
Source Code Expand
#include <bits/stdc++.h>
#define For(i, j, k) for(int i = j; i <= k; i++)
#define Forr(i, j, k) for(int i = j; i >= k; i--)
#define pb push_back
using namespace std;
const int N = 100010;
int n, m, col[N];
vector<int> G[N];
int banu, banv;
bool DFS_bipartite(int o){
for(int v : G[o]){
if((o == banu && v == banv) || (o == banv && v == banu)) continue;
if(!col[v]){
col[v] = 3 - col[o];
if(!DFS_bipartite(v)) return false;
}
else if(col[o] == col[v]) return false;
}
return true;
}
bool vis[N];
int stk[N], top;
int cycle[N], c;
void DFS_cycle(int o, int f){
vis[o] = true;
stk[++top] = o;
for(int v : G[o]){
if(v == f) continue;
if(!vis[v]) DFS_cycle(v, o);
else if(!c){
Forr(i, top, 1){
cycle[++c] = stk[i];
if(stk[i] == v) break;
}
}
}
--top;
}
int szx[N], szy[N];
int DFS_work(int o, int f){
szx[o] += col[o] == 1;
szy[o] += col[o] == 2;
int ret = 0;
for(int v : G[o]){
if((o == banu && v == banv) || (o == banv && v == banu)) continue;
if(v == f) continue;
ret += DFS_work(v, o);
szx[o] += szx[v], szy[o] += szy[v];
}
return ret + abs(szx[o] - szy[o]);
}
int calc(int x){
memset(szx, 0, sizeof szx), memset(szy, 0, sizeof szy);
szx[banu] += x, szx[banv] -= x;
return DFS_work(1, 0) + abs(x);
}
int main(){
scanf("%d%d", &n, &m);
For(i, 1, m){
int u, v;
scanf("%d%d", &u, &v);
G[u].pb(v), G[v].pb(u);
}
col[1] = 1;
bool bipart = DFS_bipartite(1);
DFS_cycle(1, 0);
// cerr << "cycle length = " << c << endl
// << "bipart = " << bipart << endl;
if(!c){
int ans = DFS_work(1, 0);
if(szx[1] != szy[1]) puts("-1");
else printf("%d\n", ans);
}
else if(bipart){
banu = cycle[1], banv = cycle[2];
DFS_work(1, 0);
if(szx[1] != szy[1]){
puts("-1");
return 0;
}
int L = -n, R = n;
while(L < R){
int m1 = L + (R - L) / 3, m2 = R - (R - L) / 3;
if(calc(m1) > calc(m2)) L = m1 + 1;
else R = m2 - 1;
}
printf("%d\n", calc(L));
}
else{
banu = cycle[1], banv = cycle[2];
memset(col, 0, sizeof col);
col[1] = 1;
assert(DFS_bipartite(1));
DFS_work(1, 0);
int d = szx[1] - szy[1];
if(d % 2){
puts("-1");
return 0;
}
memset(szx, 0, sizeof szx), memset(szy, 0, sizeof szy);
szy[banu] += d / 2, szy[banv] += d / 2;
printf("%d\n", DFS_work(1, 0) + abs(d / 2));
}
return 0;
}
Submission Info
Submission Time |
|
Task |
F - Namori |
User |
dy0607 |
Language |
C++14 (GCC 5.4.1) |
Score |
1500 |
Code Size |
2453 Byte |
Status |
WA |
Exec Time |
366 ms |
Memory |
15616 KB |
Compile Error
./Main.cpp: In function ‘int main()’:
./Main.cpp:70:23: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &m);
^
./Main.cpp:73:24: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &u, &v);
^
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 |
2 ms |
2688 KB |
0_01.txt |
AC |
2 ms |
2688 KB |
0_02.txt |
AC |
3 ms |
3456 KB |
0_03.txt |
AC |
2 ms |
3712 KB |
1_00.txt |
AC |
2 ms |
2688 KB |
1_01.txt |
AC |
51 ms |
14848 KB |
1_02.txt |
AC |
40 ms |
9472 KB |
1_03.txt |
AC |
30 ms |
7416 KB |
1_04.txt |
AC |
46 ms |
10752 KB |
1_05.txt |
AC |
47 ms |
10368 KB |
1_06.txt |
AC |
48 ms |
10368 KB |
1_07.txt |
AC |
46 ms |
9472 KB |
1_08.txt |
AC |
47 ms |
11008 KB |
1_09.txt |
AC |
38 ms |
7296 KB |
1_10.txt |
AC |
41 ms |
7168 KB |
1_11.txt |
AC |
43 ms |
7168 KB |
1_12.txt |
AC |
45 ms |
7040 KB |
1_13.txt |
AC |
46 ms |
7680 KB |
1_14.txt |
AC |
46 ms |
7168 KB |
1_15.txt |
AC |
46 ms |
7040 KB |
1_16.txt |
AC |
46 ms |
7040 KB |
1_17.txt |
AC |
46 ms |
7040 KB |
2_00.txt |
AC |
3 ms |
3456 KB |
2_01.txt |
WA |
366 ms |
15616 KB |
2_02.txt |
WA |
248 ms |
11520 KB |
2_03.txt |
AC |
124 ms |
7416 KB |
2_04.txt |
WA |
291 ms |
11392 KB |
2_05.txt |
WA |
314 ms |
11392 KB |
2_06.txt |
WA |
330 ms |
11264 KB |
2_07.txt |
WA |
300 ms |
11392 KB |
2_08.txt |
WA |
322 ms |
11264 KB |
2_09.txt |
AC |
194 ms |
7296 KB |
2_10.txt |
AC |
237 ms |
7168 KB |
2_11.txt |
AC |
274 ms |
7168 KB |
2_12.txt |
AC |
300 ms |
7040 KB |
2_13.txt |
AC |
48 ms |
10880 KB |
2_14.txt |
AC |
46 ms |
7424 KB |
2_15.txt |
AC |
299 ms |
7040 KB |
2_16.txt |
AC |
46 ms |
7040 KB |
2_17.txt |
AC |
299 ms |
7040 KB |
2_18.txt |
AC |
2 ms |
2944 KB |
2_19.txt |
AC |
48 ms |
11516 KB |
2_20.txt |
AC |
32 ms |
7416 KB |
2_21.txt |
WA |
364 ms |
15616 KB |
2_22.txt |
AC |
52 ms |
11520 KB |
2_23.txt |
AC |
54 ms |
11392 KB |
2_24.txt |
AC |
57 ms |
11264 KB |
2_25.txt |
AC |
52 ms |
11520 KB |
2_26.txt |
AC |
56 ms |
11392 KB |
2_27.txt |
AC |
44 ms |
7424 KB |
2_28.txt |
AC |
47 ms |
7168 KB |
2_29.txt |
AC |
49 ms |
7168 KB |
2_30.txt |
AC |
54 ms |
7040 KB |
2_31.txt |
AC |
51 ms |
9856 KB |
2_32.txt |
AC |
53 ms |
7296 KB |
2_33.txt |
AC |
52 ms |
7040 KB |
2_34.txt |
AC |
47 ms |
7040 KB |
2_35.txt |
AC |
51 ms |
7040 KB |
2_36.txt |
AC |
125 ms |
7416 KB |
2_37.txt |
AC |
124 ms |
7416 KB |
2_38.txt |
AC |
124 ms |
7416 KB |
2_39.txt |
AC |
124 ms |
7416 KB |
2_40.txt |
AC |
125 ms |
7416 KB |
2_41.txt |
AC |
125 ms |
7416 KB |
2_42.txt |
AC |
125 ms |
7416 KB |
2_43.txt |
AC |
123 ms |
7416 KB |