QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#185901 | #6396. Puzzle: Kusabi | UESTC_Guest_WiFi | WA | 30ms | 64556kb | C++17 | 2.9kb | 2023-09-22 19:38:42 | 2023-09-22 19:38:43 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using db = long double;
const int N = 2e5 + 50;
using pii = pair<int, int>;
vector<pii> ans;
int n;
int t[N];
vector<int> e[N];
vector<int> depnodes[N];
int dep[N], mx_dep;
multiset<pii> s[5][N];
void dfs(int u, int fa)
{
dep[u] = dep[fa] + 1;
depnodes[dep[u]].push_back(u);
for (auto v : e[u])
{
if (v == fa)
continue;
dfs(v, u);
}
}
int dp[N][4];
void work(int depth)
{
for (auto u : depnodes[depth])
{
for (auto v : e[u])
{
if (dep[v] < dep[u])
continue;
for (int i = 1; i <= 3; i++)
{
for (auto k : s[i][v])
s[i][u].insert(k);
s[i][v].clear();
}
}
s[t[u]][u].insert(make_pair(depth, u));
for (auto it = s[1][u].begin(); it != s[1][u].end();)
{
if (s[2][u].empty())
break;
auto it2 = s[2][u].lower_bound(*it);
if (it2 != s[2][u].begin())
{
it2--;
if (it2->first < it->first)
{
ans.emplace_back(it->second, it2->second);
it = s[1][u].erase(it);
s[2][u].erase(it2);
}
else
it = next(it);
}
else
it = next(it);
}
for (auto it = s[3][u].begin(); it != s[3][u].end();)
{
if (next(it) != s[3][u].end())
{
if (dep[it->second] == dep[next(it)->second])
{
ans.emplace_back(it->second, next(it)->second);
it = s[3][u].erase(it);
it = s[3][u].erase(it);
}
else
it = next(it);
}
else
break;
}
int sum_sz = 0;
for (int i = 1; i <= 3; i++)
sum_sz += s[i][u].size();
if ((sum_sz >= 2) || (u == 1 && sum_sz >= 1))
{
cout << "NO\n";
exit(0);
}
}
}
int main()
{
scanf("%d", &n);
int cnt_spec = 0;
for (int i = 1; i < n; i++)
{
int ni, pi;
char ti[10] = {0};
scanf("%d%d%s", &ni, &pi, ti);
t[ni] = (ti[0] == 'C' ? 1 : (ti[0] == 'D' ? 2 : (ti[0] == 'T' ? 3 : 0)));
e[ni].push_back(pi), e[pi].push_back(ni);
cnt_spec += (t[ni] > 0);
}
if (cnt_spec % 2)
return printf("NO\n"), 0;
dfs(1, 0);
mx_dep = *max_element(dep, dep + N);
for (int d = mx_dep; d >= 1; d--)
work(d);
printf("YES\n");
for (auto [x, y] : ans)
printf("%d %d\n", x, y);
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 4ms
memory: 61516kb
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: 4ms
memory: 61476kb
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 10 3 8 9 6 2 5 7
result:
ok Correct.
Test #3:
score: 0
Accepted
time: 4ms
memory: 62184kb
input:
2 2 1 Tong
output:
NO
result:
ok Correct.
Test #4:
score: -100
Wrong Answer
time: 30ms
memory: 64556kb
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.