QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#470739 | #6396. Puzzle: Kusabi | UESTC_Snow_Halation# | WA | 9ms | 56956kb | C++14 | 4.0kb | 2024-07-10 16:08:20 | 2024-07-10 16:08:21 |
Judging History
answer
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#define FOR() ll le=e[u].size();for(ll i=0;i<le;i++)
#define QWQ cout<<"QwQ\n";
#define ll long long
#include <vector>
#include <queue>
#include <map>
typedef long double db;
using namespace std;
const ll N=501010;
const ll qwq=303030;
const ll inf=0x3f3f3f3f3f;
const db eps = 1e-8;
int n;
char s[12];
int dp[N];
struct E{
int dep,id;
};
inline bool cmp(E A,E B) { return A.dep < B.dep; }
vector <int> e[N];
int fa[N];
vector <E> t[N];
vector <E> c[N];
vector <E> d[N];
int cnt;
int st1[N],st2[N];
bool flag = 1;
inline ll read() {
ll sum = 0, ff = 1; char c = getchar();
while(c<'0' || c>'9') { if(c=='-') ff = -1; c = getchar(); }
while(c>='0'&&c<='9') { sum = sum * 10 + c - '0'; c = getchar(); }
return sum * ff;
}
void DFS(int u) {
for(int v : e[u]) {
if(v==fa[u]) continue;
DFS(v);
if(!flag) return ;
}
if(!flag) return ;
sort(t[u].begin(), t[u].end(), cmp);
sort(c[u].begin(), c[u].end(), cmp);
sort(d[u].begin(), d[u].end(), cmp);
int jiou = 0;
int sizt = t[u].size(), sizc = c[u].size(), sizd = d[u].size();
if(sizt&1) jiou++;
if((sizc+sizd)&1) jiou++;
if(jiou>2) { flag = 0; return ; }
if(abs(sizc-sizd)>=2) { flag = 0; return ; }
if(sizt&1) {
int now = 0;
while(1) {
if(now==sizt || now==sizt-1) break;
if(t[u][now].dep!=t[u][now+1].dep) {
t[fa[u]].push_back(t[u][now]);
now++;
}
else {
st1[++cnt] = t[u][now].id; st2[cnt] = t[u][now+1].id;
now+=2;
}
}
if(now==sizt-1) t[fa[u]].push_back(t[u][now]);
}
else {
for(int i=0;i<sizt;i+=2) {
if(t[u][i].dep!=t[u][i+1].dep) { flag = 0; return ; }
else {
st1[++cnt] = t[u][i].id; st2[cnt] = t[u][i+1].id;
}
}
}
if(sizc==sizd) {
for(int i=0;i<sizc;i++) {
if(c[u][i].dep<=d[u][i].dep) { flag = 0; return ; }
else {
st1[++cnt] = c[u][i].id; st2[cnt] = d[u][i].id;
}
}
}
if(sizc==sizd-1) {
int i = sizc-1, j = sizd-1;
while(1) {
if(i==-1 || j==-1) break;
if(c[u][i].dep<=d[u][j].dep) {
if(i==j) { flag = 0; return ; }
else {
d[fa[u]].push_back(d[u][j]);
j--;
}
}
else {
st1[++cnt] = c[u][i].id; st2[cnt] = d[u][j].id;
i--; j--;
}
}
if(j==0) d[fa[u]].push_back(d[u][j]);
}
if(sizd==sizc-1) {
int i = 0, j = 0;
while(1) {
if(i==sizc || j==sizd) break;
if(c[u][i].dep<=d[u][j].dep) {
if(i==j+1) { flag = 0; return ; }
else {
c[fa[u]].push_back(c[u][i]);
i++;
}
}
else {
st1[++cnt] = c[u][i].id; st2[cnt] = d[u][j].id;
i++; j++;
}
}
if(i==sizc-1) c[fa[u]].push_back(c[u][i]);
}
}
int main() {
int x,y;
n = read();
for(int i=1;i<n;i++) {
x = read(); y = read(); scanf("%s",s+1);
dp[x] = dp[y] + 1;
fa[x] = y;
e[x].push_back(y);
e[y].push_back(x);
if(s[1]=='T') t[x].push_back({dp[x],x});
if(s[1]=='D') d[x].push_back({dp[x],x});
if(s[1]=='C') c[x].push_back({dp[x],x});
}
DFS(1);
if(t[1].size() + d[1].size() + c[1].size()) { cout<<"NO\n"; return 0; }
if(!flag) { cout<<"NO\n"; return 0; }
else {
cout<<"YES\n";
for(int i=1;i<=cnt;i++) {
cout<<st1[i]<<" "<<st2[i]<<endl;
}
}
return 0;
}
/*
4 3
1 2
2 4
3 3
4 1
*/
详细
Test #1:
score: 100
Accepted
time: 8ms
memory: 56956kb
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: -100
Wrong Answer
time: 9ms
memory: 56944kb
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:
NO
result:
wrong answer Jury has answer but participant has not.