QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#103814 | #6396. Puzzle: Kusabi | skyline# | WA | 4ms | 7352kb | C++14 | 3.5kb | 2023-05-07 16:46:54 | 2023-05-07 16:46:56 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+5;
vector<int>G[N];
int dep[N],op[N],fg[N],p[N];
char s[100];
int cmp(int i,int j){return dep[i]<dep[j];}
int bs(int x){return x>0?x:-x;}
void dfs(int u)
{
vector<int>D,T,C;
if(op[u]==1)D.push_back(u);
if(op[u]==2)C.push_back(u);
if(op[u]==3)T.push_back(u);
for(int v:G[u])
{
dfs(v);
if(op[fg[v]]==1)D.push_back(fg[v]);
if(op[fg[v]]==2)C.push_back(fg[v]);
if(op[fg[v]]==3)T.push_back(fg[v]);
}
sort(D.begin(),D.end(),cmp);
sort(T.begin(),T.end(),cmp);
sort(C.begin(),C.end(),cmp);
if(bs(D.size()-C.size())>1){puts("NO");exit(0);}
/*printf("u=%d\n",u);
for(int x:D)printf("%d ",x);puts("");
for(int x:T)printf("%d ",x);puts("");
for(int x:C)printf("%d ",x);puts("");*/
if(D.size()>C.size())
{
int l=0,r=D.size()-1,ans=r+1;
while(l<=r)
{
int mid=l+r>>1,o=1;
for(int i=0;i<C.size();++i)
if(dep[D[i+(i>=mid)]]>=dep[C[i]]){o=0;break;}
if(o)ans=mid,r=mid-1;
else l=mid+1;
}
if(ans==D.size()){puts("NO");exit(0);};
if(T.size()&1){puts("NO");exit(0);};
for(int i=0;i+1<T.size();i+=2)
{
if(dep[T[i]]!=dep[T[i+1]]){puts("NO");exit(0);}
p[T[i]]=T[i+1],p[T[i+1]]=T[i];
}
fg[u]=D[ans];
for(int i=0;i<C.size();++i)
p[D[i+(i>=ans)]]=C[i],p[C[i]]=D[i+(i>=ans)];
}
if(D.size()==C.size())
{
for(int i=0;i<D.size();++i)
if(dep[D[i]]>=dep[C[i]]){puts("NO");exit(0);}
else p[D[i]]=C[i],p[C[i]]=D[i];
if(T.size()&1)
{
int pp=-1;
for(int i=0;i+1<T.size();i+=2)
if(dep[T[i]]!=dep[T[i+1]])
{
pp=i;break;
}
else p[T[i]]=T[i+1],p[T[i+1]]=T[i];
if(pp==-1)
{
fg[u]=T[T.size()-1];return;
}
for(int i=pp+1;i+1<T.size();i+=2)
if(dep[T[i]]!=dep[T[i+1]]){puts("NO");exit(0);}
else p[T[i]]=T[i+1],p[T[i+1]]=T[i];
fg[u]=T[pp];return;
}
else
{
for(int i=0;i+1<T.size();i+=2)
{
if(dep[T[i]]!=dep[T[i+1]]){puts("NO");exit(0);}
p[T[i]]=T[i+1],p[T[i+1]]=T[i];
}
}
}
if(D.size()<C.size())
{
int l=0,r=C.size()-1,ans=r+1;
while(l<=r)
{
int mid=l+r>>1,o=1;
for(int i=0;i<D.size();++i)
if(dep[D[i]]>=dep[C[i+(i>=mid)]]){o=0;break;}
if(o)ans=mid,l=mid+1;
else r=mid-1;
}
if(ans==C.size()){puts("NO");exit(0);};
if(T.size()&1){puts("NO");exit(0);};
for(int i=0;i+1<T.size();i+=2)
{
if(dep[T[i]]!=dep[T[i+1]]){puts("NO");exit(0);}
p[T[i]]=T[i+1],p[T[i+1]]=T[i];
}
fg[u]=C[ans];
for(int i=0;i<D.size();++i)
p[D[i]]=C[i+(i>=ans)],p[C[i+(i>=ans)]]=D[i];
}
}
int main() {
int n;scanf("%d",&n);
for(int i=2;i<=n;++i)
{
int pa;
scanf("%d%d%s",&pa,&pa,s+1);
G[pa].push_back(i);
dep[i]=dep[pa]+1;
if(s[1]=='D')op[i]=1;
if(s[1]=='C')op[i]=2;
if(s[1]=='T')op[i]=3;
}
dfs(1);
puts("YES");
for(int i=1;i<=n;++i)
if(p[i]>i)printf("%d %d\n",i,p[i]);
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 4ms
memory: 6656kb
input:
8 2 1 - 3 1 - 4 2 Tong 5 2 Tong 6 3 Duan 7 3 - 8 7 Chang
output:
YES 4 5 6 8
result:
ok Correct.
Test #2:
score: 0
Accepted
time: 1ms
memory: 7352kb
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 2 6 3 10 5 7 8 9
result:
ok Correct.
Test #3:
score: -100
Wrong Answer
time: 0ms
memory: 5888kb
input:
2 2 1 Tong
output:
YES
result:
wrong answer Only odd number of marked vertices to match.