QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#317693 | #7936. Eliminate Tree | one_god_and_two_dogs# | WA | 67ms | 37756kb | C++14 | 1.3kb | 2024-01-29 14:27:17 | 2024-01-29 14:27:17 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int M=1e6+5;
int n;
vector<int>Q[M];
int in[M];
bool flag[M];
struct info {
int x,y;
bool operator <(const info &_)const {
return y>_.y;
}
};
priority_queue<info>S;
int find(int now) {
int x=0;
for(int i=0; i<Q[now].size(); ++i) {
int To=Q[now][i];
if(!flag[To]) {x=To;break;}
}
return x;
}
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)
S.push((info)<%To,in[find(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];
}in[0]=1e9;
for(int i=1; i<=n; ++i)
if(in[i]==1) {
int x=find(i);
S.push((info)<%i,in[x]%>);
}
int cnt=0,ans=0;
while(!S.empty()) {
int now=S.top().x;S.pop();
if(flag[now])continue;
int x=find(now);
if(in[x]<=2) {
Del(now);
Del(x);
ans++;
}else {
Del(now);
ans+=2;
}
}
printf("%d",ans);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 28244kb
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: 29504kb
input:
4 1 2 2 3 3 4
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: -100
Wrong Answer
time: 67ms
memory: 37756kb
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:
159671
result:
wrong answer 1st numbers differ - expected: '138182', found: '159671'