QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#126865 | #6396. Puzzle: Kusabi | UNos_maricones# | WA | 24ms | 26956kb | C++20 | 3.8kb | 2023-07-19 08:30:50 | 2023-07-19 08:30:53 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std ;
#define ff first
#define ss second
#define pb push_back
typedef long long ll;
typedef pair<ll,int> ii;
typedef long double lf;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll SQ = 320;
const ll N = 3e5+5;
const ll mod = 998244353;
const ll oo = 1e14+5;
vector <int> g[N];
int fat[N];
int ty[N];
int tin[N], tout[N];
int tme = 0;
int visi[N];
vector <ii> ans;
vector <int> dep[N];
int depp[N];
int remchang[N], remduan[N];
int can = 1;
void dfs (int u, int d) {
tin[u] = tme++;
depp[u]=d;
if (ty[u] == 0) dep[d].pb(u);
for (auto &v : g[u]) {
dfs(v, d + 1);
}
tout[u] = tme++;
}
bool anc (int u, int v) {
return (tin[u] <= tin[v] && tout[v] <= tout[u]);
}
bool path (int u, int v) {
ans.pb({u, v});
while (!anc(u, v)) {
if (visi[u]) return false;
visi[u] = 1;
u = fat[u];
}
while (u != v) {
for (auto &e : g[u]) {
if (anc(e, v)) {
if (visi[e]) return false;
visi[e] = 1;
u = e;
break;
}
}
}
return true;
}
bool cmp ( int u, int v ) {
return depp[u] < depp[v];
}
void solve (int u) {
vector <int> dchang, dduan;
if (ty[u] == 1) dduan.pb(u);
if (ty[u] == 2) dchang.pb(u);
for (auto &v : g[u]) {
solve(v);
if (can == 0) return;
if (remchang[v] != -1) {
if (visi[v]) {
can = 0;
return;
}
dchang.pb(remchang[v]);
}
if (remduan[v] != -1) {
if (visi[v]) {
can = 0;
return;
}
dduan.pb(remduan[v]);
}
}
// cout << u << " has\n";
// for (auto &e : dchang) cout << e << ' ';
// cout << '\n';
// for (auto &e : dduan) cout << e << ' ';
// cout << '\n';
sort(dchang.begin(), dchang.end(), cmp);
sort(dduan.begin(), dduan.end(), cmp);
int sz1 = dchang.size();
int sz2 = dduan.size();
if (min(sz1, sz2) + 1 < max(sz1, sz2)) {
can = 0;
return;
}
if (sz1 <= sz2) {
int idx = sz2 - 1;
int unused = 0;
for (int i = sz1 - 1; i >= 0; --i) {
while (idx >= 0 && depp[dduan[idx]] >= depp[dchang[i]]) unused = idx, idx--;
if (idx < 0) {
can = 0;
break;
}
path(dchang[i], dduan[idx--]);
}
if (sz1 < sz2)
remduan[u] = dduan[unused];
}
else {
int idx = 0;
int unused = sz1-1;
for (int i = 0; i < sz2; ++i) {
while (idx < sz1 && depp[dchang[idx]] <= depp[dduan[i]]) unused = idx, idx++;
if (idx == sz1) {
can = 0;
break;
}
path(dchang[idx++], dduan[i]);
}
remchang[u] = dchang[unused];
}
}
int main(){
#ifdef LOCAL
freopen("input.txt","r",stdin);
#endif // LOCAL
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int n; cin >> n;
int tot = n-1;
ty[0]=3;
for (int i = 1; i < n; ++i) {
int x, pi; string ti;
cin >> x >> pi >> ti;
pi--;
g[pi].pb(i);
fat[i] = pi;
if (ti == "Tong") ty[i] = 0;
else if (ti == "Duan") ty[i] = 1;
else if (ti == "Chang") ty[i] = 2;
else tot--, ty[i] = 3;
}
dfs(0, 0);
for (int i = 0; i < N; ++i) {
if (dep[i].size() % 2) {
cout << "NO\n";
return 0;
}
for (int j = 0; j + 1 < dep[i].size(); j += 2) {
if (!path(dep[i][j], dep[i][j + 1])) {
cout << "NO\n";
return 0;
}
}
}
memset(remchang, -1, sizeof remchang);
memset(remduan, -1, sizeof remduan);
solve(0);
// cout << can << endl;
if (ans.size() < (tot+1)/2) cout << "NO\n";
else {
cout << "YES\n";
for (int i = 0; i < ans.size(); ++i) cout << ans[i].ff + 1 << ' ' << ans[i].ss + 1 << '\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 26956kb
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: 6ms
memory: 24916kb
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 8 9 10 3 6 2 5 7
result:
ok Correct.
Test #3:
score: 0
Accepted
time: 1ms
memory: 24872kb
input:
2 2 1 Tong
output:
NO
result:
ok Correct.
Test #4:
score: -100
Wrong Answer
time: 24ms
memory: 26416kb
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:
NO
result:
wrong answer Jury has answer but participant has not.