QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#470736 | #6396. Puzzle: Kusabi | UESTC_DECAYALI# | WA | 33ms | 8172kb | C++17 | 3.2kb | 2024-07-10 16:06:52 | 2024-07-10 16:06:52 |
Judging History
answer
#include <bits/stdc++.h>
enum {
NONE = 0,
TONG,
CHANG,
DUAN,
};
void quit() {
std::cout << "NO\n";
exit(0);
}
struct label {
int type, depth, id;
};
int n;
std::vector<std::pair<int, int>> ans;
std::vector<std::vector<int>> ch;
std::vector<int> nt;
label dfs(int cur, int depth = 1) {
// std::cerr << "cur = " << cur << char(10);
std::vector<label> chang, duan, tong;
auto push_back = [&](label l) {
switch(l.type) {
case CHANG: chang.push_back(l); break;
case DUAN: duan.push_back(l); break;
case TONG: tong.push_back(l); break;
}
};
push_back({ nt[cur], depth, cur });
for(auto ch: ch[cur]) push_back(dfs(ch, depth + 1));
// std::cerr << "cc cur = " << cur << char(10);
// std::cerr << "tong.size() = " << tong.size() << ", duan.size() == " << duan.size() << ", chang.size() == " << chang.size() << char(10);
if(duan.size() > chang.size() + 1 || chang.size() > duan.size() + 1) quit();
if(tong.size() % 2 == 1 && duan.size() != chang.size()) quit();
if(tong.size() % 2 == 0) {
// std::cerr << "debug1 cur = " << cur << " \n";
for(int i = 0; i < tong.size(); i += 2) ans.emplace_back(tong[i].id, tong[i + 1].id);
std::sort( duan.begin(), duan.end(), [](label a, label b) { return a.depth < b.depth; });
std::sort(chang.begin(),chang.end(), [](label a, label b) { return a.depth < b.depth; });
if(duan.size() == chang.size()) {
for(int i = 0; i < duan.size(); ++i) {
if(duan[i].depth >= chang[i].depth) quit();
ans.emplace_back(duan[i].id, chang[i].id);
}
return { NONE, 0, 0 };
} else
if(duan.size() < chang.size()) {
label res = {NONE, 0, 0};
for(int i = 0, j = 0; i < duan.size(); ++i) {
while(duan[i].depth >= chang[j].depth) {
if(res.type == NONE) res = chang[j++];
else quit();
}
ans.emplace_back(duan[i].id, chang[j++].id);
}
if(res.type == NONE) res = chang.back();
return res;
} else {
label res = {NONE, 0, 0};
for(int i = chang.size() - 1, j = duan.size() - 1; ~i; --i) {
while(duan[j].depth >= chang[i].depth) {
if(res.type == NONE) res = duan[j--];
else quit();
}
ans.emplace_back(duan[j--].id, chang[i].id);
}
if(res.type == NONE) res = duan.front();
return res;
}
} else {
// std::cerr << "debug2 cur = " << cur << " \n";
for(int i = 0; i < duan.size() && i < chang.size(); ++i) {
ans.emplace_back(duan[i].id, chang[i].id);
}
for(int i = 1; i < tong.size(); i += 2) ans.emplace_back(tong[i].id, tong[i + 1].id);
return tong.front();
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin >> n;
ch.resize(n + 1), nt.resize(n + 1);
for(int i = 2, _, p; i <= n; ++i) {
std::string s;
std::cin >> _ >> p >> s;
if(s == "Tong") nt[i] = TONG; else
if(s == "Chang") nt[i] = CHANG; else
if(s == "Duan") nt[i] = DUAN; else
nt[i] = NONE;
ch[p].push_back(i);
}
if(dfs(1).type != NONE) quit();
std::cout << "YES\n";
for(auto [a, b]: ans) std::cout << a << ' ' << b << char(10);
return 0;
}
/*
8
2 1 -
3 1 -
4 2 Tong
5 2 Tong
6 3 Duan
7 3 -
8 7 Chang
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
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3624kb
input:
8 2 1 - 3 1 - 4 2 Tong 5 2 Tong 6 3 Duan 7 3 - 8 7 Chang
output:
YES 4 5 6 8
result:
ok Correct.
Test #2:
score: 0
Accepted
time: 0ms
memory: 3616kb
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 3 10 8 9 2 6 7 5
result:
ok Correct.
Test #3:
score: 0
Accepted
time: 0ms
memory: 3632kb
input:
2 2 1 Tong
output:
NO
result:
ok Correct.
Test #4:
score: -100
Wrong Answer
time: 33ms
memory: 8172kb
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 38802 88119 10006 93143 76473 31531 15988 42604 6569 30148 2008 11412 46703 64525 70618 71289 47748 81204 20514 42353 46131 97634 82155 83516 62410 66867 9975 15712 3205 4978 5622 83026 10902 48783 30893 82167 65878 93809 33377 66951 66936 94549 64411 79128 8453 22475 36857 54702 629...
result:
wrong answer Pair 84075-21604 symbol not satisfied.