QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#187840#6396. Puzzle: KusabiTlenekWodoruWA 37ms10368kbC++144.3kb2023-09-25 02:04:582023-09-25 02:04:58

Judging History

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

  • [2023-09-25 02:04:58]
  • 评测
  • 测评结果:WA
  • 用时:37ms
  • 内存:10368kb
  • [2023-09-25 02:04:58]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
vector<int>D[100009];
char tab[100009];
int odl[100009];
int g[100009];
int n,IleUwu=0;
vector<pair<int,int>>ODP;
vector<pair<int,int>>usun(vector<pair<int,int>>A, int ind)
{
    vector<pair<int,int>>W;
    for(int i=0;i<ind;i++)
    {
        W.push_back(A[i]);
    }
    for(int i=ind+1;i<A.size();i++)
    {
        W.push_back(A[i]);
    }
    return W;
}
void DFS(int v, int last)
{
    for(int som : D[v])
    {
        if(som!=last)
        {
            odl[som]=odl[v]+1;
            DFS(som,v);
        }
    }
    vector<pair<int,int>>A,B,C;
    if(g[v]!=0)
    {
        if(tab[v]=='C'){A.push_back({odl[v],v});}
        else if(tab[v]=='D'){B.push_back({odl[v],v});}
        else if(tab[v]=='T'){C.push_back({odl[v],v});}
    }
    for(int som : D[v])
    {
        if(som==last){continue;}
        if(g[som]!=0)
        {
            if(tab[g[som]]=='C'){A.push_back({odl[g[som]],g[som]});}
            else if(tab[g[som]]=='D'){B.push_back({odl[g[som]],g[som]});}
            else if(tab[g[som]]=='T'){C.push_back({odl[g[som]],g[som]});}
        }
    }
    if((A.size()!=B.size()||C.size()%2==1)&&v==1){cout<<"NO"<<'\n';exit(0);}
    if((C.size()%2==1&&A.size()!=B.size())||abs((int)A.size()-(int)B.size())>1){cout<<"NO"<<'\n';exit(0);}
    if(C.size()%2==0&&A.size()==B.size()){C.push_back({-1,0});}
    sort(A.begin(),A.end());
    sort(B.begin(),B.end());
    sort(C.begin(),C.end());
    if(C.size()%2==1)
    {
        bool cv=1;
        while(C.size()>0)
        {
            if(C[C.size()-2].first!=C[C.size()-1].first)
            {
                if(cv==0){cout<<"NO"<<'\n';exit(0);}
                else{cv=0;g[v]=C.back().second;C.pop_back();}
                continue;
            }
            ODP.push_back({C[C.size()-2].second,C[C.size()-1].second});
            C.pop_back();
            C.pop_back();
        }
        for(int i=0;i<A.size();i++)
        {
            if(A[i].first<=B[i].first){cout<<"NO"<<'\n';exit(0);}
            ODP.push_back({A[i].second,B[i].second});
        }
    }
    else
    {
        while(C.size()>0)
        {
            if(C[C.size()-2].first!=C[C.size()-1].first){cout<<"NO"<<'\n';exit(0);}
            ODP.push_back({C[C.size()-2].second,C[C.size()-1].second});
            C.pop_back();
            C.pop_back();
        }
        if(A.size()==B.size()+1)
        {
            int usu=0;
            for(int i=0;i<B.size();i++)
            {
                if(A[i].first<=B[i].first)
                {
                    break;
                }
                usu=i+1;
            }
            g[v]=A[usu].second;
            A=usun(A,usu);
            for(int i=0;i<B.size();i++)
            {
                if(A[i].first<=B[i].first){cout<<"NO"<<'\n';exit(0);}
                ODP.push_back({A[i].second,B[i].second});
            }
        }
        else if(A.size()+1==B.size())
        {
            reverse(A.begin(),A.end());
            reverse(B.begin(),B.end());
            int usu=0;
            for(int i=0;i<A.size();i++)
            {
                if(A[i].first<=B[i].first)
                {
                    break;
                }
                usu=i+1;
            }
            g[v]=B[usu].second;
            B=usun(B,usu);
            for(int i=0;i<A.size();i++)
            {
                if(A[i].first<=B[i].first){cout<<"NO"<<'\n';exit(0);}
                ODP.push_back({A[i].second,B[i].second});
            }
        }
    }
}
int main()
{ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin>>n;
    for(int i=2;i<=n;i++)
    {
        string Z;
        int a,b;
        cin>>a>>b>>Z;
        D[a].push_back(b);
        D[b].push_back(a);
        IleUwu++;
        g[i]=i;
        if(Z=="Chang")
        {
            tab[i]='C';
        }
        else if(Z=="Duan")
        {
            tab[i]='D';
        }
        else if(Z=="Tong")
        {
            tab[i]='T';
        }
        else if(Z=="-")
        {
            g[i]=0;
            IleUwu--;
            tab[i]='-';
        }
    }
    DFS(1,-1);
    cout<<"YES"<<'\n';
    for(pair<int,int>xd : ODP)
    {
        cout<<xd.first<<" "<<xd.second<<'\n';
    }
    return 0;
}



Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 6596kb

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: 1ms
memory: 5984kb

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

input:

2
2 1 Tong

output:

NO

result:

ok Correct.

Test #4:

score: 0
Accepted
time: 37ms
memory: 10368kb

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:

YES
46666 56115
88119 38802
93143 10006
31531 76473
42604 15988
6569 30148
11412 2008
64525 46703
71289 70618
81204 47748
42353 20514
97634 46131
83516 82155
66867 62410
15712 9975
4978 3205
83026 5622
48783 10902
82167 30893
93809 65878
33377 66951
94549 66936
79128 64411
8453 22475
54702 36857
629...

result:

ok Correct.

Test #5:

score: -100
Wrong Answer
time: 24ms
memory: 9924kb

input:

100000
2 1 -
3 2 -
4 3 -
5 4 -
6 4 -
7 6 -
8 7 -
9 5 -
10 9 -
11 10 -
12 6 -
13 12 -
14 11 -
15 9 -
16 14 -
17 16 -
18 10 -
19 15 -
20 13 -
21 20 -
22 17 -
23 22 -
24 22 Duan
25 11 -
26 12 -
27 20 -
28 18 -
29 28 -
30 16 -
31 28 -
32 30 -
33 31 -
34 28 -
35 34 -
36 35 -
37 22 -
38 34 -
39 38 -
40 35...

output:

NO

result:

wrong answer Jury has answer but participant has not.