This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define dbgv(v) cout<<#v<<" = "; f(i,0,v.size()) cout<<v[i]<<" "; cout<<endl
#define dbga(a,x,y) cout<<#a<<" = "; f(i,x,y) cout<<a[i]<<" "; cout<<endl
#define erorp(x) cout<<#x<<"={"<<x.F<<" , "<<x.S<<"}"<<endl
#define eror(x) cout<<#x<<'='<<(x)<<endl
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define f(i,a,b) for(int i=a;i<b;i++)
#define nb(x) __builtin_popcount(x)
#define all(v) v.begin(),v.end()
#define bit(n,k) (((n)>>(k))&1)
#define Add(x,y) x=(x+y)%mod
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define lst(x) x[x.size()-1]
#define sz(x) int(x.size())
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first
//#include "catdog.h"
const int N=1e6+99;
int n,ans,a[N],dp[N];
vector<int> g[N];
void initialize(int _n,vector<int> A,vector<int> B){
n=_n;
f(i,0,n-1) g[A[i]].pb(B[i]),g[B[i]].pb(A[i]);
}
void dfs1(int u,int p){
int cnt1=0,cnt2=0;
dp[u]=0;
for(auto v : g[u]){
if(v==p) continue ;
dfs1(v,u);
cnt1+=(dp[v]==1);
cnt2+=(dp[v]==2);
}
if(a[u]==0){
ans+=min(cnt1,cnt2);
if(cnt1>cnt2) dp[u]=1;
if(cnt2>cnt1) dp[u]=2;
}
if(a[u]==1){
ans+=cnt2;
dp[u]=1;
}
if(a[u]==2){
ans+=cnt1;
dp[u]=2;
}
}
int solve(){
ans=0;
dfs1(1,1);
return ans;
}
int cat(int u){
a[u]=1;
return solve();
}
int dog(int u){
a[u]=2;
return solve();
}
int neighbor(int u){
a[u]=0;
return solve();
}
/*
int32_t main(){
ios:: sync_with_stdio(0), cin.tie(0), cout.tie(0);
vector<int> A,B;
int n,q;
cin>>n;
f(i,1,n){
int u,v;
cin>>u>>v;
A.pb(u);
B.pb(v);
}
initialize(n,A,B);
cin>>q;
while(q--){
int t,u;
cin>>t>>u;
if(t==1) cout<<cat(u)<<endl;
if(t==2) cout<<dog(u)<<endl;
if(t==3) cout<<neighbor(u)<<endl;
}
}*/
/*
5
1 2
2 3
2 4
4 5
3
1 3
2 5
2 1
5
1 2
2 3
2 4
4 5
5
1 3
2 5
1 2
2 1
3 2
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |