QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#374420 | #6396. Puzzle: Kusabi | ricofx | WA | 1ms | 6848kb | C++17 | 4.0kb | 2024-04-02 14:11:53 | 2024-04-02 14:11:54 |
Judging History
answer
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
using namespace std;
#define fi first
#define se second
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
//std::mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
const int mod = 998244353;
const int N = 1e5 + 5;
int n, w[N], dep[N];
vector<int> G[N];
vector<pii> ans;
pii dfs(int u) {
vector<int> d[3];
for (int v : G[u]) {
dep[v] = dep[u] + 1;
auto t = dfs(v);
if (t.fi != 0) d[t.fi - 1].push_back(t.se);
}
if (w[u]) d[w[u] - 1].push_back(u);
for (int i = 0; i < 3; i++) sort(d[i].begin(), d[i].end(), [](int x, int y) {
return dep[x] < dep[y];
});
int cnt = 0, lst = 0, lp = 0, idx = 0;
for (int i = 0; i <= d[0].size(); i++) {
if ((i == d[0].size()) || (i != 0 && dep[d[0][i]] != dep[d[0][i - 1]])) {
for (int j = lp; j + 1 < i; j += 2) {
ans.push_back({d[0][j + 1], d[0][j]});
//cerr << d[0].size() << ' '<< j << ' ' << d[0][j + 1] << ' ' << d[0][j] << '\n';
}
if (lst & 1) idx = d[0][i - 1], ++cnt;
lst = 0, lp = i;
}
++lst;
}
if (abs((int)d[1].size() - (int)d[2].size()) + cnt > 1) {cout << "NO\n" << '\n' ; exit(0);}
int m = min(d[1].size(), d[2].size());
if (d[1].size() == d[2].size()) {
for (int i = 0; i < m; i++) if (dep[d[1][i]] >= dep[d[2][i]]) {cout << "NO\n"; exit(0);}
for (int i = 0; i < m; i++) ans.push_back({d[1][i], d[2][i]});
} else if (d[1].size() > d[2].size()) {
if (m == 0) return {2, d[1][0]};
vector<int> pre(m, 1), suf(m, 1);
pre[0] = (dep[d[1][0]] < dep[d[2][0]]);
for (int i = 1; i < m; i++) pre[i] = (pre[i - 1] && (dep[d[1][i]] < dep[d[2][i]]));
suf[m - 1] = (dep[d[1].back()] < dep[d[2].back()]);
for (int i = m - 2; i >= 0; i--) suf[i] = (suf[i + 1] && dep[d[2][i]] > dep[d[1][i + 1]]);
int pos = -1;
for (int i = 0; i < d[1].size(); i++) {
int check = ((i == 0 && suf[0]) || (i + 1 == d[1].size() && pre[m - 1]) ||
(i > 0 && i + 1 < d[1].size() && pre[i - 1] && suf[i]));
if (check) {
pos = i;
break;
}
}
if (pos == -1) {cout << "NO\n"; exit(0);}
for (int p = 0, q = 0; p < m; p++, q++) {
if (q == pos) ++q;
ans.push_back({d[2][p], d[1][q]});
}
return {2, d[1][pos]};
} else {
swap(d[1], d[2]);
if (m == 0) return {3, d[1][0]};
vector<int> pre(m, 1), suf(m, 1);
pre[0] = (dep[d[1][0]] > dep[d[2][0]]);
for (int i = 1; i < m; i++) pre[i] = (pre[i - 1] && (dep[d[1][i]] > dep[d[2][i]]));
suf[m - 1] = (dep[d[1].back()] > dep[d[2].back()]);
for (int i = m - 2; i >= 0; i--) suf[i] = (suf[i + 1] && dep[d[2][i]] < dep[d[1][i + 1]]);
int pos = -1;
for (int i = 0; i < d[1].size(); i++) {
int check = ((i == 0 && suf[0]) || (i + 1 == d[1].size() && pre[m - 1]) ||
(i > 0 && i + 1 < d[1].size() && pre[i - 1] && suf[i]));
if (check) pos = i;
}
if (pos == -1) {cout << "NO\n"; exit(0);}
for (int p = 0, q = 0; p < m; p++, q++) {
if (q == pos) ++q;
ans.push_back({d[2][p], d[1][q]});
}
return {3, d[1][pos]};
}
return cnt == 1 ? (pii){1, idx} : (pii){0, 0};
}
void MAIN() {
cin >> n;
for (int i = 2; i <= n; i++) {
int u, v;
static string t;
cin >> u >> v >> t;
G[v].push_back(u);
w[i] = t == "-" ? 0 : t == "Tong" ? 1 : t == "Duan" ? 2 : 3;
}
dfs(1);
cout << "YES\n";
for (auto [x, y] : ans) cout << x << ' ' << y << '\n';
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int T = 1;
//cin >> T;
while (T--) MAIN();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 6848kb
input:
8 2 1 - 3 1 - 4 2 Tong 5 2 Tong 6 3 Duan 7 3 - 8 7 Chang
output:
YES 5 4 6 8
result:
ok Correct.
Test #2:
score: 0
Accepted
time: 1ms
memory: 6680kb
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 9 8 2 6 7 5
result:
ok Correct.
Test #3:
score: -100
Wrong Answer
time: 1ms
memory: 6488kb
input:
2 2 1 Tong
output:
YES
result:
wrong answer Only odd number of marked vertices to match.