QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#778156 | #2596. Even Forest | niumachaoren | WA | 8ms | 61992kb | C++14 | 1.4kb | 2024-11-24 13:00:49 | 2024-11-24 13:00:49 |
Judging History
answer
#include<bits/stdc++.h>
inline void read(int &x){
x=0;char ch=getchar();
while(!isdigit(ch))ch=getchar();
while(isdigit(ch)){
x=(x<<1)+(x<<3)+(ch^48);
ch=getchar();
}
}
using namespace std;
const int maxn=1e6+10;
int n,dp[maxn][2],g[maxn][2],rt;
vector<int>e[maxn<<1];
void dfs(int u,int fa){
if(e[u].size()==1&&u!=rt){
// cout<<rt<<'\n';
dp[u][0]=0;
return ;
}
int siz=0;
for(int v:e[u]){
if(v==fa)continue;
siz++;
dfs(v,u);
}
for(int i=0;i<=siz;i++){
g[i][0]=g[i][1]=0x3f3f3f3f;
}
g[0][0]=g[0][1]=0;
for(int i=1;i<=e[u].size();i++){
int v=e[u][i-1];
if(v==fa)continue;
for(int j=i;j>=0;j--){
g[j][0]=min(g[j][0]+dp[v][1],g[j-1][0]+dp[v][0]);//选j个偶的留下
g[j][1]=min(g[j][1]+dp[v][0],g[j-1][1]+dp[v][1]);//选j个奇的留下
}
}
for(int j=0;j<=siz;j++){
dp[u][0]=min(dp[u][0],g[j][1]+siz-j);
dp[u][1]=min(dp[u][1],g[j][0]+siz-j);
}
}
int main(){
read(n);
for(int i=1;i<n;i++){
int u,v;
read(u),read(v);
e[u].push_back(v);
e[v].push_back(u);
}
int ans=0;
memset(dp,0x3f,sizeof(dp));
rt=1;
dfs(rt,0);
ans=max(ans,min(dp[rt][1],dp[rt][0]));
memset(dp,0x3f,sizeof(dp));
rt=n;
dfs(rt,0);
ans=max(ans,min(dp[rt][1],dp[rt][0]));
memset(dp,0x3f,sizeof(dp));
rt=n/2;
dfs(rt,0);
ans=max(ans,min(dp[rt][1],dp[rt][0]));
cout<<ans<<'\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 8ms
memory: 59964kb
input:
4 1 2 2 3 3 4
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 4ms
memory: 60052kb
input:
4 1 2 1 3 1 4
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 4ms
memory: 59988kb
input:
6 1 2 2 3 4 2 4 5 6 4
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: 0
Accepted
time: 8ms
memory: 59984kb
input:
1
output:
0
result:
ok 1 number(s): "0"
Test #5:
score: -100
Wrong Answer
time: 4ms
memory: 61992kb
input:
2 1 2
output:
0
result:
wrong answer 1st numbers differ - expected: '1', found: '0'