#pragma GCC optimize("O3", "inline")
#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
#define ins insert
#define pb push_back
#define int unsigned long long int
#define pii pair<int, int>
#define endl '\n'
#define drop(x) cout<<(x)<<endl;return;
#define reach cerr << "reached >.<!" << endl;
/*
m : 11059739 -> l ~23
p : 4567896467
*/
// mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const int mod = 1e9 + 7, sze = 1e6 + 50, inf = 1e6, prime = 2333;
//\\
dp / binary search / greedy / sprase table / segment tree
//
int root,specialnode,n;
int dp[sze][2][2][2][2];
int p[sze];
int dsu[sze];
vector<int> graph[sze];
int f(int node){
if(dsu[node]==node){
return node;
}
return dsu[node]=f(dsu[node]);
}
void dfs(int node,int par=0){
p[node]=par;
for(auto v:graph[node]){
if(v!=par){
dfs(v,node);
}
}
}
int kafkef(int node,int me,int pc,int rt,int sp){
if(dp[node][me][pc][rt][sp]!=-1){
return dp[node][me][pc][rt][sp];
}
if( (node==root && me!=rt) | (node==specialnode&& me!=sp) | (node==specialnode && pc && rt) ){
return dp[node][me][pc][rt][sp] = inf;
}
int ans=inf;
bool mustwhite = ( pc | (node == root && sp) | (node == specialnode && rt));
int sum=me;
for(auto v:graph[node]){
if(v!=p[node]){
sum+=kafkef(v,0,me,rt,sp);
}
}
if(mustwhite){
ans=min(ans,sum);
}
else{
for(auto v:graph[node]){
if(v!=p[node]){
ans=min(ans, sum - kafkef(v,0,me,rt,sp) + kafkef(v,1,me,rt,sp));
}
}
}
return dp[node][me][pc][rt][sp]=ans;
}
void gkd(){
memset(dp, -1, sizeof dp);
int n;
cin>>n;
for(int i=1;i<=n;i++){
dsu[i]=i;
}
for(int i=0;i<n;i++){
int u,v;
cin>>u>>v;
int x = f(u);
int y = f(v);
if(x==y){
root=u;
specialnode=v;
}
else{
dsu[x]=y;
graph[u].pb(v);
graph[v].pb(u);
}
}
dfs(root);
int ans=inf;
for(int i =0 ;i<=1;i++){
for(int j=0;j<=1;j++){
ans=min(ans,kafkef(root,i,0,i,j));
}
}
if(ans>=inf){
drop(-1);
}
drop(ans);
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
int tt = 1; //cin>>tt;
while (tt--)gkd();
}
Compilation message
Main.cpp:19:1: warning: multi-line comment [-Wcomment]
19 | //\\
| ^
Main.cpp: In function 'long long unsigned int kafkef(long long unsigned int, long long unsigned int, long long unsigned int, long long unsigned int, long long unsigned int)':
Main.cpp:47:32: warning: comparison of integer expressions of different signedness: 'long long unsigned int' and 'int' [-Wsign-compare]
47 | if(dp[node][me][pc][rt][sp]!=-1){
| ~~~~~~~~~~~~~~~~~~~~~~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
152404 KB |
Output is correct |
2 |
Correct |
54 ms |
152404 KB |
Output is correct |
3 |
Correct |
51 ms |
152404 KB |
Output is correct |
4 |
Correct |
50 ms |
152400 KB |
Output is correct |
5 |
Correct |
207 ms |
170068 KB |
Output is correct |
6 |
Correct |
216 ms |
170128 KB |
Output is correct |
7 |
Correct |
208 ms |
170112 KB |
Output is correct |
8 |
Correct |
211 ms |
170108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
152404 KB |
Output is correct |
2 |
Correct |
52 ms |
152404 KB |
Output is correct |
3 |
Correct |
54 ms |
152524 KB |
Output is correct |
4 |
Correct |
52 ms |
152404 KB |
Output is correct |
5 |
Correct |
51 ms |
152400 KB |
Output is correct |
6 |
Correct |
52 ms |
152400 KB |
Output is correct |
7 |
Correct |
52 ms |
152404 KB |
Output is correct |
8 |
Correct |
51 ms |
152400 KB |
Output is correct |
9 |
Correct |
53 ms |
152400 KB |
Output is correct |
10 |
Correct |
51 ms |
152400 KB |
Output is correct |
11 |
Correct |
52 ms |
152412 KB |
Output is correct |
12 |
Correct |
52 ms |
152352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
152404 KB |
Output is correct |
2 |
Correct |
52 ms |
152404 KB |
Output is correct |
3 |
Correct |
54 ms |
152524 KB |
Output is correct |
4 |
Correct |
52 ms |
152404 KB |
Output is correct |
5 |
Correct |
51 ms |
152400 KB |
Output is correct |
6 |
Correct |
52 ms |
152400 KB |
Output is correct |
7 |
Correct |
52 ms |
152404 KB |
Output is correct |
8 |
Correct |
51 ms |
152400 KB |
Output is correct |
9 |
Correct |
53 ms |
152400 KB |
Output is correct |
10 |
Correct |
51 ms |
152400 KB |
Output is correct |
11 |
Correct |
52 ms |
152412 KB |
Output is correct |
12 |
Correct |
52 ms |
152352 KB |
Output is correct |
13 |
Correct |
53 ms |
152524 KB |
Output is correct |
14 |
Correct |
53 ms |
152400 KB |
Output is correct |
15 |
Correct |
53 ms |
152456 KB |
Output is correct |
16 |
Correct |
51 ms |
152404 KB |
Output is correct |
17 |
Correct |
49 ms |
152404 KB |
Output is correct |
18 |
Correct |
53 ms |
152368 KB |
Output is correct |
19 |
Correct |
49 ms |
152404 KB |
Output is correct |
20 |
Correct |
52 ms |
152340 KB |
Output is correct |
21 |
Correct |
51 ms |
152480 KB |
Output is correct |
22 |
Correct |
52 ms |
152336 KB |
Output is correct |
23 |
Correct |
50 ms |
152556 KB |
Output is correct |
24 |
Correct |
49 ms |
152400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
152404 KB |
Output is correct |
2 |
Correct |
54 ms |
152404 KB |
Output is correct |
3 |
Correct |
51 ms |
152404 KB |
Output is correct |
4 |
Correct |
50 ms |
152400 KB |
Output is correct |
5 |
Correct |
207 ms |
170068 KB |
Output is correct |
6 |
Correct |
216 ms |
170128 KB |
Output is correct |
7 |
Correct |
208 ms |
170112 KB |
Output is correct |
8 |
Correct |
211 ms |
170108 KB |
Output is correct |
9 |
Correct |
51 ms |
152404 KB |
Output is correct |
10 |
Correct |
52 ms |
152404 KB |
Output is correct |
11 |
Correct |
54 ms |
152524 KB |
Output is correct |
12 |
Correct |
52 ms |
152404 KB |
Output is correct |
13 |
Correct |
51 ms |
152400 KB |
Output is correct |
14 |
Correct |
52 ms |
152400 KB |
Output is correct |
15 |
Correct |
52 ms |
152404 KB |
Output is correct |
16 |
Correct |
51 ms |
152400 KB |
Output is correct |
17 |
Correct |
53 ms |
152400 KB |
Output is correct |
18 |
Correct |
51 ms |
152400 KB |
Output is correct |
19 |
Correct |
52 ms |
152412 KB |
Output is correct |
20 |
Correct |
52 ms |
152352 KB |
Output is correct |
21 |
Correct |
53 ms |
152524 KB |
Output is correct |
22 |
Correct |
53 ms |
152400 KB |
Output is correct |
23 |
Correct |
53 ms |
152456 KB |
Output is correct |
24 |
Correct |
51 ms |
152404 KB |
Output is correct |
25 |
Correct |
49 ms |
152404 KB |
Output is correct |
26 |
Correct |
53 ms |
152368 KB |
Output is correct |
27 |
Correct |
49 ms |
152404 KB |
Output is correct |
28 |
Correct |
52 ms |
152340 KB |
Output is correct |
29 |
Correct |
51 ms |
152480 KB |
Output is correct |
30 |
Correct |
52 ms |
152336 KB |
Output is correct |
31 |
Correct |
50 ms |
152556 KB |
Output is correct |
32 |
Correct |
49 ms |
152400 KB |
Output is correct |
33 |
Correct |
135 ms |
157304 KB |
Output is correct |
34 |
Correct |
134 ms |
157268 KB |
Output is correct |
35 |
Correct |
136 ms |
157392 KB |
Output is correct |
36 |
Correct |
129 ms |
157268 KB |
Output is correct |
37 |
Correct |
69 ms |
153684 KB |
Output is correct |
38 |
Correct |
153 ms |
157484 KB |
Output is correct |
39 |
Correct |
56 ms |
152912 KB |
Output is correct |
40 |
Correct |
138 ms |
157484 KB |
Output is correct |
41 |
Correct |
135 ms |
158404 KB |
Output is correct |
42 |
Correct |
136 ms |
158272 KB |
Output is correct |
43 |
Correct |
183 ms |
170108 KB |
Output is correct |
44 |
Correct |
176 ms |
163996 KB |
Output is correct |