#include <bits/stdc++.h>
#define Alora "cownav"
#define fi(i,a,b) for(int i = a; i <= b; i++)
#define fid(i,a,b) for(int i = a; i >= b; i--)
#define int long long
#define ll long long
#define f first
#define se second
#define pii pair<int, int>
#define getbit(i, j) ((i >> j) & 1)
#define all(v) v.begin(), v.end()
#define pb push_back
#define maxn 100005
const int M = 1e9 + 7;
using namespace std;
int n, anc[maxn], cha[maxn], root, special, res = 1e9;
vector <int> g[maxn];
int get(int u){
if(anc[u] == 0) return u;
return (anc[u] = get(anc[u]));
}
void dfs(int u, int fa){
for(auto v: g[u]) if(v != fa){
cha[v] = u;
dfs(v, u);
}
}
int dp[maxn][2][2][2][2];
int cal(int u, int me, int up, int rt, int sp){
int &ans = dp[u][me][up][rt][sp];
if(ans != -1) return ans;
ans = 1e9;
bool ok = 0;
if(u == root && me != rt) ok = 1;
if(u == special && me != sp) ok = 1;
if(u == special && rt && up) ok = 1;
if(ok) return (ans = 1e9); // TH vi phạm
bool covered = 0;
if(up || (u == root && sp) || (u == special && rt)) covered = 1;
int sum = me;
for(auto v: g[u]) if(v != cha[u]){
sum += cal(v, 0, me, rt, sp);
}
if(covered) return (ans = sum); // TH đỉnh u đã đc bao phủ bởi 1 đỉnh màu đen
for(auto v: g[u]) if(v != cha[u]){
ans = min(ans, sum - cal(v, 0, me, rt, sp) + cal(v, 1, me, rt, sp));
}
return ans;
}
main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
if(fopen(Alora".inp", "r")){
freopen(Alora".inp", "r", stdin);
freopen(Alora".out", "w", stdout);}
cin >> n;
fi(i, 1, n){
int u, v; cin >> u >> v;
int p = get(u), q = get(v);
if(p != q){
anc[p] = q;
g[u].pb(v); g[v].pb(u);
}
else root = u, special = v;
// xóa 1 cạnh trong chu trình để tạo thành cây -> dp trêm cây.
// đánh dâu lại 2 đỉnh này, 1 đỉnh làm gốc, 1 đỉnh là special
}
memset(dp, -1, sizeof(dp));
dfs(root, 0);
fi(rt, 0, 1)
fi(sp, 0, 1)
res = min(res, cal(root, rt, 0, rt, sp));
if(res == 1e9) cout << -1;
else cout << res;
return 0;
}
Compilation message
Main.cpp:56:2: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
56 | main(){
| ^~~~
Main.cpp: In function 'int main()':
Main.cpp:60:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | freopen(Alora".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:61:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | freopen(Alora".out", "w", stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
15196 KB |
Output is correct |
2 |
Correct |
3 ms |
15196 KB |
Output is correct |
3 |
Correct |
3 ms |
15196 KB |
Output is correct |
4 |
Correct |
2 ms |
15196 KB |
Output is correct |
5 |
Correct |
155 ms |
30952 KB |
Output is correct |
6 |
Correct |
135 ms |
30956 KB |
Output is correct |
7 |
Correct |
165 ms |
30960 KB |
Output is correct |
8 |
Correct |
169 ms |
30960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
15196 KB |
Output is correct |
2 |
Correct |
2 ms |
15196 KB |
Output is correct |
3 |
Correct |
3 ms |
15196 KB |
Output is correct |
4 |
Correct |
3 ms |
15196 KB |
Output is correct |
5 |
Correct |
3 ms |
15196 KB |
Output is correct |
6 |
Correct |
2 ms |
15196 KB |
Output is correct |
7 |
Correct |
3 ms |
15196 KB |
Output is correct |
8 |
Correct |
3 ms |
15196 KB |
Output is correct |
9 |
Correct |
3 ms |
15328 KB |
Output is correct |
10 |
Correct |
3 ms |
15196 KB |
Output is correct |
11 |
Correct |
3 ms |
15196 KB |
Output is correct |
12 |
Correct |
3 ms |
15196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
15196 KB |
Output is correct |
2 |
Correct |
2 ms |
15196 KB |
Output is correct |
3 |
Correct |
3 ms |
15196 KB |
Output is correct |
4 |
Correct |
3 ms |
15196 KB |
Output is correct |
5 |
Correct |
3 ms |
15196 KB |
Output is correct |
6 |
Correct |
2 ms |
15196 KB |
Output is correct |
7 |
Correct |
3 ms |
15196 KB |
Output is correct |
8 |
Correct |
3 ms |
15196 KB |
Output is correct |
9 |
Correct |
3 ms |
15328 KB |
Output is correct |
10 |
Correct |
3 ms |
15196 KB |
Output is correct |
11 |
Correct |
3 ms |
15196 KB |
Output is correct |
12 |
Correct |
3 ms |
15196 KB |
Output is correct |
13 |
Correct |
3 ms |
15196 KB |
Output is correct |
14 |
Correct |
4 ms |
15196 KB |
Output is correct |
15 |
Correct |
3 ms |
15196 KB |
Output is correct |
16 |
Correct |
3 ms |
15196 KB |
Output is correct |
17 |
Correct |
2 ms |
15328 KB |
Output is correct |
18 |
Correct |
3 ms |
15196 KB |
Output is correct |
19 |
Correct |
3 ms |
15192 KB |
Output is correct |
20 |
Correct |
3 ms |
15196 KB |
Output is correct |
21 |
Correct |
3 ms |
15196 KB |
Output is correct |
22 |
Correct |
3 ms |
15196 KB |
Output is correct |
23 |
Correct |
3 ms |
15400 KB |
Output is correct |
24 |
Correct |
3 ms |
15196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
15196 KB |
Output is correct |
2 |
Correct |
3 ms |
15196 KB |
Output is correct |
3 |
Correct |
3 ms |
15196 KB |
Output is correct |
4 |
Correct |
2 ms |
15196 KB |
Output is correct |
5 |
Correct |
155 ms |
30952 KB |
Output is correct |
6 |
Correct |
135 ms |
30956 KB |
Output is correct |
7 |
Correct |
165 ms |
30960 KB |
Output is correct |
8 |
Correct |
169 ms |
30960 KB |
Output is correct |
9 |
Correct |
3 ms |
15196 KB |
Output is correct |
10 |
Correct |
2 ms |
15196 KB |
Output is correct |
11 |
Correct |
3 ms |
15196 KB |
Output is correct |
12 |
Correct |
3 ms |
15196 KB |
Output is correct |
13 |
Correct |
3 ms |
15196 KB |
Output is correct |
14 |
Correct |
2 ms |
15196 KB |
Output is correct |
15 |
Correct |
3 ms |
15196 KB |
Output is correct |
16 |
Correct |
3 ms |
15196 KB |
Output is correct |
17 |
Correct |
3 ms |
15328 KB |
Output is correct |
18 |
Correct |
3 ms |
15196 KB |
Output is correct |
19 |
Correct |
3 ms |
15196 KB |
Output is correct |
20 |
Correct |
3 ms |
15196 KB |
Output is correct |
21 |
Correct |
3 ms |
15196 KB |
Output is correct |
22 |
Correct |
4 ms |
15196 KB |
Output is correct |
23 |
Correct |
3 ms |
15196 KB |
Output is correct |
24 |
Correct |
3 ms |
15196 KB |
Output is correct |
25 |
Correct |
2 ms |
15328 KB |
Output is correct |
26 |
Correct |
3 ms |
15196 KB |
Output is correct |
27 |
Correct |
3 ms |
15192 KB |
Output is correct |
28 |
Correct |
3 ms |
15196 KB |
Output is correct |
29 |
Correct |
3 ms |
15196 KB |
Output is correct |
30 |
Correct |
3 ms |
15196 KB |
Output is correct |
31 |
Correct |
3 ms |
15400 KB |
Output is correct |
32 |
Correct |
3 ms |
15196 KB |
Output is correct |
33 |
Correct |
92 ms |
20828 KB |
Output is correct |
34 |
Correct |
79 ms |
20988 KB |
Output is correct |
35 |
Correct |
99 ms |
20828 KB |
Output is correct |
36 |
Correct |
104 ms |
20968 KB |
Output is correct |
37 |
Correct |
21 ms |
16728 KB |
Output is correct |
38 |
Correct |
103 ms |
20892 KB |
Output is correct |
39 |
Correct |
8 ms |
15708 KB |
Output is correct |
40 |
Correct |
92 ms |
20964 KB |
Output is correct |
41 |
Correct |
66 ms |
22076 KB |
Output is correct |
42 |
Correct |
82 ms |
21780 KB |
Output is correct |
43 |
Correct |
138 ms |
32104 KB |
Output is correct |
44 |
Correct |
113 ms |
26836 KB |
Output is correct |