QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#528623 | #6399. Classic: Classical Problem | OneWan | WA | 2ms | 3852kb | C++23 | 4.7kb | 2024-08-23 17:06:11 | 2024-08-23 17:06:11 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
vector<int> v[1000005];
int type[100005]; //1 tong 2 duan 3 chang
pair<int, int> hv[100005];
int dep[100005];
set<pair<int, int>> res;
bool cmp(int x, int y)
{
return dep[x] < dep[y];
}
void dfs(int u, int fa)
{
dep[u] = dep[fa] + 1;
vector<int> t;
vector<int> d;
vector<int> c;
if (type[u] != 0) {
if (type[u] == 1) {
t.push_back(u);
} else if (type[u] == 2) {
d.push_back(u);
} else if (type[u] == 3) {
c.push_back(u);
}
}
for (auto x : v[u]) {
if (x == fa) continue;
dfs(x, u);
if (hv[x].first != 0) {
if (hv[x].first == 1) {
t.push_back(hv[x].second);
} else if (hv[x].first == 2) {
d.push_back(hv[x].second);
} else if (hv[x].first == 3) {
c.push_back(hv[x].second);
}
}
}
sort(begin(c), end(c), cmp);
sort(begin(d), end(d), cmp);
sort(begin(t), end(t), cmp);
int less = 0;
int lessid = 0;
//cout<<u<<" "<<t.size()<<'\n';
for (int i = 0; i < t.size(); i++) {
if (i + 1 < t.size() && dep[t[i]] == dep[t[i + 1]]) {
res.insert({t[i], t[i + 1]});
i++;
} else {
less++;
if (hv[u].first != 0) {
cout << "NO\n";
exit(0);
}
hv[u].first = 1;
hv[u].second = t[i];
}
}
int l = 0, r = 0;
// reverse(begin(c),end(c));
//reverse(begin(d),end(d));
//1 tong 2 duan 3 chang
if (c.size() > d.size()) {
for (l = 0, r = 0; l < d.size(); l++) {
while (r < c.size() && d[l] >= c[r]) {
if (hv[u].first != 0) {
cout << "NO\n";
exit(0);
} else {
hv[u].first = 3;
hv[u].second = c[r];
}
r++;
}
if (r < c.size()) {
res.insert({c[r], d[l]});
r++;
} else {
if (hv[u].first != 0) {
cout << "NO\n";
exit(0);
} else {
hv[u].first = 2;
hv[u].second = d[r];
}
}
}
for (; r < c.size(); r++) {
if (hv[u].first != 0) {
cout << "NO\n";
exit(0);
} else {
hv[u].first = 3;
hv[u].second = c[r];
}
}
} else { // c.size( )<d.size()
//1 tong 2 duan 3 chang
reverse(begin(c), end(c));
reverse(begin(d), end(d));
for (l = 0, r = 0; l < c.size(); l++) {
while (r < d.size() && dep[c[l]] <= dep[d[r]]) {
if (hv[u].first != 0) {
cout << "No\n";
exit(0);
} else {
hv[u].first = 2;
hv[u].second = d[r];
}
r++;
}
if (r < d.size()) {
res.insert({c[l], d[r]});
r++;
} else {
if (hv[u].first != 0) {
cout << "No\n";
exit(0);
} else {
hv[u].first = 3;
hv[u].second = c[l];
}
}
}
//cout<<u<<" "<<d.size()<<" "<<c.size()<<" "<<hv[u].first<<" "<<'\n';
for (r; r < d.size(); r++) {
if (hv[u].first != 0) {
// cout<<u<<"?\n";
cout << "No\n";
exit(0);
} else {
hv[u].first = 2;
hv[u].second = d[r];
}
}
}
if (u == 1 && hv[u].first != 0) {
cout << "No\n";
exit(0);
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n - 1; i++) {
int x, y;
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
string sx;
cin >> sx;
if (sx == "Tong") {
type[x] = 1;
} else if (sx == "Duan") {
type[x] = 2;
} else if (sx == "Chang") {
type[x] = 3;
} else {
type[x] = 0;
}
}
dfs(1, 0);
cout << "YES\n";
for (auto [x, y] : res) {
cout << x << " " << y << '\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 2ms
memory: 3852kb
input:
3 2 3 0 2 3 5 2 3 4 3 5 0 2 3
output:
YES
result:
wrong answer 1st lines differ - expected: '1 2', found: 'YES'