QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#241648#6396. Puzzle: Kusabiucup-team902#WA 1ms6800kbC++174.7kb2023-11-06 15:05:192023-11-06 15:05:20

Judging History

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

  • [2023-11-06 15:05:20]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:6800kb
  • [2023-11-06 15:05:19]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> G[100005];
int dep[100005];
int lst[100005];
int tmp[100005];
vector<pair<int,int>> ans;
bool cmp(int x, int y)
{
    return dep[x] < dep[y];
}
void dfs(int x)
{

    for (int v : G[x])
        dep[v] = dep[x] + 1, dfs(v);
    vector<int> s[3];
    if (tmp[x] != 0)
        s[tmp[x] - 1].push_back(x);
    for (int v : G[x]) {
        if (lst[v] == 0)
            continue;
        s[tmp[lst[v]] - 1].push_back(lst[v]);
    }
    sort(s[0].begin(), s[0].end(), cmp);
    sort(s[1].begin(), s[1].end(), cmp);
    sort(s[2].begin(), s[2].end(), cmp);
    //cout << s[0].size() << " " << x << endl;
    if ((s[0].size() + s[1].size() + s[2].size()) % 2 == 0) {
        if (s[0].size() % 2 != 0) {
            cout << "NO" << endl;
            exit(0);
        }
        for (int i = 0; i < s[0].size(); i += 2) {
            if (dep[s[0][i]] != dep[s[0][i + 1]]) {
                cout << "NO" << endl;
                exit(0);
            }
            ans.push_back({ s[0][i], s[0][i + 1] });
        }
        if (s[1].size() != s[2].size()) {
            cout << "NO" << endl;
            exit(0);
        }
        for (int i = 0; i< s[1].size(); ++i) {
            if (dep[s[1][i]] >= dep[s[2][i]]) {
                cout << "NO" << endl;
                exit(0);
            }
            ans.push_back({ s[1][i], s[2][i] });
        }
        lst[x] = 0;
    } else {
        if (s[0].size() % 2 == 0) {
            for (int i = 0; i < s[0].size(); i += 2) {
                if (dep[s[0][i]] != dep[s[0][i + 1]]) {
                    cout << "NO" << endl;
                    exit(0);
                }
                ans.push_back({ s[0][i], s[0][i + 1] });
            }
            if (abs((int)s[1].size() - (int)s[2].size()) > 1) {
                cout << "NO" << endl;
                exit(0);
            }
            if(s[1].size()>s[2].size()){
                set<pair<int, int>> now;
                for(int v:s[1]){
                    now.insert({dep[v], v});
                }
                for(int v:s[2]){
                    auto t = now.lower_bound({dep[v],-0x3f3f3f3f});
                    if(t==now.begin()){
                        cout << "NO" << endl;
                        exit(0);
                    }
                    t--;
                    ans.push_back({
                        v,(*t).second
                    });
                    now.erase(t);
                }
                lst[x] = (*now.begin()).second;
            }
            if(s[1].size()<s[2].size()){
                set<pair<int, int>> now;
                for(int v:s[2]){
                    now.insert({dep[v], v});
                }
                for(int v:s[1]){
                    auto t = now.upper_bound({dep[v],0x3f3f3f3f});
                    if(t==now.end()){
                        cout << "NO" << endl;
                        exit(0);
                    }
                    ans.push_back({
                        v,(*t).second});
                    now.erase(t);
                }
                lst[x] = (*now.begin()).second;
            }
        } else {
            if (s[1].size() != s[2].size()) {
                cout << "NO" << endl;
                exit(0);
            }
            for (int i = 0; i < s[1].size(); ++i) {
                if (dep[s[1][i]] >= dep[s[2][i]]) {
                    cout << "NO" << endl;
                    exit(0);
                }
                ans.push_back({ s[1][i], s[2][i] });
            }
            int up = 0;
            if((int)s[0].size()==1)
                up = s[0][0];
            for (int i = 0; i + 1 < s[0].size();i+=2){
                if(dep[s[0][i]]!=dep[s[0][i+1]]&&up!=0){
                    cout << "NO" << endl;
                    exit(0);
                }
                else if(dep[s[0][i]]!=dep[s[0][i+1]]){
                    up = s[0][i];
                    i--;
                }
                else{
                    ans.push_back({s[0][i], s[0][i + 1]});
                }
            }
            lst[x] = up;
        }
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    for (int i = 1; i < n; ++i) {
        int x, y;
        string s;
        cin >> x >> y >> s;
        if (s[0] == 'T') {
            tmp[x] = 1;
        } else if (s[0] == 'D')
            tmp[x] = 2;
        else if (s[0] == 'C')
            tmp[x] = 3;
        G[y].push_back(x);
    }
    dfs(1);
    cout << "YES" << endl;
    for (auto [x, y] : ans) {
        cout << x << " " << y << endl;
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

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

result:

ok Correct.

Test #3:

score: -100
Wrong Answer
time: 0ms
memory: 5884kb

input:

2
2 1 Tong

output:

YES

result:

wrong answer Only odd number of marked vertices to match.