QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#103804#6396. Puzzle: Kusabiskyline#WA 3ms6968kbC++143.5kb2023-05-07 16:20:442023-05-07 16:20:47

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-07 16:20:47]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:6968kb
  • [2023-05-07 16:20:44]
  • 提交

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)
                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)
                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: 2ms
memory: 6968kb

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: 0ms
memory: 6904kb

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: 3ms
memory: 5900kb

input:

2
2 1 Tong

output:

YES

result:

wrong answer Only odd number of marked vertices to match.