QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#470887#6396. Puzzle: KusabiUESTC_OldEastWest#WA 22ms11628kbC++203.0kb2024-07-10 16:49:482024-07-10 16:49:48

Judging History

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

  • [2024-07-10 16:49:48]
  • 评测
  • 测评结果:WA
  • 用时:22ms
  • 内存:11628kb
  • [2024-07-10 16:49:48]
  • 提交

answer

#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int M = 1e5 + 5;
const int mod = 998244353;
int n, k, a[M], opt[M], dep[M];
vector<int> g[M];
vector<pair<int, int>> ans;


int dfs(int x, int fa) {
    dep[x] = dep[fa] + 1;

    vector<int> chang, duan, tong;

    if (opt[x] == 1) chang.push_back(x);
    if (opt[x] == 2) duan.push_back(x);
    if (opt[x] == 3) tong.push_back(x);

    for (int y : g[x]) {
        if (y == fa) continue;
        int t = dfs(y, x);
        if (t == -1) continue;
        if (opt[t] == 1) chang.push_back(t);
        if (opt[t] == 2) duan.push_back(t);
        if (opt[t] == 3) tong.push_back(t);
    }
    sort(chang.begin(), chang.end(), [](const int&x, const int&y) {
        return dep[x] < dep[y];
    });
    sort(duan.begin(), duan.end(), [](const int&x, const int&y) {
        return dep[x] > dep[y];
    });
    sort(tong.begin(), tong.end(), [](const int&x, const int&y) {
        return dep[x] > dep[y];
    });

    int node = -1;
    int cnt = 0;

    if (tong.size() & 1) {
        node = tong.back();
        cnt = 1;
    }

    for (int i = 0; i < (int)tong.size() - 1; i += 2) {
        if (dep[tong[i]] == dep[tong[i + 1]]) {
            ans.push_back({tong[i], tong[i + 1]});
        } else {
            cnt ++;
            node = tong[i];
            i -= 1;
        }
    }

    if (cnt > 1) {
        cout << "NO" << endl;
        exit(0);
    }

    if (chang.size() > duan.size()) {
        if (chang.size() != duan.size() + 1 || cnt) {
            cout << "NO" << endl;
            exit(0);
        }
        int i = 0, j = 0;
        for (;j < duan.size(); j++, i++) {
            ans.push_back({chang[i], duan[i]});
        }
        return chang.back();
    } else if (chang.size() < duan.size()) {
        if (duan.size() != chang.size() + 1 || cnt) {
            cout << "NO" << endl;
            exit(0);
        }
        int i = 0, j = 0;
        for (;j < chang.size(); j++, i++) {
            ans.push_back({chang[i], duan[i]});
        }
        return duan.back();
    } else {
        int i = 0, j = 0;
        for (;j < chang.size(); j++, i++) {
            ans.push_back({chang[i], duan[i]});
        }      
    }
    if (cnt) {
        // cout << "111: " << node << endl;
        return node;
    }
    return -1;
}


signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    
    cin >> n;

    int cnt = 1;

    for (int i = 1; i < n; i++) {
        int x, y; string ts;
        cin >> x >> y >> ts;
        g[x].push_back(y);
        g[y].push_back(x);
        if (ts[0] == 'C') opt[x] = 1;
        else if (ts[0] == 'D') opt[x] = 2;
        else if (ts[0] == 'T') opt[x] = 3;
        else cnt++;
    }

    if (n - cnt & 1) {
        cout << "NO" << endl;
        return 0;
    }

    dfs(1, 0);

    cout << "YES" << endl;
    
    for (auto [x, y] : ans) {
        cout << x << " " << y << endl;
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 5544kb

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

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

input:

2
2 1 Tong

output:

NO

result:

ok Correct.

Test #4:

score: -100
Wrong Answer
time: 22ms
memory: 11628kb

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:

NO

result:

wrong answer Jury has answer but participant has not.