QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#220754 | #6307. Chase Game 2 | Young | WA | 5ms | 5960kb | C++14 | 1.6kb | 2023-10-20 19:42:07 | 2023-10-20 19:42:07 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
vector<int>ve[100005];
vector<int>sum;
int ans[100005];
void solve(){
int n;
cin>>n;
sum.clear();
for(int i=1;i<=n;i++){
ve[i].clear();
}
for(int i=1;i<n;i++){
int x,y;
cin>>x>>y;
ve[x].push_back(y);
ve[y].push_back(x);
}
for(int i=1;i<=n;i++){
int g=ve[i].size();
if(g==n-1){
cout<<-1<<'\n';return ;
}
if(g==1){
ans[ve[i][0]]++;
}
}
// for(int i=1;i<=n;i++){
// for(auto j:ans[i]){
// cout<<j<<' ';
// }
// cout<<'\n';
// }
for(int i=1;i<=n;i++){
if(ans[i]!=0){
sum.push_back(ans[i]);
ans[i]=0;
}
}
int mm=0;
// for(auto i:sum) cout<<i<<' ';
// cout<<'\n';
while(sum.size()!=0){
sort(sum.begin(),sum.end());
int g=sum.size();
if(g==1){
sum[g-1]--;
mm++;
if(sum[g-1]==0) sum.pop_back();
}
else{
sum[g-1]--;sum[g-2]--;
int k1=sum[g-1],k2=sum[g-2];
sum.pop_back();
sum.pop_back();
if(k1>0){
sum.push_back(k1);
}
if(k2>0){
sum.push_back(k2);
}
mm++;
}
}
cout<<mm<<'\n';
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int t;
cin>>t;
while(t--){
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5784kb
input:
4 2 1 2 4 1 2 2 3 3 4 4 1 2 2 3 2 4 5 1 2 2 3 3 4 3 5
output:
-1 1 -1 2
result:
ok 4 number(s): "-1 1 -1 2"
Test #2:
score: -100
Wrong Answer
time: 5ms
memory: 5960kb
input:
10000 4 1 2 1 3 3 4 4 1 2 1 3 1 4 4 1 2 2 3 1 4 5 1 2 2 3 1 4 4 5 5 1 2 2 3 3 4 4 5 4 1 2 2 3 2 4 5 1 2 1 3 2 4 2 5 4 1 2 2 3 1 4 5 1 2 1 3 2 4 1 5 5 1 2 2 3 3 4 2 5 5 1 2 1 3 2 4 2 5 4 1 2 1 3 3 4 5 1 2 1 3 3 4 1 5 4 1 2 1 3 1 4 5 1 2 1 3 3 4 3 5 5 1 2 2 3 3 4 3 5 4 1 2 1 3 2 4 5 1 2 2 3 2 4 3 5 5 ...
output:
1 -1 1 1 1 -1 3 1 2 2 2 1 2 -1 2 2 1 2 2 1 1 1 -1 2 2 2 1 -1 1 1 2 1 1 -1 1 2 1 1 1 -1 2 1 2 2 2 1 1 1 -1 2 2 1 1 2 1 2 1 1 2 -1 -1 -1 3 2 2 1 1 1 2 2 2 -1 2 2 -1 1 1 -1 2 -1 -1 2 2 2 2 1 1 1 1 1 1 1 1 1 2 -1 2 1 2 -1 2 1 1 1 -1 2 -1 1 -1 -1 3 -1 2 1 2 2 1 1 1 1 2 1 1 1 1 -1 2 1 2 1 1 1 1 1 1 1 2 -1...
result:
wrong answer 7th numbers differ - expected: '2', found: '3'