QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#109234 | #6396. Puzzle: Kusabi | wsyear | WA | 9ms | 20652kb | C++17 | 3.4kb | 2023-05-27 23:15:32 | 2023-05-27 23:15:36 |
Judging History
answer
#include <bits/stdc++.h>
#ifdef dbg
#define D(...) fprintf(stderr, __VA_ARGS__)
#define DD(...) D(#__VA_ARGS__ " = "), debug_helper::debug(__VA_ARGS__), D("\n")
#include "C:\Users\wsyear\Desktop\OI\templates\debug.hpp"
#else
#define D(...) ((void)0)
#define DD(...) ((void)0)
#endif
#define rep(i, j, k) for (int i = (j); i <= (k); ++i)
#define per(i, j, k) for (int i = (j); i >= (k); --i)
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
#define gc getchar
#define pc putchar
using ll = long long;
using pii = std::pair<int, int>;
using pll = std::pair<ll, ll>;
using namespace std;
template <class T = int> T read() {
T x = 0; bool f = 0; char ch = gc();
while (!isdigit(ch)) f = ch == '-', ch = gc();
while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ 48), ch = gc();
return f ? -x: x;
}
template <class T> void write(T x) {
if (x >= 0) { if (x > 9) write(x / 10); pc(x % 10 + 48); }
else { pc('-'); if (x < -9) write(-x / 10); pc(48 - x % 10); }
}
const int N = 100010;
int n, fa[N], a[N], dep[N];
string s[N];
vector<int> e[N];
vector<pii> ans;
set<pii> vd[N], vc[N];
vector<pii> vt[N], vec;
set<pii>::iterator it;
void dfs(int u) {
dep[u] = dep[fa[u]] + 1;
for (int v : e[u]) {
dfs(v);
for (auto x : vd[v]) vd[u].insert(x);
for (auto x : vt[v]) vt[u].emplace_back(x);
for (auto x : vc[v]) vc[u].insert(x);
vd[v].clear(), vt[v].clear(), vc[v].clear();
}
if (a[u] == 0) vd[u].insert(pii(dep[u], u));
else if (a[u] == 1) vt[u].emplace_back(pii(dep[u], u));
else if (a[u] == 2) vc[u].insert(pii(dep[u], u));
// DD(u, vt[u], vd[u], vc[u]), cerr.flush();
map<int, int> mp;
for (it = vd[u].end(); it != vd[u].begin();) {
it = prev(it);
int v = it -> se, d = it -> fi;
if (vc[u].empty() || (*prev(vc[u].end())).fi <= d);
else {
int w = (*vc[u].upper_bound(pii(d, n + 1))).se;
ans.emplace_back(pii(v, w));
mp[v] = 1, vc[u].erase(pii(dep[w], w));
}
}
for (auto [v, w] : mp) vd[u].erase(pii(dep[v], v));
// DD(u, vt[u], vd[u], vc[u]), cerr.flush();
mp.clear();
for (it = vc[u].begin(); it != vc[u].end();) {
int v = it -> se, d = it -> fi;
if (vd[u].empty() || (*vd[u].begin()).fi >= d);
else {
int w = (*prev(vd[u].lower_bound(pii(d, 0)))).se;
ans.emplace_back(pii(v, w));
mp[v] = 1, vd[u].erase(pii(dep[w], w));
}
it = next(it);
}
for (auto [v, w] : mp) vc[u].erase(pii(dep[v], v));
// DD(u, vt[u], vd[u], vc[u]), cerr.flush();
sort(ALL(vt[u])), vec.clear();
for (int i = 0; i < SZ(vt[u]);) {
if (i + 1 >= SZ(vt[u]) || vt[u][i].fi != vt[u][i + 1].fi) {
vec.emplace_back(vt[u][i]);
i += 1;
} else {
ans.emplace_back(vt[u][i].se, vt[u][i + 1].se);
i += 2;
}
}
swap(vt[u], vec);
// DD(u, vt[u], vd[u], vc[u]), cerr.flush();
}
int main() {
n = read(), a[1] = -1;
rep (i, 2, n) {
i = read(), fa[i] = read(), cin >> s[i];
if (s[i] == "Tong") a[i] = 1;
else if (s[i] == "Duan") a[i] = 0;
else if (s[i] == "Chang") a[i] = 2;
else if (s[i] == "-") a[i] = -1;
e[fa[i]].emplace_back(i);
}
dfs(1);
int sum = 0;
rep (i, 2, n) sum += (a[i] != -1);
if (SZ(ans) != sum / 2) puts("NO");
else {
puts("YES");
for (auto [u, v] : ans) write(u), pc(32), write(v), pc(10);
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 9ms
memory: 20652kb
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: 8ms
memory: 20652kb
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: 4ms
memory: 20572kb
input:
2 2 1 Tong
output:
YES
result:
wrong answer Only odd number of marked vertices to match.