QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#884008 | #9556. The Hanged Man | Nimi_Sora | WA | 30ms | 3712kb | C++23 | 1.7kb | 2025-02-05 20:48:09 | 2025-02-05 20:48:09 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=3e5+7,mod=998244353;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
vector<ll>e[N];
vector<pll>ans;
ll dep[N];
ll root;
bool f;
ll dfs(ll now,ll fa){
dep[now]=dep[fa]+1;
vector<pll>ve;
for(auto it:e[now]){
if(it==fa)continue;
ll x=dfs(it,now);
ve.emplace_back(dep[x],x);
}
if(now==root){
sort(all(ve));
for(int i=0;i+1<ve.size();i+=2){
ans.emplace_back(ve[i].se,ve[i+1].se);
}
if(ve.size()&1){
ll leaf=ve.back().se;
if(dep[leaf]-dep[root]>=2){
ans.emplace_back(leaf,root);
}else f=0;
}
return 0;
}
if(e[now].size()==1)return now;
else if(e[now].size()==2)return ve[0].se;
sort(all(ve));
for(int i=0;i+1<ve.size();i+=2){
ans.emplace_back(ve[i].se,ve[i+1].se);
}
if(ve.size()&1){
return ve.back().se;
}else{
return now;
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int tt;
cin>>tt;
while(tt--){
ll n;cin>>n;
for(int i=1;i<=n;i++)e[i].clear();
ans.clear();
root=1;f=1;
for(int i=0;i<n-1;i++){
ll x,y;cin>>x>>y;
e[x].emplace_back(y),e[y].emplace_back(x);
}
for(int i=1;i<=n;i++)if(e[i].size()&1==0){root=i;break;}
dep[root]=0;
dfs(root,root);
if(f){
cout<<ans.size()<<'\n';
for(auto [it1,it2]:ans)cout<<it1<<' '<<it2<<'\n';
}else cout<<"-1\n";
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3584kb
input:
3 4 1 2 2 3 2 4 7 1 2 1 3 1 4 4 5 4 6 4 7 6 1 2 2 3 2 4 1 5 5 6
output:
-1 3 5 6 2 3 7 1 2 3 4 2 6
result:
ok Good Job! (3 test cases)
Test #2:
score: 0
Accepted
time: 1ms
memory: 3584kb
input:
3 6 1 2 1 3 1 4 4 5 4 6 2 1 2 2 2 1
output:
-1 -1 -1
result:
ok Good Job! (3 test cases)
Test #3:
score: 0
Accepted
time: 29ms
memory: 3584kb
input:
100000 3 1 3 2 1 3 2 3 1 2 3 2 3 1 3 3 2 1 1 3 3 1 2 2 3 3 1 3 2 3 3 2 1 1 3 3 2 3 1 2 3 2 3 1 3 3 2 1 1 3 3 2 3 1 2 3 1 3 2 3 3 1 3 2 1 3 2 3 1 2 3 2 3 1 3 3 1 3 2 1 3 1 2 2 3 3 1 3 2 3 3 2 1 1 3 3 1 2 2 3 3 1 3 2 3 3 1 3 2 1 3 2 3 1 2 3 1 3 2 3 3 1 3 2 1 3 2 3 1 2 3 1 3 2 3 3 2 1 1 3 3 2 3 1 2 3 2...
output:
1 2 3 1 3 1 1 2 1 1 2 3 1 3 1 1 2 1 1 2 3 1 3 1 1 2 1 1 2 3 1 3 1 1 2 1 1 2 3 1 3 1 1 2 1 1 2 3 1 3 1 1 2 1 1 2 3 1 3 1 1 2 1 1 2 3 1 3 1 1 2 1 1 2 3 1 3 1 1 2 1 1 2 3 1 3 1 1 2 1 1 2 3 1 3 1 1 2 1 1 2 3 1 3 1 1 2 1 1 2 3 1 3 1 1 2 1 1 2 3 1 3 1 1 2 1 1 2 3 1 3 1 1 2 1 1 2 3 1 3 1 1 2 1 1 2 3 1 3 1 ...
result:
ok Good Job! (100000 test cases)
Test #4:
score: 0
Accepted
time: 26ms
memory: 3712kb
input:
75000 4 3 1 2 1 1 4 4 3 1 2 4 1 2 4 2 1 1 3 3 4 4 1 4 2 1 3 4 4 2 1 3 2 1 4 4 3 2 2 4 1 2 4 2 3 3 4 1 2 4 3 4 2 4 1 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 1 1 3 4 1 2 4 1 2 3 2 3 4 2 5 2 4 5 3 1 2 3 5 2 4 1 4 5 1 3 5 1 3 4 2 3 4 2 5 2 3 4 5 1 -1 -1 -1 1 5 4 -1 2 4 5 3 1 1 5 1 1 4 1 ...
result:
wrong answer Participant did not find the answer. (test case 33)