QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#228698#6396. Puzzle: KusabikiwiHM#WA 1ms6848kbC++144.2kb2023-10-28 14:01:032023-10-28 14:01:04

Judging History

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

  • [2023-10-28 14:01:04]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:6848kb
  • [2023-10-28 14:01:03]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e5 + 5;

int n;
int a[maxn];
int dep[maxn], tong[maxn];
vector<pair<int, int> > ans;
vector<int> tree[maxn];

inline int dfsTong(int u, int d){
    dep[u] = d;
    vector<int> vec;
    for(auto v: tree[u]){
        int res = dfsTong(v, d + 1);
        if(~res){
            tong[v] = true;
            vec.push_back(res);
        }
    }
    sort(vec.begin(), vec.end(), [&](const int &i, const int &j){ return dep[i] < dep[j]; });
    int res = -1;
    for(int i = 0; i < vec.size(); ){
        if(i + 1 == vec.size()){
            if(~res){ puts("NO"); exit(0); }
            res = vec[i], ++i;
        }
        else{
            if(dep[vec[i + 1]] == dep[vec[i]]) ans.push_back(make_pair(vec[i], vec[i + 1])), i += 2;
            else{
                if(~res){ puts("NO"); exit(0); }
                res = vec[i], ++i;
            }
        }
    }

    if(a[u] == 1){
        if(~res){ puts("NO"); exit(0); }
        res = u;
    }
    return res;
}

inline vector<int> erase(const vector<int> &vec, int p){
    vector<int> ret;
    assert(p >= 0 && p < vec.size());
    for(int i = 0; i < vec.size(); ++i) if(i != p) ret.push_back(vec[i]);
    return ret;
}

inline bool check(vector<int> &A, vector<int> &B){
    assert(A.size() == B.size());
    for(int i = 0; i < A.size(); ++i) if(dep[A[i]] >= dep[B[i]]) return false;
    return true;
}

inline pair<int, int> dfs(int u){
    vector<int> vDuan, vChang;
    for(auto v: tree[u]){
        pair<int, int> res = dfs(v);
        if(tong[v] && res.first != -1){ puts("NO"); exit(0); }
        if(res.first == 0) vDuan.push_back(res.second);
        if(res.first == 2) vChang.push_back(res.second);
    }
    if(a[u] == 0) vDuan.push_back(u);
    if(a[u] == 2) vChang.push_back(u);

    sort(vDuan.begin(), vDuan.end(), [&](const int &i, const int &j){ return dep[i] < dep[j]; });
    sort(vChang.begin(), vChang.end(), [&](const int &i, const int &j){ return dep[i] < dep[j]; });

    // printf("u = %d\n", u);
    // for(auto v: vDuan) printf("%d ", v); puts("");
    // for(auto v: vChang) printf("%d ", v); puts("");

    pair<int, int> ret = make_pair(-1, 0);

    if(vDuan.size() == vChang.size()){
        for(int i = 0; i < vDuan.size(); ++i){
            if(dep[vDuan[i]] < dep[vChang[i]]){
                ans.push_back(make_pair(vDuan[i], vChang[i]));
            }
            else{ puts("NO"); exit(0); }
        }
        return ret;
    }
    else if(vDuan.size() == vChang.size() + 1){
        int lb = -1, rb = vDuan.size();
        for(; lb + 1 < rb; ){
            int md = (lb + rb) >> 1;
            vector<int> vDuan_ = erase(vDuan, md);
            if(check(vDuan_, vChang)) rb = md;
            else lb = md;
        }
        if(rb == vDuan.size()){ puts("NO"); exit(0); }
        ret = make_pair(0, vDuan[rb]);
        vDuan = erase(vDuan, rb); assert(vDuan.size() == vChang.size());
        for(int i = 0; i < vDuan.size(); ++i) ans.push_back(make_pair(vDuan[i], vChang[i]));
        return ret;
    }
    else if(vChang.size() == vDuan.size() + 1){
        int lb = -1, rb = vChang.size();
        for(; lb + 1 < rb; ){
            int md = (lb + rb) >> 1;
            vector<int> vChang_ = erase(vChang, md);
            if(check(vDuan, vChang_)) lb = md;
            else rb = md;
        }
        if(lb == -1){ puts("NO"); exit(0); }
        ret = make_pair(2, vChang[lb]);
        vChang = erase(vChang, lb); assert(vDuan.size() == vChang.size());
        for(int i = 0; i < vDuan.size(); ++i) ans.push_back(make_pair(vDuan[i], vChang[i]));
        return ret;
    }
    else{ puts("NO"); exit(0); }
}

int main(){
    scanf("%d", &n);
    for(int it = 1; it < n; ++it){
        int i, p; scanf("%d%d", &i, &p);
        char buf[15]; scanf("%s", buf);
        --i, --p;
        tree[p].push_back(i);
        if(buf[0] == 'D') a[i] = 0;
        else if(buf[0] == 'T') a[i] = 1;
        else if(buf[0] == 'C') a[i] = 2;
        else a[i] = -1;
    }

    dfsTong(0, 0);

    dfs(0);

    puts("YES");
    for(auto p: ans) printf("%d %d\n", p.first + 1, p.second + 1);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 6808kb

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
8 9
3 10
2 6
7 5

result:

ok Correct.

Test #3:

score: -100
Wrong Answer
time: 1ms
memory: 6848kb

input:

2
2 1 Tong

output:

YES

result:

wrong answer Only odd number of marked vertices to match.