QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#135489#6396. Puzzle: KusabiUrgantTeam#WA 23ms10580kbC++234.0kb2023-08-05 16:15:162023-08-05 16:15:18

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-08-05 16:15:18]
  • 评测
  • 测评结果:WA
  • 用时:23ms
  • 内存:10580kb
  • [2023-08-05 16:15:16]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;

int const maxn = 1e5 + 5;
int a[maxn], h[maxn];
vector < int > g[maxn];
vector < pair < int, int > > dp[maxn];
vector < pair < int, int > > ans;

int good(vector < pair < int, int > > A, vector < pair < int, int > > B) {
    assert(A.size() == B.size());
    for (int i = 0; i < A.size(); i++) {
        if (A[i].second <= B[i].second) return 0;
    }
    return 1;
}

void dfs(int v) {
    for (auto u : g[v]) {
        h[u] = h[v] + 1;
        dfs(u);
        for (auto key : dp[u]) dp[v].push_back(key);
    }
    if (a[v]) dp[v].push_back({a[v], v});
    vector < int > tong;
    for (auto key : dp[v]) {
        if (key.first == 2) tong.push_back(key.second);
    }
    sort(tong.begin(), tong.end(), [](int x, int y) {
        return h[x] < h[y];
    });
    vector < pair < int, int > > add;
    int i = 0;
    while (i < tong.size()) {
        if (i + 1 < (int)tong.size() && h[tong[i]] == h[tong[i + 1]]) {
            ans.push_back({tong[i], tong[i + 1]});
            i += 2;
        } else {
            add.push_back({2, tong[i]});
            i++;
        }
    }
    vector < pair < int, int > > big, small;
    for (auto key : dp[v]) {
        if (key.first == 1) big.push_back({h[key.second], key.second});
        else if (key.first == 3) small.push_back({h[key.second], key.second});
    }
    sort(big.begin(), big.end());
    sort(small.begin(), small.end());
    if (abs((int)big.size() - (int)small.size()) > 1) {
        cout << "NO\n";
        exit(0);
    }
    if (big.size() == small.size()) {
        for (int i = 0; i < big.size(); i++) {
            if (big[i].first <= small[i].first) {
                cout << "NO\n";
                exit(0);
            }
            ans.push_back({big[i].second, small[i].second});
        }
        dp[v] = {};
    } else if (big.size() > small.size()) {
        int lef = -1, righ = big.size();
        while (righ - lef > 1) {
            int mid = (righ + lef) / 2;
            vector < pair < int, int > > A, B = small;
            for (int i = 0; i < big.size(); i++) if (i != mid) A.push_back(big[i]);
            if (good(A, B)) lef = mid;
            else righ = mid;
        }
        if (lef == -1) {
            cout << "NO\n";
            exit(0);
        }
        dp[v] = {{1, big[lef].second}};
        vector < pair < int, int > > A, B = small;
        for (int i = 0; i < big.size(); i++) if (i != lef) A.push_back(big[i]);
        for (int i = 0; i < A.size(); i++) ans.push_back({A[i].second, B[i].second});
    } else {
        int lef = -1, righ = small.size();
        while (righ - lef > 1) {
            int mid = (righ + lef) / 2;
            vector < pair < int, int > > A = big, B;
            for (int i = 0; i < small.size(); i++) if (i != mid) B.push_back(small[i]);
            if (good(A, B)) righ = mid;
            else lef = mid;
        }
        if (righ == small.size()) {
            cout << "NO\n";
            exit(0);
        }
        dp[v] = {{3, small[righ].second}};
        vector < pair < int, int > > A = big, B;
        for (int i = 0; i < small.size(); i++) if (i != lef) B.push_back(small[i]);
        for (int i = 0; i < A.size(); i++) ans.push_back({A[i].second, B[i].second});
    }
    for (auto key : add) dp[v].push_back(key);
    if (dp[v].size() > 1) {
        cout << "NO\n";
        exit(0);
    }
}

main() {
#ifdef HOME
    freopen("input.txt", "r", stdin);
#endif // HOME
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, u, v;
    cin >> n;
    string type;
    for (int i = 2; i <= n; i++) {
        cin >> u >> v >> type;
        assert(u == i);
        g[v].push_back(u);
        if (type == "Chang") a[i] = 1;
        else if (type == "Tong") a[i] = 2;
        else if (type == "Duan") a[i] = 3;
    }
    dfs(1);
    if (dp[1].size()) cout << "NO\n";
    else {
        cout << "YES\n";
        for (auto key : ans) cout << key.first << " " << key.second << '\n';
    }
    return 0;
}


Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 2ms
memory: 8284kb

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

input:

2
2 1 Tong

output:

NO

result:

ok Correct.

Test #4:

score: -100
Wrong Answer
time: 23ms
memory: 10580kb

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.