QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#114528#6396. Puzzle: KusabiComplexPugWA 1ms34140kbC++202.9kb2023-06-22 14:24:002023-06-22 14:24:02

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-22 14:24:02]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:34140kb
  • [2023-06-22 14:24:00]
  • 提交

answer

#include <bits/stdc++.h>
#define FOR(i, a, b) for(int i = a; i <= b; ++i)
using namespace std;
// #define int long long
const int _ = 1e6+6;
int n,w[_];
vector<int> g[_];
int son[_][4],ty[_],dis[_];
void dfs_init(int u,int fa) {
    son[u][w[u]]++;
    for(auto v:g[u]) {
        if(v==fa) continue;
        dis[v]=dis[u]+1;
        dfs_init(v,u);
        FOR(i,1,3) son[u][i]+=son[v][i];
    }
    // cout<<u<<": ";
    // FOR(i,1,3) cout<<son[u][i]<<" ";
    // cout<<"\n";
    if(son[u][1]%2==0&&son[u][2]==son[u][3]) {ty[u]=0;return;}
    if(u==1) puts("NO"),exit(0);
    if(son[u][1]%2==1&&son[u][2]==son[u][3]) {ty[u]=1;return;}
    if(son[u][1]%2==0&&son[u][2]==son[u][3]+1) {ty[u]=2;return;}
    if(son[u][1]%2==0&&son[u][2]+1==son[u][3]) {ty[u]=3;return;}
    puts("NO");exit(0);
}
vector<array<int,2>> ans;
int p[_];
void dfs(int u,int fa) {
    set<array<int,2>> S1,S2,S0; 
    for(auto v:g[u]) {
        if(v==fa) continue;
        dfs(v,u);
        if(ty[v]==1) S0.insert({dis[p[v]],p[v]});
        if(ty[v]==2) S1.insert({dis[p[v]],p[v]});
        if(ty[v]==3) S2.insert({dis[p[v]],p[v]});
    }
    if(w[u]==1) S0.insert({dis[u],u});
    if(w[u]==2) S1.insert({dis[u],u});
    if(w[u]==3) S2.insert({dis[u],u});
    // cout<<u<<":" ;
    // for(auto x:S0) cout<<x[1]<<" ";cout<<"\n";
    while(S0.size()>=2) {
        auto x=*S0.begin();S0.erase(S0.begin());
        auto y=*S0.begin();S0.erase(S0.begin());
        // cout<<x[1]<<" "<<y[1]<<"\n";
        if(x[0]==y[0]) {
            // cout<<x[1]<<y[1]<<"\n";
            ans.push_back({x[1],y[1]});
        } else {
            if(ty[u]==1&&p[u]==0) {
                p[u]=x[1];
                S0.insert(y);
            } else {
                puts("NO");exit(0);
            }
        }
    }
    if(S0.size()) p[u]=(*S0.begin())[1];

    // return;
    while(S1.size()) {
        auto x=*S1.rbegin();
        auto y=S2.upper_bound({x[0],0x3f3f3f3f});
        if(y==S2.end()) {
            if(p[u]==0&&ty[u]==2) {
                p[u]=x[1];
                S1.erase(S1.find(x));
            } else {
                puts("NO");exit(0);
            }
        } else {
            ans.push_back({x[1],(*y)[1]});
            S1.erase(S1.find(x));
            S2.erase(y);
        }
    }
    if(ty[u]==3) p[u]=(*S2.begin())[1]; 
}
signed main() {
    // #ifdef ONLINE_JUDGE
    // #else
        freopen("a.in","r",stdin);
        // freopen("a.out","w",stdout);
    // #endif
    cin>>n;
    map<string,int> mp;
    mp["-"]=0,mp["Duan"]=2,mp["Tong"]=1,mp["Chang"]=3;
    FOR(TTT,2,n) {
        int i,p;
        string s;
        cin>>i>>p>>s;
        w[i]=mp[s];
        g[i].push_back(p);
        g[p].push_back(i);
    }
    dfs_init(1,0);
    dfs(1,0);
    puts("YES");
    for(auto [x,y]:ans) {
        cout<<x<<" "<<y<<"\n";
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 34140kb

input:

8
2 1 -
3 1 -
4 2 Tong
5 2 Tong
6 3 Duan
7 3 -
8 7 Chang

output:

YES

result:

wrong output format Unexpected end of file - int32 expected