QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#104738 | #6396. Puzzle: Kusabi | zbceyond | WA | 69ms | 16288kb | C++20 | 2.4kb | 2023-05-11 20:25:29 | 2023-05-11 20:25:48 |
Judging History
answer
#include<bits/stdc++.h>
#define rep(i, x, y) for(int i=x;i<=y;i++)
#define per(i, x, y) for(int i=x;i>=y;i--)
using namespace std;
#define int long long
const int N = 2e5 + 10;
const int mod = 998244353;
int n, flag[N],dep[N],g[N];
vector<array<int, 2>> e[N];
vector<array<int, 2>> ans;
bool ok=1;
void dfs(int u, int fa) {
array<vector<int>,4>tmp;
for (auto [v, mark]: e[u]) {
if (v == fa)continue;
flag[v] = mark;
dep[v]=dep[u]+1;
dfs(v, u);
if(g[v])tmp[flag[g[v]]].push_back(g[v]);
}
if(flag[u])tmp[flag[u]].push_back(u);
sort(tmp[1].begin(),tmp[1].end(),[&](int x,int y){
return dep[x]<dep[y];
});
sort(tmp[2].begin(),tmp[2].end(),[&](int x,int y){
return dep[x]<dep[y];
});
sort(tmp[3].begin(),tmp[3].end(),[&](int x,int y){
return dep[x]<dep[y];
});
vector<int>rem;
for(int i=0;i<tmp[3].size();i++){
if(i+1==tmp[3].size()){
rem.push_back(tmp[3][i]);
break;
}
int u=tmp[3][i],v=tmp[3][i+1];
if(dep[u]==dep[v]){
ans.push_back({u,v});
i++;
}else rem.push_back(u);
}
int j=0;
for(int i=0;i<tmp[1].size();i++){
if(j==tmp[2].size())rem.push_back(tmp[1][i]);
else{
auto u=tmp[1][i],v=tmp[2][j];
if(dep[u]>dep[v]){
ans.push_back({u,v});
j++;
}else{
rem.push_back(u);
}
}
}
while(j<tmp[2].size())rem.push_back(tmp[2][j]),j++;
if(rem.size()>1)ok=0;
if(rem.size()==1)g[u]=rem[0];
}
void solve() {
cin >> n;
int cnt = 0, x = 0, y = 0;
rep(i, 1, n - 1) {
int u, v;
string s;
cin >> u >> v >> s;
int mark = 0;
if (s == "Chang")mark = 1, y++;
if (s == "Duan")mark = 2, y--;
if (s == "Tong")mark = 3, x++;
e[u].push_back({v, mark});
e[v].push_back({u, mark});
cnt += (mark != 0);
}
if (cnt % 2 == 1 or x % 2 == 1 or y != 0)cout << "NO\n";
else {
dfs(1, 0);
if(not g[1] or not ok){
cout<<"YES\n";
for(auto [u,v]:ans)cout<<u<<" "<<v<<"\n";
}
else cout<<"NO\n";
}
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
int tc = 1;
//cin >> tc;
for (int i = 1; i <= tc; i++) {
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 8152kb
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: 2ms
memory: 8076kb
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 10 3 8 9 6 2 5 7
result:
ok Correct.
Test #3:
score: 0
Accepted
time: 2ms
memory: 8176kb
input:
2 2 1 Tong
output:
NO
result:
ok Correct.
Test #4:
score: -100
Wrong Answer
time: 69ms
memory: 16288kb
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:
YES 46666 56115 88119 38802 93143 10006 31531 76473 42604 15988 6569 30148 11412 2008 64525 46703 71289 70618 81204 47748 42353 20514 97634 46131 83516 82155 66867 62410 15712 9975 4978 3205 83026 5622 48783 10902 82167 30893 93809 65878 33377 66951 94549 66936 79128 64411 8453 22475 54702 36857 629...
result:
wrong output format Unexpected end of file - int32 expected