QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#241652 | #6396. Puzzle: Kusabi | ucup-team902# | WA | 18ms | 8800kb | C++17 | 4.7kb | 2023-11-06 15:08:22 | 2023-11-06 15:08:23 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> G[100005];
int dep[100005];
int lst[100005];
int tmp[100005];
vector<pair<int,int>> ans;
bool cmp(int x, int y)
{
return dep[x] < dep[y];
}
void dfs(int x)
{
for (int v : G[x])
dep[v] = dep[x] + 1, dfs(v);
vector<int> s[3];
if (tmp[x] != 0)
s[tmp[x] - 1].push_back(x);
for (int v : G[x]) {
if (lst[v] == 0)
continue;
s[tmp[lst[v]] - 1].push_back(lst[v]);
}
sort(s[0].begin(), s[0].end(), cmp);
sort(s[1].begin(), s[1].end(), cmp);
sort(s[2].begin(), s[2].end(), cmp);
//cout << s[0].size() << " " << x << endl;
if ((s[0].size() + s[1].size() + s[2].size()) % 2 == 0) {
if (s[0].size() % 2 != 0) {
cout << "NO" << endl;
exit(0);
}
for (int i = 0; i < s[0].size(); i += 2) {
if (dep[s[0][i]] != dep[s[0][i + 1]]) {
cout << "NO" << endl;
exit(0);
}
ans.push_back({ s[0][i], s[0][i + 1] });
}
if (s[1].size() != s[2].size()) {
cout << "NO" << endl;
exit(0);
}
for (int i = 0; i< s[1].size(); ++i) {
if (dep[s[1][i]] >= dep[s[2][i]]) {
cout << "NO" << endl;
exit(0);
}
ans.push_back({ s[1][i], s[2][i] });
}
lst[x] = 0;
} else {
if (s[0].size() % 2 == 0) {
for (int i = 0; i < s[0].size(); i += 2) {
if (dep[s[0][i]] != dep[s[0][i + 1]]) {
cout << "NO" << endl;
exit(0);
}
ans.push_back({ s[0][i], s[0][i + 1] });
}
if (abs((int)s[1].size() - (int)s[2].size()) > 1) {
cout << "NO" << endl;
exit(0);
}
if(s[1].size()>s[2].size()){
set<pair<int, int>> now;
for(int v:s[1]){
now.insert({dep[v], v});
}
for(int v:s[2]){
auto t = now.lower_bound({dep[v],-0x3f3f3f3f});
if(t==now.begin()){
cout << "NO" << endl;
exit(0);
}
t--;
ans.push_back({
v,(*t).second
});
now.erase(t);
}
lst[x] = (*now.begin()).second;
}
if(s[1].size()<s[2].size()){
set<pair<int, int>> now;
for(int v:s[2]){
now.insert({dep[v], v});
}
for(int v:s[1]){
auto t = now.upper_bound({dep[v],0x3f3f3f3f});
if(t==now.end()){
cout << "NO" << endl;
exit(0);
}
ans.push_back({
v,(*t).second});
now.erase(t);
}
lst[x] = (*now.begin()).second;
}
} else {
if (s[1].size() != s[2].size()) {
cout << "NO" << endl;
exit(0);
}
for (int i = 0; i < s[1].size(); ++i) {
if (dep[s[1][i]] >= dep[s[2][i]]) {
cout << "NO" << endl;
exit(0);
}
ans.push_back({ s[1][i], s[2][i] });
}
int up = 0;
if((int)s[0].size()==1)
up = s[0][0];
for (int i = 0; i + 1 < s[0].size();i+=2){
if(dep[s[0][i]]!=dep[s[0][i+1]]&&up!=0){
cout << "NO" << endl;
exit(0);
}
else if(dep[s[0][i]]!=dep[s[0][i+1]]){
up = s[0][i];
i--;
}
else{
ans.push_back({s[0][i], s[0][i + 1]});
}
}
lst[x] = up;
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 1; i < n; ++i) {
int x, y;
string s;
cin >> x >> y >> s;
if (s[0] == 'T') {
tmp[x] = 1;
} else if (s[0] == 'D')
tmp[x] = 2;
else if (s[0] == 'C')
tmp[x] = 3;
G[y].push_back(x);
}
dfs(1);
if(lst[1]!=0){
cout << "NO" << endl;
return 0;
}
cout << "YES" << endl;
for (auto [x, y] : ans) {
cout << x << " " << y << endl;
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 6108kb
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: 1ms
memory: 6184kb
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: 1ms
memory: 6112kb
input:
2 2 1 Tong
output:
NO
result:
ok Correct.
Test #4:
score: -100
Wrong Answer
time: 18ms
memory: 8800kb
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.