QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#471144 | #6396. Puzzle: Kusabi | UESTC_DebugSimulator | WA | 0ms | 5876kb | C++17 | 2.6kb | 2024-07-10 18:34:05 | 2024-07-10 18:34:06 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define lowbit(i) (i&-i)
#define rand() (myrand())
using namespace std;
mt19937 myrand(time(0));
const int maxn = 2e5 + 5;
int n, _, head[maxn], cnt;
int c[maxn], dep[maxn], jud, fa[maxn];
string ty;
vector<pair<int, int> >ans;
struct node{
int next, to;
}edge[maxn<<1];
void addedge(int from, int to) {
edge[++cnt].next = head[from];
edge[cnt].to = to;
head[from] = cnt;
}
bool cmp(int x, int y) {return dep[x] < dep[y];}
int dfs(int u, int p) {
if (jud) return 0;
dep[u] = dep[p] + 1;
vector<int>a[3];
if (c[u] != -1) a[c[u]].push_back(u);
for (int i = head[u] ; i ; i = edge[i].next) {
int v = edge[i].to;
if (v == p) continue;
int x = dfs(v, u);
if (c[x] != -1) a[c[x]].push_back(x);
}
sort(a[0].begin(), a[0].end(), cmp);
sort(a[1].begin(), a[1].end(), cmp);
sort(a[2].begin(), a[2].end(), cmp);
int len0 = a[0].size(), len1 = a[1].size(), len2 = a[2].size(), res = 0, cnt = 0, pos = 0;
while(pos < len0) {
vector<int>stk;
stk.push_back(a[0][pos]);
while(pos + 1 < len0 && dep[a[0][pos + 1]] == dep[a[0][pos]]) {
pos++;
stk.push_back(a[0][pos]);
}
int tot = stk.size();
// if (tot >= 2) for (int i = 0 ; i < tot ; i += 2) ans.push_back({stk[i], stk[i + 1]});
if (tot&1) {
cnt++;
res = stk[tot - 1];
}
pos++;
}
int i, j;
if (len1 < len2) {
for (i = len2 - 1, j = len1 - 1 ; i >= 0 && j >= 0 ; --i) {
if (dep[a[1][j]] > dep[a[2][i]]) {
ans.push_back({a[1][j], a[2][i]});
j--;
}
else {
res = a[2][i];
cnt++;
}
}
if (!i) res = a[2][i], cnt++;
if (!j) res = a[1][j], cnt++;
}
else {
for (i = 0, j = 0 ; i < len1 && j < len2 ; ++i) {
if (dep[a[1][i]] > dep[a[2][j]]) {
ans.push_back({a[1][i], a[2][j]});
j++;
}
else {
res = a[1][i];
cnt++;
}
}
if (i == len1 - 1) res = a[1][i], cnt++;
if (j == len2 - 1) res = a[2][j], cnt++;
}
if (cnt > 1) {
jud = 1;
return 0;
}
return res;
}
void solve() {
cin >> n;
for (int i = 0 ; i <= n ; ++i) c[i] = -1;
for (int i = 1 ; i < n ; ++i) {
int x, p;
cin >> x >> p >> ty;
fa[x] = p;
if (ty[0] != '-') {
if (ty[0] == 'T') c[x] = 0;
if (ty[0] == 'C') c[x] = 1;
if (ty[0] == 'D') c[x] = 2;
}
addedge(x, p);
addedge(p, x);
}
if (dfs(1, 0)) jud = 1;
if (jud) {
cout << "NO" << '\n';
return;
}
cout << "YES" << '\n';
for (auto [x, y] : ans) {
cout << x << ' ' << y << '\n';
}
}
signed main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 5876kb
input:
8 2 1 - 3 1 - 4 2 Tong 5 2 Tong 6 3 Duan 7 3 - 8 7 Chang
output:
YES 8 6
result:
wrong output format Unexpected end of file - int32 expected