QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#219119 | #6396. Puzzle: Kusabi | zyoobn | WA | 1ms | 3560kb | C++20 | 4.4kb | 2023-10-19 01:56:11 | 2023-10-19 01:56:12 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
void solve() {
int n;
cin >> n;
vector<int> a(n, -1);
vector g(n, vector<int>());
for (int i = 1; i < n; ++i) {
int u, v;
string s;
cin >> u >> v >> s;
--u, --v;
g[v].push_back(u);
if (s == "Tong") {
a[u] = 0;
} else if (s == "Duan") {
a[u] = 1;
} else if (s == "Chang") {
a[u] = 2;
}
}
bool ok = true;
vector<int> dep(n);
vector<array<int, 2>> ans;
auto dfs = [&] (auto dfs, int u) -> array<int, 2> {
if (!ok) {
return {-1, -1};
}
vector f(3, vector<int>());
if (a[u] != -1) {
f[a[u]].push_back(u);
}
for (auto &v : g[u]) {
dep[v] = dep[u] + 1;
auto [x, y] = dfs(dfs, v);
if (!ok) {
return {-1, -1};
}
if (x == -1) {
continue;
}
f[x].push_back(y);
}
bool have = false;
sort(f[0].begin(), f[0].end(), [&] (auto &x, auto &y) {
return dep[x] < dep[y];
});
array<int, 2> res{-1, -1};
for (int i = 0; i < f[0].size(); ++i) {
if (i + 1 >= f[0].size()) {
if (!have) {
have = true;
res = {0, f[0][i]};
} else {
ok = false;
return {-1, -1};
}
} else {
if (dep[f[0][i]] == dep[f[0][i + 1]]) {
ans.push_back({f[0][i], f[0][i + 1]});
++i;
} else {
if (!have) {
have = true;
res = {0, f[0][i]};
} else {
ok = false;
return {-1, -1};
}
}
}
}
if (abs((int)f[1].size() - (int)f[2].size()) >= 2) {
ok = false;
return {-1, -1};
}
if (f[1].size() >= f[2].size()) {
sort(f[1].begin(), f[1].end(), [&] (auto &x, auto &y) {
return dep[x] > dep[y];
});
sort(f[2].begin(), f[2].end(), [&] (auto &x, auto &y) {
return dep[x] > dep[y];
});
} else {
sort(f[1].begin(), f[1].end(), [&] (auto &x, auto &y) {
return dep[x] < dep[y];
});
sort(f[2].begin(), f[2].end(), [&] (auto &x, auto &y) {
return dep[x] < dep[y];
});
}
int l = 0, r = 0;
while (l < f[1].size() && r < f[2].size()) {
int x = f[1][l], y = f[2][r];
if (dep[x] < dep[y]) {
ans.push_back({x, y});
} else {
if (!have) {
have = true;
if (f[1].size() > f[2].size()) {
res = {1, x};
--r;
} else if (f[1].size() < f[2].size()) {
res = {2, y};
--l;
} else {
ok = false;
return {-1, -1};
}
} else {
ok = false;
return {-1, -1};
}
}
++l, ++r;
}
if (!have) {
if (f[1].size() > f[2].size()) {
res = {1, f[1][l]};
} else if (f[1].size() < f[2].size()) {
res = {2, f[2][r]};
}
} else {
if (f[1].size() != f[2].size()) {
ok = false;
return {-1, -1};
}
}
return res;
};
auto [x, y] = dfs(dfs, 0);
if (x != -1 || !ok) {
cout << x << ' ' << ok << endl;
cout << "NO" << '\n';
return;
}
cout << "YES" << '\n';
for (auto &[u, v] : ans) {
cout << u + 1 << ' ' << v + 1 << '\n';
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
// cin >> t;
while(t--) {
solve();
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3560kb
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: 3448kb
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: -100
Wrong Answer
time: 0ms
memory: 3424kb
input:
2 2 1 Tong
output:
0 1 NO
result:
wrong answer YES or NO expected in answer, but 0 found.