#include <bits/stdc++.h>
using namespace std;
#define lg long long
const lg N = 2e5+5;
vector<lg> adj[N], adj2[N];
lg dp[N][4][2][2], par[N], vis[N], tmp, node1, node2;
lg get(lg src)
{
if(par[src] == src) return src;
return par[src] = get(par[src]);
}
lg dfs(lg src, lg b, lg x, lg y, lg par = -1)
{
b &= 3;
auto&ret = dp[src][b][x][y];
if(src == node1 && (b&1) != x)
{
// cout << src << ' ' << b << ' ' << x << ' ' << y << '\n';
// cout << 1e14 << '\n';
return ret = 1e14;
}
if(src == node2 && (b&1) != y)
{
// cout << src << ' ' << b << ' ' << x << ' ' << y << '\n';
// cout << 1e14 << '\n';
return ret = 1e14;
}
if(~ret) return ret;
ret = 1e14;
lg sum = 0;
lg check = 0;
for(auto it : adj[src])
{
if(it == par) continue;
if(src != node1 && src != node2 && ((it == node1 && x) || (it == node2 && y))) check++;
sum += dfs(it, (b << 1), x, y, src);
}
if((src == node1 && y) || (src == node2 && x)) check++;
if(src == node1 || src == node2)
{
if(par != node1 && par != node2) check += b/2;
}
else if((b&2)) check++;
if(check > 1)
{
// cout << "FAIL " << src << ' ' << b << ' ' << x << ' ' << y << ' ' << check << ' ' << par << '\n';
// cout << ret << '\n';
return ret = 1e14;
}
if(b&2)
{
ret = sum;
// cout << "b " << src << ' ' << b << ' ' << x << ' ' << y << ' ' << check << ' ' << par << '\n';
// cout << ret << '\n';
return ret;
}
lg ox = 1;
for(auto it : adj[src])
{
if(it == par) continue;
if(check && it != node1 && it != node2) continue;
ox = 0;
ret = min(ret, dfs(it, (b << 1)|1, x, y, src)-dfs(it, (b << 1), x, y, src)+sum+1);
}
if(check && ox) ret = sum;
// cout << "a " << src << ' ' << b << ' ' << x << ' ' << y << ' ' << check << ' ' << par << '\n';
// cout << ret << '\n';
return ret;
}
vector<lg> path;
lg odd_cycle(lg src, lg par = -1)
{
vis[src] = ++tmp;
for(auto it : adj2[src])
{
if(it == par) continue;
if(vis[it])
{
path.push_back(it);
return (vis[src]-vis[it]-1)%2;
}
if(odd_cycle(it, src))
{
path.push_back(src);
return true;
}
}
return 0;
}
int main()
{
lg n;
cin >> n;
for(int i = 1; i <= n; i++) par[i] = i;
for(int i = 0; i < n; i++)
{
lg u, v;
cin >> u >> v;
int a = get(u), b = get(v);
adj2[u].push_back(v);
adj2[v].push_back(u);
if(a == b)
{
node1 = u;
node2 = v;
continue;
}
par[a] = b;
adj[u].push_back(v);
adj[v].push_back(u);
}
memset(dp, -1, sizeof(dp));
lg ans = 1e18;
for(int i = 0; i < 4; i++)
{
ans = min({ans, dfs(1, 0, i%2, i/2), dfs(1, 1, i%2, i/2)+1});
}
if(ans >= 1e14) ans = -1;
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
37724 KB |
Output is correct |
2 |
Correct |
5 ms |
37780 KB |
Output is correct |
3 |
Correct |
6 ms |
37720 KB |
Output is correct |
4 |
Correct |
6 ms |
37728 KB |
Output is correct |
5 |
Correct |
202 ms |
56632 KB |
Output is correct |
6 |
Correct |
168 ms |
56144 KB |
Output is correct |
7 |
Correct |
166 ms |
53540 KB |
Output is correct |
8 |
Correct |
185 ms |
54264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
37724 KB |
Output is correct |
2 |
Correct |
6 ms |
37764 KB |
Output is correct |
3 |
Correct |
6 ms |
37724 KB |
Output is correct |
4 |
Correct |
5 ms |
37720 KB |
Output is correct |
5 |
Correct |
6 ms |
37724 KB |
Output is correct |
6 |
Correct |
6 ms |
37820 KB |
Output is correct |
7 |
Correct |
6 ms |
37724 KB |
Output is correct |
8 |
Correct |
6 ms |
37724 KB |
Output is correct |
9 |
Correct |
6 ms |
37816 KB |
Output is correct |
10 |
Correct |
6 ms |
37820 KB |
Output is correct |
11 |
Correct |
6 ms |
37724 KB |
Output is correct |
12 |
Correct |
5 ms |
37720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
37724 KB |
Output is correct |
2 |
Correct |
6 ms |
37764 KB |
Output is correct |
3 |
Correct |
6 ms |
37724 KB |
Output is correct |
4 |
Correct |
5 ms |
37720 KB |
Output is correct |
5 |
Correct |
6 ms |
37724 KB |
Output is correct |
6 |
Correct |
6 ms |
37820 KB |
Output is correct |
7 |
Correct |
6 ms |
37724 KB |
Output is correct |
8 |
Correct |
6 ms |
37724 KB |
Output is correct |
9 |
Correct |
6 ms |
37816 KB |
Output is correct |
10 |
Correct |
6 ms |
37820 KB |
Output is correct |
11 |
Correct |
6 ms |
37724 KB |
Output is correct |
12 |
Correct |
5 ms |
37720 KB |
Output is correct |
13 |
Correct |
6 ms |
37724 KB |
Output is correct |
14 |
Correct |
6 ms |
37884 KB |
Output is correct |
15 |
Correct |
7 ms |
37724 KB |
Output is correct |
16 |
Correct |
7 ms |
37768 KB |
Output is correct |
17 |
Correct |
6 ms |
37724 KB |
Output is correct |
18 |
Correct |
7 ms |
37736 KB |
Output is correct |
19 |
Correct |
6 ms |
37724 KB |
Output is correct |
20 |
Correct |
7 ms |
37728 KB |
Output is correct |
21 |
Correct |
6 ms |
37736 KB |
Output is correct |
22 |
Correct |
6 ms |
37728 KB |
Output is correct |
23 |
Correct |
6 ms |
37900 KB |
Output is correct |
24 |
Correct |
7 ms |
37988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
37724 KB |
Output is correct |
2 |
Correct |
5 ms |
37780 KB |
Output is correct |
3 |
Correct |
6 ms |
37720 KB |
Output is correct |
4 |
Correct |
6 ms |
37728 KB |
Output is correct |
5 |
Correct |
202 ms |
56632 KB |
Output is correct |
6 |
Correct |
168 ms |
56144 KB |
Output is correct |
7 |
Correct |
166 ms |
53540 KB |
Output is correct |
8 |
Correct |
185 ms |
54264 KB |
Output is correct |
9 |
Correct |
6 ms |
37724 KB |
Output is correct |
10 |
Correct |
6 ms |
37764 KB |
Output is correct |
11 |
Correct |
6 ms |
37724 KB |
Output is correct |
12 |
Correct |
5 ms |
37720 KB |
Output is correct |
13 |
Correct |
6 ms |
37724 KB |
Output is correct |
14 |
Correct |
6 ms |
37820 KB |
Output is correct |
15 |
Correct |
6 ms |
37724 KB |
Output is correct |
16 |
Correct |
6 ms |
37724 KB |
Output is correct |
17 |
Correct |
6 ms |
37816 KB |
Output is correct |
18 |
Correct |
6 ms |
37820 KB |
Output is correct |
19 |
Correct |
6 ms |
37724 KB |
Output is correct |
20 |
Correct |
5 ms |
37720 KB |
Output is correct |
21 |
Correct |
6 ms |
37724 KB |
Output is correct |
22 |
Correct |
6 ms |
37884 KB |
Output is correct |
23 |
Correct |
7 ms |
37724 KB |
Output is correct |
24 |
Correct |
7 ms |
37768 KB |
Output is correct |
25 |
Correct |
6 ms |
37724 KB |
Output is correct |
26 |
Correct |
7 ms |
37736 KB |
Output is correct |
27 |
Correct |
6 ms |
37724 KB |
Output is correct |
28 |
Correct |
7 ms |
37728 KB |
Output is correct |
29 |
Correct |
6 ms |
37736 KB |
Output is correct |
30 |
Correct |
6 ms |
37728 KB |
Output is correct |
31 |
Correct |
6 ms |
37900 KB |
Output is correct |
32 |
Correct |
7 ms |
37988 KB |
Output is correct |
33 |
Correct |
120 ms |
45300 KB |
Output is correct |
34 |
Correct |
148 ms |
45148 KB |
Output is correct |
35 |
Correct |
126 ms |
45148 KB |
Output is correct |
36 |
Correct |
136 ms |
45312 KB |
Output is correct |
37 |
Correct |
31 ms |
39524 KB |
Output is correct |
38 |
Correct |
126 ms |
45492 KB |
Output is correct |
39 |
Correct |
14 ms |
38244 KB |
Output is correct |
40 |
Correct |
139 ms |
45352 KB |
Output is correct |
41 |
Correct |
127 ms |
46788 KB |
Output is correct |
42 |
Correct |
106 ms |
46792 KB |
Output is correct |
43 |
Correct |
192 ms |
53580 KB |
Output is correct |
44 |
Correct |
144 ms |
52284 KB |
Output is correct |