QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#103914 | #6396. Puzzle: Kusabi | HEltim7 | WA | 29ms | 11768kb | C++23 | 2.6kb | 2023-05-07 20:40:02 | 2023-05-07 20:40:05 |
Judging History
answer
#include <algorithm>
#include <array>
#include <cassert>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <tuple>
#include <vector>
using namespace std;
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
#include <heltim7/debug>
#else
#define debug(...) 7
#endif
#define endl '\n'
using LL=long long;
constexpr int N=1e5+10;
vector<int> adj[N],equ[N];
int type[N],dep[N],id[N],ed[N],p[N],idx;
bool pass[N];
vector<pair<int,int>> ans;
void init(int u) {
id[u]=++idx;
for(int v:adj[u]) {
dep[v]=dep[u]+1;
init(v);
}
ed[u]=idx;
}
void no() {
cout<<"NO"<<endl;
exit(0);
};
void jump(int u,int v) {
if(dep[u]<dep[v]) swap(u,v);
if(type[u]==2&&type[v]==3) no();
ans.emplace_back(u,v);
while(u!=v) {
if(dep[u]<dep[v]) swap(u,v);
if(pass[u]) no();
pass[u]=1;
u=p[u];
}
};
int dfs(int u) {
vector<int> lng,sht,unpair;
auto push=[&](int x) {
if(type[x]==3) lng.emplace_back(x);
if(type[x]==2) sht.emplace_back(x);
};
for(int v:adj[u]) push(dfs(v));
push(u);
auto cmp=[&](int x,int y) { return dep[x]<dep[y]; };
sort(lng.begin(),lng.end(),cmp);
sort(sht.begin(),sht.end(),cmp);
debug(u,lng,sht);
for(int i=0,j=0;i<sht.size()||j<lng.size();) {
if(i>=sht.size()) unpair.emplace_back(lng[j++]);
else if(j>=lng.size()) unpair.emplace_back(sht[i++]);
else {
debug(dep[sht[i]],dep[lng[j]]);
if(dep[sht[i]]<dep[lng[j]]) jump(sht[i++], lng[j++]);
else unpair.emplace_back(lng[j++]);
}
}
if(unpair.size()>=2) no();
if(unpair.empty()) return 0;
return unpair.front();
}
void solve() {
int n;
cin>>n;
for(int i=2;i<=n;i++) {
cin>>p[i]>>p[i];
adj[p[i]].push_back(i);
string s;
cin>>s;
if(s=="Tong") type[i]=1;
else if(s=="Duan") type[i]=2;
else if(s=="Chang") type[i]=3;
}
dep[1]=1;
init(1);
for(int i=2;i<=n;i++) {
if(type[i]==1) equ[dep[i]].push_back(i);
}
for(int i=2;i<=n;i++) {
if(equ[i].size()&1) no();
sort(equ[i].begin(),equ[i].end(),[](int x,int y) {
return id[x]<id[y];
});
for(int j=1;j<equ[i].size();j+=2) {
jump(equ[i][j-1], equ[i][j]);
}
}
if(dfs(1)) no();
cout<<"YES"<<endl;
for(auto [x,y]:ans) cout<<x<<' '<<y<<endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(nullptr);
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 9600kb
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: 3ms
memory: 9908kb
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 8 9 10 3 6 2 5 7
result:
ok Correct.
Test #3:
score: 0
Accepted
time: 1ms
memory: 9064kb
input:
2 2 1 Tong
output:
NO
result:
ok Correct.
Test #4:
score: -100
Wrong Answer
time: 29ms
memory: 11768kb
input:
100000 2 1 Duan 3 1 Duan 4 3 - 5 4 Duan 6 3 - 7 4 Duan 8 4 - 9 8 - 10 7 Duan 11 9 - 12 7 Duan 13 7 Duan 14 8 Duan 15 13 - 16 10 Duan 17 11 Duan 18 12 - 19 1 Duan 20 5 Duan 21 4 Duan 22 14 Duan 23 16 - 24 22 Duan 25 16 Duan 26 13 - 27 13 - 28 17 - 29 5 Duan 30 22 - 31 23 - 32 9 Duan 33 5 - 34 30 Duan...
output:
NO
result:
wrong answer Jury has answer but participant has not.