QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#470773 | #6396. Puzzle: Kusabi | UESTC_DECAYALI# | WA | 27ms | 7912kb | C++17 | 3.5kb | 2024-07-10 16:18:34 | 2024-07-10 16:18:34 |
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();
std::sort(tong.begin(), tong.end(), [](label a, label b) { return a.depth < b.depth; });
if(tong.size() % 2 == 0) {
// std::cerr << "debug1 cur = " << cur << " \n";
for(int i = 0; i < tong.size(); i += 2) {
if(tong[i].depth != tong[i + 1].depth) quit();
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);
}
label res = { NONE, 0, 0 };
for(int i = 0; i < tong.size(); ) {
if(i == tong.size() - 1 || tong[i].depth != tong[i + 1].depth) {
res = tong[i++];
} else {
ans.emplace_back(tong[i].id, tong[i + 1].id);
i += 2;
}
}
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: 3604kb
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: 3536kb
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: 3620kb
input:
2 2 1 Tong
output:
NO
result:
ok Correct.
Test #4:
score: -100
Wrong Answer
time: 27ms
memory: 7912kb
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.