QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#371033 | #6396. Puzzle: Kusabi | ricofx# | WA | 0ms | 12740kb | C++17 | 3.6kb | 2024-03-29 21:16:56 | 2024-03-29 21:17:16 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define enl putchar('\n')
const int MAXN = 1e5 + 5;
typedef pair<int, int> pii;
int n, m;
string s;
struct EG {
int to, nxt;
}e[MAXN];
int head[MAXN], etot;
inline void add(int u, int v) {
e[etot] = { v,head[u] };
head[u] = etot++;
}
struct FFF {
vector<int>v;
priority_queue<int>q1, q2;
}f[MAXN];
int fa[MAXN], dep[MAXN];
vector<pii>ans;
bool bfs() {
vector<int>vec;
queue<int>q;
q.push(1);
while (!q.empty()) {
int u = q.front(); q.pop();
vec.push_back(u);
for (int i = head[u]; ~i; i = e[i].nxt) {
q.push(e[i].to);
dep[e[i].to] = dep[u] + 1;
}
}
reverse(vec.begin(), vec.end());
for (int u : vec) {
int sizq1 = f[u].q1.size(), sizq2 = f[u].q2.size();
if (abs(sizq1 - sizq2) > 1)return !printf("NO\n");
vector<int>vq1,vq2;
while(!f[u].q1.empty()){vq1.push_back(f[u].q1.top());f[u].q1.pop();}
while(!f[u].q2.empty()){vq2.push_back(f[u].q2.top());f[u].q2.pop();}
int rst=-1;
if(sizq1>sizq2){
for(int i=0,j=0;i<sizq2;++i,++j){
if(vq1[j] >= vq2[i]) {
if(rst == -1){
rst = vq1[i];
++j;
if(vq1[j] >= vq2[i])return !printf("NO\n");
}else{
return !printf("NO\n");
}
}
}
}else{
reverse(vq1.begin(),vq1.end());
reverse(vq2.begin(),vq2.end());
for(int i=0,j=0;i<sizq1;++i,++j){
if(vq1[i] >= vq2[j]) {
if(rst == -1){
rst = vq2[i];
++j;
if(vq1[i] >= vq2[j])return !printf("NO\n");
}else{
return !printf("NO\n");
}
}
}
}
if(sizq1 >sizq2){
if( rst!=-1 )
f[u].q1.push(rst);
else
f[u].q1.push(vq1[sizq1-1]);
// if(sizq1>sizq2)
}
if(sizq1<sizq2){
if(rst!=-1)
f[u].q2.push(rst);
else
f[u].q2.push(vq2[sizq2-1]);
}
if (f[u].v.size() + f[u].q1.size() + f[u].q2.size() > 1)
return !printf("NO\n");
if (!f[u].v.empty())f[fa[u]].v.push_back(f[u].v[0]);
if (!f[u].q1.empty())f[fa[u]].q1.push(f[u].q1.top());
if (!f[u].q2.empty())f[fa[u]].q2.push(f[u].q2.top());
if (f[fa[u]].v.size() == 2) {
int x = f[fa[u]].v.back(); f[fa[u]].v.pop_back();
int y = f[fa[u]].v.back(); f[fa[u]].v.pop_back();
ans.push_back({ x,y });
}
}
if (f[1].v.size() || f[1].q1.size() || f[1].q2.size())
return !printf("NO\n");
return true;
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
memset(head, -1, sizeof(head));
cin >> n;
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v >> s;
fa[u] = v;
add(v, u);
if (s == "Tong")f[u].v.push_back(u);
else if (s == "Duan")f[u].q1.push(u);
else if (s == "Chang")f[u].q2.push(u);
}
// for(int i=1;i<=n;++i,enl)
// for(int j=0;j<3;++j)
// // if(!f[i][j].empty())
// printf("%d ",f[i][j].size());
if (bfs()) {
printf("YES\n");
for (auto [x, y] : ans)
printf("%d %d\n", x, y);
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 12740kb
input:
8 2 1 - 3 1 - 4 2 Tong 5 2 Tong 6 3 Duan 7 3 - 8 7 Chang
output:
YES 5 4
result:
wrong output format Unexpected end of file - int32 expected