QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#407551 | #5157. High-quality Tree | adivse | RE | 3ms | 45032kb | C++14 | 1.3kb | 2024-05-08 23:35:07 | 2024-05-08 23:35:07 |
Judging History
answer
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define xl F[x].son[0]
#define xr F[x].son[1]
#define fir first
#define sec second
#define bug cout<<"bugbugbugbug"<<endl;
using namespace std;
void kuaidu(){ios::sync_with_stdio(false),cin.tie(0);}
inline int max(int a,int b){ if(a<b) return b; return a;}
inline int min(int a,int b){ if(a<b) return a; return b;}
const int N=1e5+10;
const int M=1e6+10;
const int mod=1e9+7;
const int INF=1e16;
typedef pair<int,int> PII;
int n,m,T;
vector<int> A[N];
int son[N];
int dp[N][50];
void dfs(int x,int pa){
son[x]=1;
int x1=0,x2=0;
for(auto y:A[x]){
if(y==pa) continue;
dfs(y,x);
son[x]+=son[y];
if(x1) x2=y;
else x1=y;
}
dp[x][1]=son[x]-1;
dp[x][0]=son[x];
if(x1 && x2){
for(int i=2;i<50;i++){
for(int o=1;o<=2;o++)
for(int p=1;p<=2;p++){
if(o==2 && p==2) continue;
dp[x][i]=min({dp[x][i],dp[x1][i-o]+dp[x2][i-p]});
}
}
}
else if(x1){
dp[x][2]=son[x]-2;
}
}
signed main(){
kuaidu();
// T=1;cin>>T;while(T--){}
memset(dp,0x3f,sizeof dp);
cin>>n;
for(int i=1;i<n;i++){
int a,b;
cin>>a>>b;
A[a].push_back(b);
A[b].push_back(a);
}
dfs(1,1);
int ans=INF;
for(int i=0;i<50;i++) ans=min(ans,dp[1][i]);
cout<<ans<<endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 45012kb
input:
6 1 2 1 3 3 4 3 5 5 6
output:
1
result:
ok single line: '1'
Test #2:
score: 0
Accepted
time: 3ms
memory: 45032kb
input:
12 1 2 2 3 3 4 3 5 1 6 6 7 7 8 7 9 9 10 6 11 11 12
output:
3
result:
ok single line: '3'
Test #3:
score: -100
Runtime Error
input:
200000 167246 158246 40931 40296 178588 27974 35171 899 4204 163250 101422 9230 55420 93371 16012 140142 28866 154497 33519 180725 50361 52348 46923 175364 126599 169575 15138 34958 164256 64770 63123 130169 154172 168301 127476 54744 199964 81879 173765 69220 178225 73653 59861 46415 138112 17507 8...