#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define fi first
#define se second
#define ins push_back
#define vec vector
#define FOR(i, l, r) for(int i=l; i<=r; ++i)
#define FORD(i, r, l) for(int i=r; i>=l; --i)
#define FORE(i, v) for(auto &i : v)
#define all(x) (x).begin(), (x).end()
#define file "input"
const int lim = 1e9, nx = 1e5+5;
int n;
vec<int> graph[nx];
bool calc[nx][2][2][2][2];
int root, spec, dp[nx][2][2][2][2];
bool vi[nx];
void prep(int u, int p) {
vi[u] = 1;
if (root > 0) return;
for(int v : graph[u]) if (v != p) {
if (!vi[v]) prep(v, u);
else {
root = v;
spec = u;
return;
}
}
}
int dfs(int u, bool c, bool p, bool r, bool sp, int par) {
if (calc[u][c][p][r][sp]) return dp[u][c][p][r][sp];
calc[u][c][p][r][sp] = 1;
if (u == root && c != r) return n+1;
if (u == spec && c != sp) return n+1;
if (u == spec && p && r) return n+1;
bool satisfied = 0;
if (u == root && sp) satisfied = 1;
if (u == spec && r) satisfied = 1;
if (p) satisfied = 1;
int &ret = dp[u][c][p][r][sp];
ret = c;
bool covered = p || (u == root && sp) || (u == spec && r);
if (covered)
{
for (int v : graph[u]) if (v ^ par) {
if (v == root && u == spec) continue;
if (u == root && v == spec) continue;
ret += dfs(v, 0, c, r, sp, u);
}
}
else
{
int alter = n + 1;
for (int v : graph[u]) if (v ^ par)
{
if (v == root && u == spec) continue;
if (u == root && v == spec) continue;
ret += dfs(v, 0, c, r, sp, u);
alter = min(alter, dfs(v, 1, c, r, sp, u) - dfs(v, 0, c, r, sp, u));
}
ret += alter;
}
ret = min(ret, n+1);
return ret;
}
void solve() {
memset(dp, 0x3f, sizeof(dp));
cin >> n;
FOR(i, 1, n) {
int u, v;
cin >> u >> v;
graph[u].ins(v);
graph[v].ins(u);
}
prep(1, 1);
FOR(i, 1, n)
FOR(c, 0, 1)
FOR(p, 0, 1)
FOR(r, 0, 1)
FOR(sp, 0, 1)
dp[i][c][p][r][sp] = n+1;
int ans = n+1;
FOR(r, 0, 1)
FOR(sp, 0, 1) {
ans = min(ans, dfs(root, r, 0, r, sp, root));
// if (ans == 2) cout << r << " " << sp << "\n";
}
cout << (ans <= n ? ans : -1);
// cout << dfs(root, root, 0, 0, 0, 0);
// cout << root << " " << spec << "\n";
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (fopen(file".inp", "r")) {
freopen(file".inp", "r", stdin);
freopen(file".out", "w", stdout);
}
solve();
return 0;
}
Compilation message
Main.cpp: In function 'int dfs(int, bool, bool, bool, bool, int)':
Main.cpp:44:8: warning: variable 'satisfied' set but not used [-Wunused-but-set-variable]
44 | bool satisfied = 0;
| ^~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:113:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
113 | freopen(file".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:114:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
114 | freopen(file".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
3 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
136 ms |
29080 KB |
Output is correct |
6 |
Correct |
178 ms |
29076 KB |
Output is correct |
7 |
Correct |
147 ms |
29080 KB |
Output is correct |
8 |
Correct |
163 ms |
28928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10640 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10584 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10584 KB |
Output is correct |
10 |
Correct |
3 ms |
10584 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10640 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10584 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10584 KB |
Output is correct |
10 |
Correct |
3 ms |
10584 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
3 ms |
10588 KB |
Output is correct |
14 |
Correct |
3 ms |
10588 KB |
Output is correct |
15 |
Correct |
3 ms |
10676 KB |
Output is correct |
16 |
Correct |
3 ms |
10584 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
3 ms |
10588 KB |
Output is correct |
19 |
Correct |
2 ms |
10636 KB |
Output is correct |
20 |
Correct |
3 ms |
10588 KB |
Output is correct |
21 |
Correct |
2 ms |
10588 KB |
Output is correct |
22 |
Correct |
2 ms |
10588 KB |
Output is correct |
23 |
Correct |
3 ms |
10584 KB |
Output is correct |
24 |
Correct |
3 ms |
10664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
3 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
136 ms |
29080 KB |
Output is correct |
6 |
Correct |
178 ms |
29076 KB |
Output is correct |
7 |
Correct |
147 ms |
29080 KB |
Output is correct |
8 |
Correct |
163 ms |
28928 KB |
Output is correct |
9 |
Correct |
2 ms |
10640 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10584 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10584 KB |
Output is correct |
18 |
Correct |
3 ms |
10584 KB |
Output is correct |
19 |
Correct |
2 ms |
10588 KB |
Output is correct |
20 |
Correct |
2 ms |
10588 KB |
Output is correct |
21 |
Correct |
3 ms |
10588 KB |
Output is correct |
22 |
Correct |
3 ms |
10588 KB |
Output is correct |
23 |
Correct |
3 ms |
10676 KB |
Output is correct |
24 |
Correct |
3 ms |
10584 KB |
Output is correct |
25 |
Correct |
2 ms |
10588 KB |
Output is correct |
26 |
Correct |
3 ms |
10588 KB |
Output is correct |
27 |
Correct |
2 ms |
10636 KB |
Output is correct |
28 |
Correct |
3 ms |
10588 KB |
Output is correct |
29 |
Correct |
2 ms |
10588 KB |
Output is correct |
30 |
Correct |
2 ms |
10588 KB |
Output is correct |
31 |
Correct |
3 ms |
10584 KB |
Output is correct |
32 |
Correct |
3 ms |
10664 KB |
Output is correct |
33 |
Correct |
62 ms |
14564 KB |
Output is correct |
34 |
Correct |
65 ms |
14424 KB |
Output is correct |
35 |
Correct |
71 ms |
14572 KB |
Output is correct |
36 |
Correct |
79 ms |
14428 KB |
Output is correct |
37 |
Correct |
15 ms |
11612 KB |
Output is correct |
38 |
Correct |
77 ms |
14776 KB |
Output is correct |
39 |
Correct |
6 ms |
10844 KB |
Output is correct |
40 |
Correct |
63 ms |
14312 KB |
Output is correct |
41 |
Correct |
46 ms |
15316 KB |
Output is correct |
42 |
Correct |
51 ms |
15188 KB |
Output is correct |
43 |
Correct |
106 ms |
28960 KB |
Output is correct |
44 |
Correct |
76 ms |
22168 KB |
Output is correct |