QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#317687#7936. Eliminate Treeone_god_and_two_dogs#WA 62ms16552kbC++141.0kb2024-01-29 14:09:112024-01-29 14:09:11

Judging History

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

  • [2024-01-29 14:09:11]
  • 评测
  • 测评结果:WA
  • 用时:62ms
  • 内存:16552kb
  • [2024-01-29 14:09:11]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int M=2e5+5;
int n;
vector<int>Q[M];
int in[M];
int que[M],L=1,R=0;
bool flag[M];
void Del(int x) {
    flag[x]=true;
    for(int i=0; i<Q[x].size(); ++i) {
        int To=Q[x][i];
        if(flag[To])continue;
        in[To]--;
        if(in[To]<=1)que[++R]=To;
    }
}
int main() {
    scanf("%d",&n);
    for(int i=1; i<n; ++i) {
        int u,v;
        scanf("%d%d",&u,&v);
        Q[u].push_back(v);++in[v];
        Q[v].push_back(u);++in[u];
    }
    for(int i=1; i<=n; ++i)if(in[i]==1)que[++R]=i;
    int cnt=0,ans=0;
    while(L<=R) {
        int now=que[L++],x=-1;
        if(flag[now])continue;
        for(int i=0; i<Q[now].size(); ++i) {
            int To=Q[now][i];
            if(!flag[To]) {x=To;break;}
        }
        if(~x&&in[x]<=2) {
            Del(now);
            Del(x);
            ans++;
            cnt+=2;
        }else {
            Del(now);
            ans+=2;
            cnt++;
        }
    }
    printf("%d",ans);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 8856kb

input:

5
1 2
2 3
3 4
3 5

output:

4

result:

ok 1 number(s): "4"

Test #2:

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

input:

4
1 2
2 3
3 4

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: -100
Wrong Answer
time: 62ms
memory: 16552kb

input:

196666
32025 108673
181779 17115
162570 134230
93003 39673
89144 1269
185919 154408
34896 65369
35860 44720
55698 1390
45520 189805
147867 124511
135459 132555
87303 18031
176314 59695
33352 130640
87272 39716
35749 108807
143493 94486
126512 116598
40212 70895
132216 80855
22241 188737
150354 17346...

output:

169517

result:

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