QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#528625#6396. Puzzle: KusabiOneWanWA 16ms9740kbC++234.7kb2024-08-23 17:06:442024-08-23 17:06:44

Judging History

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

  • [2024-08-23 17:06:44]
  • 评测
  • 测评结果:WA
  • 用时:16ms
  • 内存:9740kb
  • [2024-08-23 17:06:44]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
vector<int> v[1000005];
int type[100005];  //1 tong 2 duan 3 chang
pair<int, int> hv[100005];
int dep[100005];
set<pair<int, int>> res;
bool cmp(int x, int y)
{
    return dep[x] < dep[y];
}
void dfs(int u, int fa)
{
    dep[u] = dep[fa] + 1;
    vector<int> t;
    vector<int> d;
    vector<int> c;
    if (type[u] != 0) {
        if (type[u] == 1) {
            t.push_back(u);
        } else if (type[u] == 2) {
            d.push_back(u);
        } else if (type[u] == 3) {
            c.push_back(u);
        }
    }
    for (auto x : v[u]) {
        if (x == fa) continue;
        dfs(x, u);
        if (hv[x].first != 0) {
            if (hv[x].first == 1) {
                t.push_back(hv[x].second);
            } else if (hv[x].first == 2) {
                d.push_back(hv[x].second);
            } else if (hv[x].first == 3) {
                c.push_back(hv[x].second);
            }
        }
    }
    sort(begin(c), end(c), cmp);
    sort(begin(d), end(d), cmp);
    sort(begin(t), end(t), cmp);
    int less = 0;
    int lessid = 0;
    //cout<<u<<" "<<t.size()<<'\n';
    for (int i = 0; i < t.size(); i++) {
        if (i + 1 < t.size() && dep[t[i]] == dep[t[i + 1]]) {
            res.insert({t[i], t[i + 1]});
            i++;
        } else {
            less++;
            if (hv[u].first != 0) {
                cout << "NO\n";
                exit(0);
            }
            hv[u].first = 1;
            hv[u].second = t[i];
        }
    }
    int l = 0, r = 0;
    // reverse(begin(c),end(c));
    //reverse(begin(d),end(d));

    //1 tong 2 duan 3 chang
    if (c.size() > d.size()) {
        for (l = 0, r = 0; l < d.size(); l++) {
            while (r < c.size() && d[l] >= c[r]) {
                if (hv[u].first != 0) {
                    cout << "NO\n";
                    exit(0);
                } else {
                    hv[u].first = 3;
                    hv[u].second = c[r];
                }
                r++;
            }
            if (r < c.size()) {
                res.insert({c[r], d[l]});
                r++;
            } else {
                if (hv[u].first != 0) {
                    cout << "NO\n";
                    exit(0);
                } else {
                    hv[u].first = 2;
                    hv[u].second = d[r];
                }
            }
        }
        for (; r < c.size(); r++) {
            if (hv[u].first != 0) {
                cout << "NO\n";
                exit(0);
            } else {
                hv[u].first = 3;
                hv[u].second = c[r];
            }
        }
    } else {  // c.size( )<d.size()
              //1 tong 2 duan 3 chang
        reverse(begin(c), end(c));
        reverse(begin(d), end(d));
        for (l = 0, r = 0; l < c.size(); l++) {
            while (r < d.size() && dep[c[l]] <= dep[d[r]]) {
                if (hv[u].first != 0) {
                    cout << "No\n";
                    exit(0);
                } else {
                    hv[u].first = 2;
                    hv[u].second = d[r];
                }
                r++;
            }
            if (r < d.size()) {
                res.insert({c[l], d[r]});
                r++;
            } else {
                if (hv[u].first != 0) {
                    cout << "No\n";
                    exit(0);
                } else {
                    hv[u].first = 3;
                    hv[u].second = c[l];
                }
            }
        }
        //cout<<u<<" "<<d.size()<<" "<<c.size()<<" "<<hv[u].first<<" "<<'\n';
        for (r; r < d.size(); r++) {
            if (hv[u].first != 0) {
                // cout<<u<<"?\n";
                cout << "No\n";
                exit(0);
            } else {
                hv[u].first = 2;
                hv[u].second = d[r];
            }
        }
    }
    if (u == 1 && hv[u].first != 0) {
        cout << "No\n";
        exit(0);
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    for (int i = 1; i <= n - 1; i++) {
        int x, y;
        cin >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
        string sx;
        cin >> sx;
        if (sx == "Tong") {
            type[x] = 1;
        } else if (sx == "Duan") {
            type[x] = 2;
        } else if (sx == "Chang") {
            type[x] = 3;
        } else {
            type[x] = 0;
        }
    }

    dfs(1, 0);
    cout << "YES\n";
    for (auto [x, y] : res) {
        cout << x << " " << y << '\n';
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3656kb

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

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

result:

ok Correct.

Test #3:

score: 0
Accepted
time: 1ms
memory: 3656kb

input:

2
2 1 Tong

output:

No

result:

ok Correct.

Test #4:

score: -100
Wrong Answer
time: 16ms
memory: 9740kb

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.