QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#208119#6559. A Tree and Two EdgesThallium54#RE 21ms8636kbC++205.0kb2023-10-09 06:10:142023-10-09 06:10:15

Judging History

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

  • [2023-10-09 06:10:15]
  • 评测
  • 测评结果:RE
  • 用时:21ms
  • 内存:8636kb
  • [2023-10-09 06:10:14]
  • 提交

answer

#include <bits/stdc++.h>

template <bool union_by_size = true>
struct UF {
    int n, comp_cnt;
    std::vector<int> p;
    UF(int n_) : n(n_), comp_cnt(n), p(n, -1) {}
    int find(int x) { return p[x] < 0 ? x : p[x] = find(p[x]); }
    bool same(int x, int y) { return find(x) == find(y); }
    template <typename F>
    bool join(int x, int y, F&& f) {
        x = find(x), y = find(y);
        if (x == y) return false;
        comp_cnt--;
        if constexpr (union_by_size) {
            if (-p[x] > -p[y]) std::swap(x, y);
        }
        p[y] += p[x];
        f(x, y);
        p[x] = y;
        return true;
    }
    bool join(int x, int y) {
        return join(x, y, [](...){});
    }
    std::vector<int> group_sizes() {
        std::vector<int> sizes;
        for (int i = 0; i < n; i++) {
            if (p[i] < 0) {
                sizes.push_back(-p[i]);
            }
        }
        return sizes;
    }
    std::vector<std::vector<int>> groups() {
        std::vector<std::vector<int>> groups(n);
        for (int i = 0; i < n; i++) {
            groups[find(i)].push_back(i);
        }
        groups.erase(
                std::remove_if(begin(groups), end(groups), [](const std::vector<int>& v) { return v.empty(); }),
                end(groups));
        return groups;
    }
    int group_cnt() {
        return comp_cnt;
    }
    int size_of(int x) {
        return -p[find(x)];
    }
};
using namespace std;
using ll = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    int q;
    cin >> n >> q;
    vector<array<int, 2>> e(n + 1);
    vector<int> extra;
    UF uf(n);

    vector<vector<array<int, 2>>> g(n);
    for (int i = 0; i <= n; i++) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        e[i] = {u, v};
        if (uf.join(u, v)) {
            g[u].push_back({v, i});
            g[v].push_back({u, i});
        } else {
            extra.push_back(i);
        }
    }


    assert(size(extra) == 2);

    vector<int> pa(n), pai(n), dep(n);

    auto dfs = [&](auto& slf, int u) -> void {
        for (auto [v, i] : g[u]) {
            if (v == pa[u]) continue;
            pa[v] = u;
            pai[v] = i;
            dep[v] = dep[u] + 1;
            slf(slf, v);
        }
    };

    dfs(dfs, 0);

    auto add = [&](int idx) {
        auto [u, v] = e[idx];
        g[u].push_back({v, idx});
        g[v].push_back({u, idx});
    };

    add(extra[0]);
    add(extra[1]);

    vector<int> bad1(n + 1), bad2(n + 1);

    auto go = [&](vector<int>& st, int u, int v) {
        while (u != v) {
            if (dep[u] < dep[v]) swap(u, v);
            st[pai[u]] = 1;
            u = pa[u];
        }
    };

    bad1[extra[0]] = 1;
    go(bad1, e[extra[0]][0], e[extra[0]][1]);

    bad2[extra[1]] = 1;
    go(bad2, e[extra[1]][0], e[extra[1]][1]);
    
    vector<int> t(n);
    set<int> endpoints;
    for (int i = 0; i < n; i++) {
        set<int> st;
        for (auto [v, idx] : g[i]) {
            int tt = bad1[idx] << 1 | bad2[idx];
            t[i] |= tt;
            if (tt) {
                st.insert(tt);
            }
        }
        if (size(st) == 3) endpoints.insert(i);
        // if (b1 && b2) {
        //     assert(midp == -1);
        //     midp = i;
        // }
    }
    vector<int> c(n, -1);
    int cur = 0;
    auto dfs2 = [&](auto& slf, int u, int p) -> int {
        c[u] = cur;
        int is_bad = 0, sum = 0;
        for (auto [v, i] : g[u]) {
            if (v == p) continue;
            if (bad1[i] || bad2[i]) {
                is_bad = 1;
            } else {
                sum += slf(slf, v, u);
            }
        }
        return sum + is_bad;
    };

    int mid = -1;
    for (int i = 0; i < n; i++) {
        if (t[i]) {
            assert(c[i] == -1);
            cur = i;
            int cnt = dfs2(dfs2, i, i);
            assert(cnt == 1 || cnt == 2);
            if (cnt == 2) {
                mid = cur;
            }
        }
    }

    int com = 0, midp = -1;
    for (int i = 0; i < n; i++) {
        com += bad1[i] && bad2[i];
    }

    assert(size(endpoints) == 2 || size(endpoints) == 0);

    if (com == 0) {
        for (int i = 0; i < n; i++) {
            if (t[i] == 3) {
                assert(midp == -1);
                midp = i;
            }
        }
    }

    auto is_mid = [&](int u) {
        return u == midp || c[u] == mid;
    };
    while (q--) {
        int u, v;
        cin >> u >> v;
        u--, v--;

        u = c[u], v = c[v];
        if (c[u] == c[v]) {
            cout << 1 << '\n';
        } else if (is_mid(u) || is_mid(v) || 
                (t[u] == t[v] && t[u] != 0 && com == 0)
                ) {
            cout << 2 << '\n';
        } else if ((t[u] == t[v] && t[u] != 0) || endpoints.count(u) || endpoints.count(v)) {
            cout << 3 << '\n';
        } else {
            cout << "4\n";
        }
    }

    return 0;
}

詳細信息

Test #1:

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

input:

4 6
1 2
1 3
1 4
2 3
2 4
1 2
1 3
1 4
2 3
2 4
3 4

output:

3
3
3
3
3
4

result:

ok 6 lines

Test #2:

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

input:

6 4
1 2
1 3
1 6
2 3
3 4
3 5
4 5
1 2
1 3
1 4
1 6

output:

2
2
4
1

result:

ok 4 lines

Test #3:

score: 0
Accepted
time: 17ms
memory: 8452kb

input:

50000 50000
11561 23122
14261 28523
24407 48814
17947 35894
14803 29607
19557 39115
12415 24830
9583 19167
18397 36794
439 878
18040 36080
17150 34300
7922 15845
18938 37877
18625 37250
6459 12919
9818 19636
3579 7158
21323 42646
23882 47764
13603 27207
8353 16707
15907 31814
20935 41871
11686 23372...

output:

4
3
3
4
4
3
1
3
4
1
3
4
3
4
3
3
1
3
3
3
4
4
4
3
3
3
4
3
3
3
1
3
3
3
3
4
4
4
4
3
4
3
3
3
4
3
4
4
4
4
3
4
4
4
4
3
3
3
4
4
4
4
4
4
3
4
3
4
1
4
1
1
4
3
3
4
3
3
1
4
3
3
4
4
3
3
4
4
4
3
4
3
4
4
4
4
4
3
4
3
3
3
1
3
4
4
3
4
3
4
3
3
4
1
4
3
3
3
4
4
4
4
3
3
3
3
3
4
4
4
4
3
3
4
3
4
4
3
3
4
4
4
1
3
3
3
3
4
4
3
...

result:

ok 50000 lines

Test #4:

score: 0
Accepted
time: 21ms
memory: 8636kb

input:

50000 50000
1730 3460
17535 35071
14108 28216
20630 41260
2091 4182
8112 16225
21373 42746
6685 13371
21142 42284
12168 24337
22564 45128
16103 32207
9254 18508
21369 42739
1955 3911
13696 27392
3929 7858
1777 3555
23382 46764
830 1660
17722 35444
11495 22991
10184 20369
13697 27395
24728 49456
4037...

output:

1
1
3
3
3
4
4
4
4
4
3
3
3
4
1
3
4
3
3
1
3
3
4
3
1
4
3
3
4
3
3
4
4
4
1
1
4
1
3
4
3
1
4
4
3
3
3
4
1
4
4
1
3
1
3
3
3
1
1
3
3
3
3
3
4
3
4
4
3
3
4
4
4
3
3
4
4
4
3
4
3
3
3
3
3
3
3
3
4
4
1
4
3
4
1
4
4
4
4
3
4
1
4
4
3
4
3
4
3
4
3
1
4
3
1
1
3
3
4
4
1
3
3
3
4
3
3
4
4
4
4
4
3
3
4
3
4
1
1
3
4
4
3
4
4
3
3
4
4
3
...

result:

ok 50000 lines

Test #5:

score: -100
Runtime Error

input:

50000 50000
21879 43758
12510 25020
2593 5187
16048 32096
9697 19394
12606 25212
3860 7720
8231 16462
23983 47966
10852 21705
6919 13839
1385 2770
4040 8080
14298 28596
22248 44496
4245 8490
14486 28972
11445 22891
21557 43114
20946 41892
23374 46749
78 157
4617 9234
8198 16396
12228 24456
16125 322...

output:


result: