QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#779592 | #2596. Even Forest | aas | WA | 5ms | 33148kb | C++14 | 920b | 2024-11-24 20:12:30 | 2024-11-24 20:12:30 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+5;
const int inf=0x3f3f3f3f3f3f3f3f;
int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
vector<int> g[N];
int s[N],o[N],p[N];
void dfs(int x,int fa){
s[x]=0;
o[x]=p[x]=inf;
int mx=0,cnt=0;
int mn=inf,mi=inf;
for(int v:g[x]){
if(v==fa)continue;
dfs(v,x);
s[x]+=min(p[v],s[v]+1);
if(s[v]<o[v]+1)mx+=s[v],++cnt;
else{
if(s[v]-o[v]-1<mn)mi=mn,mn=s[v]-o[v]-1;
else if(s[v]-o[v]-1<mi)mi=s[v]-o[v]-1;
mx+=o[v]+1;
}
}
if(cnt>=2)o[x]=p[x]=mx;
else if(cnt==1)o[x]=mx+mn,p[x]=mx;
else o[x]=mx+mn+mi,p[x]=mx+mn;
}
signed main(){
int n=read();
for(int i=1;i<n;i++){
int x=read(),v=read();
g[x].push_back(v);
}
dfs(1,0);
printf("%lld\n",min(s[1],o[1]));
}
詳細信息
Test #1:
score: 100
Accepted
time: 5ms
memory: 32460kb
input:
4 1 2 2 3 3 4
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 0ms
memory: 31924kb
input:
4 1 2 1 3 1 4
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: -100
Wrong Answer
time: 3ms
memory: 33148kb
input:
6 1 2 2 3 4 2 4 5 6 4
output:
0
result:
wrong answer 1st numbers differ - expected: '1', found: '0'