QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#252903#6396. Puzzle: KusabisupepapupuRE 0ms3868kbC++172.2kb2023-11-16 14:40:252023-11-16 14:40:25

Judging History

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

  • [2023-11-16 14:40:25]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3868kb
  • [2023-11-16 14:40:25]
  • 提交

answer

#include <bits/stdc++.h>

#define x first
#define y second
#define el '\n'
#define debug(x) cout << #x << ": " << x << el
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int N = 5010, INF = 0x3f3f3f3f, mod = 998244353;

void inc(ll &a, ll b) {
    a += b;
    if (a >= mod) a -= mod;
}

void dec(ll &a, ll b) {
    a -= b;
    if (a < 0) a += mod;
}

ll add(ll a, ll b) {
    a += b;
    return a >= mod ? a - mod : a;
}

ll del(ll a, ll b) {
    a -= b;
    return a < 0 ? a + mod : a;
}

vector<int> G[N];
vector<pii> chang, duan, tong;
int dep[N];

void dfs(int u, int fa) {
    dep[u] = dep[fa] + 1;
    for (int v: G[u]) 
        if (v != fa) dfs(v, u);
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    int n; cin >> n;
    for (int i = 1; i < n; ++i) {
        int a, b; string str; cin >> a >> b >> str;
        if (str == "Chang") chang.emplace_back(a, b);
        else if (str == "Duan") duan.emplace_back(a, b);
        else if (str == "Tong") tong.emplace_back(a, b);
        G[a].emplace_back(b), G[b].emplace_back(a);
    }
    dfs(1, 0);
    set<pii> sc, sd, st;
    for (auto[a, b]: chang) {
        if (dep[a] < dep[b]) swap(a, b);
        sc.insert({dep[a], a});
    }
    for (auto[a, b]: duan) {
        if (dep[a] < dep[b]) swap(a, b);
        sd.insert({dep[a], a});
    }
    for (auto[a, b]: tong) {
        if (dep[a] < dep[b]) swap(a, b);
        st.insert({dep[a], a});
    }
    vector<pii> ans;
    if (st.size() & 1) return cout << "NO\n", 0;
    while (st.size()) {
        if (st.begin()->x != (++st.begin())->x) return cout << "NO\n", 0;
        ans.emplace_back(st.begin()->y, (++st.begin())->y);
        st.erase(st.begin()), st.erase(st.begin());
    }
    if (sc.size() != sd.size()) return cout << "NO\n", 0;
    while (sd.size()) {
        auto[d, u] = *sd.begin();
        sd.erase(sd.begin());
        auto it = sc.lower_bound({d + 1, 0});
        if (it == sc.end()) return cout << "NO\n", 0;
        ans.emplace_back(u, it->y);
        sc.erase(it);
    }
    cout << "YES\n";
    for (auto[a, b]: ans) cout << a << ' ' << b << el;
}

詳細信息

Test #1:

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

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

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

result:

ok Correct.

Test #3:

score: 0
Accepted
time: 0ms
memory: 3868kb

input:

2
2 1 Tong

output:

NO

result:

ok Correct.

Test #4:

score: -100
Runtime Error

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:


result: