QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#646799#2596. Even Forestship2077WA 2ms7868kbC++231020b2024-10-17 08:42:022024-10-17 08:42:02

Judging History

你现在查看的是最新测评结果

  • [2024-10-17 08:42:02]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:7868kb
  • [2024-10-17 08:42:02]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
constexpr int M=1e6+5,inf=0x3f3f3f3f;
int n,dep[M],dp[M][2];
vector<int>adj[M];
int read(){
    int x=0;char ch=getchar();
    while (!isdigit(ch)) ch=getchar();
    while (isdigit(ch)) x=x*10+ch-48,ch=getchar();
    return x;
}
void dfs(int x,int f){
    dep[x]=dep[f]^1;
    bool leaf=1;int cur=0;
    for (auto y:adj[x]){
        if (y==f) continue; leaf=0; dfs(y,x);
        dp[x][0]=min(dp[x][0]+dp[y][0],inf);
        dp[x][1]=min(dp[x][1]+dp[y][1],inf);
        cur+=min(dp[y][dep[x]],dp[y][!dep[x]]+1);
    }
    if (leaf) dp[x][dep[x]]=0,dp[x][!dep[x]]=inf;
    else dp[x][dep[x]]=min(dp[x][dep[x]],cur);
}
int main(){ n=read();
    for (int i=1;i<n;i++){
        int x=read(),y=read();
        adj[x].emplace_back(y);
        adj[y].emplace_back(x);
    } dep[0]=1;
    for (int i=1;i<=n;i++)
        if (adj[i].size()==1){
            dfs(i,0);
            printf("%d\n",dp[i][0]);
            exit(0);
        }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 7868kb

input:

4
1 2
2 3
3 4

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: 0
Accepted
time: 0ms
memory: 7860kb

input:

4
1 2
1 3
1 4

output:

0

result:

ok 1 number(s): "0"

Test #3:

score: -100
Wrong Answer
time: 2ms
memory: 7856kb

input:

6
1 2
2 3
4 2
4 5
6 4

output:

2

result:

wrong answer 1st numbers differ - expected: '1', found: '2'