QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#202022 | #6394. Turn on the Light | ucup-team870# | TL | 0ms | 0kb | C++14 | 2.6kb | 2023-10-05 18:31:45 | 2023-10-05 18:31:45 |
answer
#include <bits/stdc++.h>
#define rep(i,l,r) for(int i=l; i<=r; i++)
#define per(i,r,l) for(int i=r; i>=l; i--)
#define IOS {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);}
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int N = 100010;
char s[N];
int tag[N], deep[N], cnt;
P ret[N];
P ans[N];
vector<int> e[N];
void dfs(int u, int ff) {
deep[u] = deep[ff] + 1;
vector<P> t[3];
if (tag[u]) {
t[tag[u]-1].push_back(P(deep[u], u));
}
for (auto v: e[u]) {
dfs(v, u);
if (ret[v].first) {
t[ret[v].first-1].push_back(P(deep[ret[v].second], ret[v].second));
}
}
rep (i, 0, 2) sort(t[i].begin(), t[i].end());
//tong
int flag[3] = {0,0,0}, id = -1, i;
for (i = 0; i+1 < t[0].size();) {
if (t[0][i].first == t[0][i+1].first) ans[++cnt] = P(t[0][i].second, t[0][i+1].second), i += 2;
else id=t[0][i].second, i++, flag[0]++;
}
if (i < t[0].size()) id=t[0][i].second, flag[0]++;
int ss = t[1].size(), tt = t[2].size();
ss--, tt--;
//duan
if (t[1].size() > t[2].size()) {
for (i = tt; i >= 0; i--) {
while (ss >= 0 && t[1][ss].first >= t[2][i].first) {
id = t[1][ss].second;
ss--;
flag[1]++;
}
ans[++cnt] = P(t[1][ss].second, t[2][i].second);
ss--;
}
}
//chang
else if (t[1].size() < t[2].size()) {
for (i = ss; i >= 0; i--) {
while (tt >= 0 && t[2][tt].first <= t[1][i].first) {
id = t[2][tt].second;
tt--;
flag[2]++;
}
ans[++cnt] = P(t[2][tt].second, t[1][i].second);
tt--;
}
} else {
rep (i, 1, ss) {
if (t[1][i].first >= t[2][i].first) {
flag[2] = 2;
}
ans[++cnt] = P(t[1][i].second, t[2][i].second);
}
}
if (flag[0] + flag[2] + flag[1] > 1) {
cout <<"NO";
exit(0);
}
if (flag[0]) ret[u] = P(1, id);
if (flag[1]) ret[u] = P(2, id);
if (flag[2]) ret[u] = P(3, id);
}
int main() {
int n;
scanf("%d", &n);
rep (i, 1, n-1) {
int u, v;
scanf("%d%d", &u, &v);
scanf("%s", s);
e[v].push_back(u);
if (s[0] == 'T') tag[u] = 1;
if (s[0] == 'D') tag[u] = 2;
if (s[0] == 'C') tag[u] = 3;
}
dfs(1, 0);
cout <<"YES\n";
rep (i, 1, cnt) {
cout << ans[i].first <<' '<< ans[i].second<<'\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
3