QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#135493 | #6396. Puzzle: Kusabi | UrgantTeam# | WA | 43ms | 13428kb | C++23 | 4.0kb | 2023-08-05 16:21:46 | 2023-08-05 16:21:48 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
int const maxn = 1e5 + 5;
int a[maxn], h[maxn];
vector < int > g[maxn];
vector < pair < int, int > > dp[maxn];
vector < pair < int, int > > ans;
int good(vector < pair < int, int > > A, vector < pair < int, int > > B) {
assert(A.size() == B.size());
for (int i = 0; i < A.size(); i++) {
if (A[i].first <= B[i].first) return 0;
}
return 1;
}
void dfs(int v) {
for (auto u : g[v]) {
h[u] = h[v] + 1;
dfs(u);
for (auto key : dp[u]) dp[v].push_back(key);
}
if (a[v]) dp[v].push_back({a[v], v});
vector < int > tong;
for (auto key : dp[v]) {
if (key.first == 2) tong.push_back(key.second);
}
sort(tong.begin(), tong.end(), [](int x, int y) {
return h[x] < h[y];
});
vector < pair < int, int > > add;
int i = 0;
while (i < tong.size()) {
if (i + 1 < (int)tong.size() && h[tong[i]] == h[tong[i + 1]]) {
ans.push_back({tong[i], tong[i + 1]});
i += 2;
} else {
add.push_back({2, tong[i]});
i++;
}
}
vector < pair < int, int > > big, small;
for (auto key : dp[v]) {
if (key.first == 1) big.push_back({h[key.second], key.second});
else if (key.first == 3) small.push_back({h[key.second], key.second});
}
sort(big.begin(), big.end());
sort(small.begin(), small.end());
if (abs((int)big.size() - (int)small.size()) > 1) {
cout << "NO\n";
exit(0);
}
if (big.size() == small.size()) {
for (int i = 0; i < big.size(); i++) {
if (big[i].first <= small[i].first) {
cout << "NO\n";
exit(0);
}
ans.push_back({big[i].second, small[i].second});
}
dp[v] = {};
} else if (big.size() > small.size()) {
int lef = -1, righ = big.size();
while (righ - lef > 1) {
int mid = (righ + lef) / 2;
vector < pair < int, int > > A, B = small;
for (int i = 0; i < big.size(); i++) if (i != mid) A.push_back(big[i]);
if (good(A, B)) lef = mid;
else righ = mid;
}
if (lef == -1) {
cout << "NO\n";
exit(0);
}
dp[v] = {{1, big[lef].second}};
vector < pair < int, int > > A, B = small;
for (int i = 0; i < big.size(); i++) if (i != lef) A.push_back(big[i]);
for (int i = 0; i < A.size(); i++) ans.push_back({A[i].second, B[i].second});
} else {
int lef = -1, righ = small.size();
while (righ - lef > 1) {
int mid = (righ + lef) / 2;
vector < pair < int, int > > A = big, B;
for (int i = 0; i < small.size(); i++) if (i != mid) B.push_back(small[i]);
if (good(A, B)) righ = mid;
else lef = mid;
}
if (righ == small.size()) {
cout << "NO\n";
exit(0);
}
dp[v] = {{3, small[righ].second}};
vector < pair < int, int > > A = big, B;
for (int i = 0; i < small.size(); i++) if (i != lef) B.push_back(small[i]);
for (int i = 0; i < A.size(); i++) ans.push_back({A[i].second, B[i].second});
}
for (auto key : add) dp[v].push_back(key);
if (dp[v].size() > 1) {
cout << "NO\n";
exit(0);
}
}
main() {
#ifdef HOME
freopen("input.txt", "r", stdin);
#endif // HOME
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, u, v;
cin >> n;
string type;
for (int i = 2; i <= n; i++) {
cin >> u >> v >> type;
assert(u == i);
g[v].push_back(u);
if (type == "Chang") a[i] = 1;
else if (type == "Tong") a[i] = 2;
else if (type == "Duan") a[i] = 3;
}
dfs(1);
if (dp[1].size()) cout << "NO\n";
else {
cout << "YES\n";
for (auto key : ans) cout << key.first << " " << key.second << '\n';
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 8180kb
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: 8236kb
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: 8164kb
input:
2 2 1 Tong
output:
NO
result:
ok Correct.
Test #4:
score: -100
Wrong Answer
time: 43ms
memory: 13428kb
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 answer Vertex 686 used twice.