QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#632426#7936. Eliminate TreeForever_Young#WA 155ms32212kbC++231.5kb2024-10-12 13:22:522024-10-12 13:22:53

Judging History

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

  • [2024-10-12 13:22:53]
  • 评测
  • 测评结果:WA
  • 用时:155ms
  • 内存:32212kb
  • [2024-10-12 13:22:52]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define data dataa
using LL=long long;
using ULL=unsigned long long;
using LD=long double;
const int N=int(2e5)+10;
set<int>g[N];
bool del[N];
int n;
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>q;
int main()
{
    scanf("%d",&n);
    rep(i,n-1)
    {
        int x,y;scanf("%d%d",&x,&y);
        g[x].insert(y);g[y].insert(x);
    }
    rep(i,n)if(g[i].size()==1)q.push(mp(g[*g[i].begin()].size(),i));
    int ans=0;
    bool flag=0;
    for(;!q.empty();)
    {
        int u=q.top().second;q.pop();
        if(del[u])continue;
        del[u]=1;
        int x=*g[u].begin();
        if(g[x].size()<=2)
        {
            del[x]=1;
            ans++;
            for(auto y:g[x])
            {
                if(y==u)continue;
                g[y].erase(x);
                if(g[y].size()==0){ans+=2;flag=1;break;}
                if(g[y].size()==1)q.push(mp(g[*g[y].begin()].size(),y));
                if(g[y].size()==2)
                {
                    for(auto t:g[y])if(g[t].size()==1)q.push(mp(g[y].size(),t));
                }
            }
            if(flag)break;
        }
        else
        {
            g[x].erase(u);
            ans+=2;
            if(g[x].size()==1)q.push(mp(g[*g[x].begin()].size(),x));
            if(g[x].size()==2)for(auto y:g[x])if(g[y].size()==1)q.push(mp(1,y));
        }
    }
    printf("%d\n",ans);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 13272kb

input:

4
1 2
2 3
3 4

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: -100
Wrong Answer
time: 155ms
memory: 32212kb

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:

154706

result:

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