QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#528579 | #6396. Puzzle: Kusabi | OneWan# | WA | 1ms | 3824kb | C++23 | 3.3kb | 2024-08-23 16:27:27 | 2024-08-23 16:27:29 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
vector<int>v[1000005];
int type[100005]; //1 tong 2 duan 3 chang
pair<int,int>hv[100005];
int dep[100005];
set<pair<int,int>>res;
bool cmp(int x,int y){
return dep[x]<dep[y];
}
void dfs(int u,int fa){
dep[u]=dep[fa]+1;
vector<int>t;
vector<int>d;
vector<int>c;
if(type[u]!=0){
if(type[u]==1){
t.push_back(u);
}else if(type[u]==2){
d.push_back(u);
}else if(type[u]==3){
c.push_back(u);
}
}
for(auto x:v[u]){
if(x==fa)continue;
dfs(x,u);
if(hv[x].first!=0){
if(hv[x].first==1){
t.push_back(hv[x].second);
}else if(hv[x].first==2){
d.push_back(hv[x].second);
}else if(hv[x].first==3){
c.push_back(hv[x].second);
}
}
}
sort(begin(c),end(c),cmp);
sort(begin(d),end(d),cmp);
sort(begin(t),end(t),cmp);
// for(auto x:t){
// cout<<dep[x]<<'\n';
// }
int less=0;
int lessid=0;
for(int i=0;i<t.size();i++){
if(i+1<t.size()&&dep[t[i]]==dep[t[i+1]]){
res.insert({t[i],t[i+1]});
i++;
}else{
less++;
hv[u].first=1;
hv[u].second=t[i];
}
}
if(less>=2){;
cout<<"NO\n";
exit(0);
}
int l=0,r=0;
for(l=0,r=0;l<c.size();l++){
while(r<d.size()&&dep[c[l]]<=dep[d[r]]){
if(hv[u].first!=0){
cout<<"No\n";
exit(0);
}else{
hv[u].first=2;
hv[u].second=d[r];
}
r++;
}
if(r<d.size()){
res.insert({c[l],d[r]});
r++;
}else{
if(hv[u].first!=0){
// cout<<u<<"?\n";
cout<<"No\n";
exit(0);
}else{
// cout<<u<<"????"<<l<<'\n';
hv[u].first=3;
hv[u].second=c[l];
}
}
}
// cout<<u<<"|"<<c.size()<<" "<<d.size()<<'\n';
// if(u==2){
// cout<<d[0]<<"x"<<d[1]<<'\n';
// }
for(r;r<d.size();r++){
if(hv[u].first!=0){
// cout<<u<<"?\n";
//cout<<c.size()<<" "<<d.size()<<'\n';
cout<<"No\n";
exit(0);
}else{
//cout<<u<<"????"<<l<<'\n';
hv[u].first=2;
hv[u].second=d[r];
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
for(int i=1;i<=n-1;i++){
int x,y;
cin>>x>>y;
v[x].push_back(y);
v[y].push_back(x);
string sx;
cin>>sx;
if(sx=="Tong"){
type[x]=1;
}else if(sx=="Duan"){
type[x]=2;
}else if(sx=="Chang"){
type[x]=3;
}else{
type[x]=0;
}
// cout<<x<<"?"<<type[x]<<'\n';
// cout<<u<<"?"<<type[u]<<'\n';
}
dfs(1,0);
cout<<"YES\n";
for(auto [x,y]:res){
cout<<x<<" "<<y<<'\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3824kb
input:
8 2 1 - 3 1 - 4 2 Tong 5 2 Tong 6 3 Duan 7 3 - 8 7 Chang
output:
YES 4 5 8 6
result:
ok Correct.
Test #2:
score: 0
Accepted
time: 1ms
memory: 3604kb
input:
10 2 1 Duan 3 2 Duan 4 2 - 5 4 Chang 6 2 Chang 7 1 Duan 8 6 Tong 9 6 Tong 10 3 Chang
output:
YES 5 7 6 2 8 9 10 3
result:
ok Correct.
Test #3:
score: -100
Wrong Answer
time: 0ms
memory: 3624kb
input:
2 2 1 Tong
output:
YES
result:
wrong answer Only odd number of marked vertices to match.