QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#483916#4580. Bicycle TourThirvin#WA 262ms23760kbC++204.5kb2024-07-19 14:27:022024-07-19 14:27:03

Judging History

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

  • [2024-07-19 14:27:03]
  • 评测
  • 测评结果:WA
  • 用时:262ms
  • 内存:23760kb
  • [2024-07-19 14:27:02]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define sz(x) signed(size(x))

inline void solve() {
        int n, m;
        cin >> n >> m;

        vector<int> a(n);
        for (int i = 0; i < n; i++) cin >> a[i];

        vector<tuple<int, int, int>> edge;
        for (int i = 0; i < m; i++) {
                int u, v;
                cin >> u >> v;
                u--, v--;
                edge.emplace_back(abs(a[u] - a[v]), u, v);
        }
        sort(edge.begin(), edge.end());

        vector<int> to(n), si(n);
        for (int i = 0; i < n; i++) {
                to[i] = i;
                si[i] = 1;
        }
        auto find = [&](int x) -> int {
                while (to[x] != x) {
                        x = to[x];
                }
                return x;
        };
        auto merge = [&](int x, int y) -> void {
                x = find(x);
                y = find(y);
                if (x == y) return;
                if (si[x] < si[y]) swap(x, y);
                si[x] += si[y];
                to[y] = x;
        };

        vector<vector<int>> adj(n);
        vector<tuple<int,int,int>> e;
        for (auto& [val, u, v] : edge) {
                if (find(u) == find(v)) {
                        e.emplace_back(val, u, v);
                        continue;
                }
                merge(u, v);
                adj[u].push_back(v);
                adj[v].push_back(u);
        }

        vector<int> vis(n), lv(n);
        vector dp(n, vector<int>(20));
        auto build = [&](auto build, int x, int lst) -> void {
                dp[x][0] = lst;
                vis[x] = 1;
                for (int i = 1; i < 20; i++) {
                        dp[x][i] = dp[dp[x][i - 1]][i - 1];
                }
                for (auto y : adj[x]) {
                        if (y == lst) continue;
                        lv[y] = lv[x] + 1;
                        build(build, y, x);
                }
        };

        vector<int> rt;
        for (int i = 0; i < n; i++) {
                if (!vis[i]) {
                        rt.push_back(i);
                        build(build, i, i);
                }
        }

        auto upd = [&](int& x, int k) -> void {
                for (int i = 0; k >> i; i++) {
                        if (k >> i & 1) {
                                x = dp[x][i];
                        }
                }
        };
        auto lca = [&](int x, int y) -> int {
                if (lv[x] > lv[y]) upd(x, lv[x] - lv[y]);
                else upd(y, lv[y] - lv[x]);
                if (x == y) return x;
                for (int i = 0; i < 20; i++) {
                        if (dp[x][i] != dp[y][i]) {
                                x = dp[x][i], y = dp[y][i];
                        }
                }
                return dp[x][0];
        };

        vector<vector<pair<int, int>>> ins(n), rm(n);
        int id = 0;
        for (auto [val, u, v] : e) {
                int z = lca(u, v);
                ins[u].push_back({val, id});
                ins[v].push_back({val, id});
                if (dp[z][0] != z) {
                        rm[dp[z][0]].push_back({val, id});
                }
                id++;
        }

        vector<int> res(n, -1);
        auto dfs = [&](auto dfs, int x, int lst) -> set<pair<int,int>> {
                vis[x] = 1;
                set<pair<int, int>> s;
                for (auto val : ins[x]) {
                        s.insert(val);
                }

                for (auto y : adj[x]) {
                        if (y == lst) continue;
                        auto sy = dfs(dfs, y, x);
                        if (s.size() < sy.size()) swap(s, sy);
                        for (auto val : sy) {
                                s.insert(val);
                        }
                }
                for (auto val : rm[x]) {
                        s.erase(val);
                }
                if(s.size()) res[x] = s.begin()->first;
                return s;
        };

        for (int i = 0; i < n; i++) vis[i] = 0;
        for (int i = 0; i < n; i++) {
                if (!vis[i]) {
                        dfs(dfs, i, i);
                }
        }

        for (auto x : res) {
                cout << x << " ";
        }
        cout << '\n';

        return;
}

signed main() {
        cin.tie(0)->sync_with_stdio(0);
        int t = 1;
        while (t--)
                solve();
        return 0;
}

详细

Test #1:

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

input:

8 11
5 2 7 0 10 6 6 6
1 2
1 3
2 3
2 4
2 5
2 7
3 5
1 6
6 7
6 8
7 8

output:

4 4 5 -1 8 0 0 0 

result:

ok single line: '4 4 5 -1 8 0 0 0 '

Test #2:

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

input:

4 5
10 20 30 40
1 2
1 3
1 4
2 3
3 4

output:

20 20 20 30 

result:

ok single line: '20 20 20 30 '

Test #3:

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

input:

5 4
72 35 22 49 108
1 2
2 3
3 4
4 5

output:

-1 -1 -1 -1 -1 

result:

ok single line: '-1 -1 -1 -1 -1 '

Test #4:

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

input:

2 1
10 20
1 2

output:

-1 -1 

result:

ok single line: '-1 -1 '

Test #5:

score: 0
Accepted
time: 25ms
memory: 7664kb

input:

252 31626
825 5234 3578 4723 2145 4362 1861 2413 7203 1939 3210 7153 2155 4559 4403 1466 887 3786 6529 719 4272 3287 5703 6708 2390 4987 4214 770 3487 6230 3498 6255 4963 1093 3065 2961 1663 4857 3224 4284 4228 106 1614 1010 145 1557 4510 1032 4632 155 5570 154 884 1204 2876 6163 5023 4593 7261 3729...

output:

55 33 46 34 10 33 34 30 22 33 33 46 10 34 26 100 14 62 17 9 13 35 81 45 23 29 23 20 11 32 11 32 29 38 18 13 14 0 28 13 11 42 35 22 9 57 22 22 35 7 52 7 14 45 72 92 14 8 15 62 10 20 21 34 66 9 20 24 29 6 30 16 26 9 6 27 8 40 85 17 12 26 29 17 10 61 35 9 39 15 14 14 31 22 63 66 110 24 16 7 30 22 15 18...

result:

ok single line: '55 33 46 34 10 33 34 30 22 33 ...5 15 71 10 52 18 16 30 10 34 4 '

Test #6:

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

input:

30 435
268435456 1024 67108864 16777216 134217728 131072 524288 512 256 8 32 64 33554432 2097152 128 2048 16384 8192 4096 8388608 65536 536870912 4 2 16 1048576 32768 1 262144 4194304
24 28
23 28
10 28
25 28
11 28
12 28
15 28
9 28
8 28
2 28
16 28
19 28
18 28
17 28
27 28
21 28
6 28
28 29
7 28
26 28
1...

output:

201326592 768 50331648 12582912 100663296 98304 393216 384 192 6 24 48 25165824 1572864 96 1536 12288 6144 3072 6291456 49152 402653184 3 3 12 786432 24576 3 196608 3145728 

result:

ok single line: '201326592 768 50331648 1258291... 786432 24576 3 196608 3145728 '

Test #7:

score: 0
Accepted
time: 78ms
memory: 16392kb

input:

430 92235
268435456 64 536870912 256 128 134217728 2 64 33554432 524288 1048576 268435456 1048576 512 32 32 8192 8192 8192 268435456 256 1024 16384 8 2097152 1 4 65536 268435456 32768 2048 512 128 32768 268435456 8192 4194304 16384 1024 4194304 128 33554432 67108864 1 512 16 128 16384 536870912 4 16...

output:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

result:

ok single line: '0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 '

Test #8:

score: -100
Wrong Answer
time: 262ms
memory: 23760kb

input:

4815 185773
999998072 999997632 999995973 999998063 999999530 999999125 999999430 999998145 999999596 999997846 999999214 999998642 999997013 999995796 999999311 999996273 999996607 999996332 999995768 999996225 999996086 999996362 999997090 999997081 999998960 999996645 999996842 999998514 99999527...

output:

30 57 91 47 76 25 193 80 42 120 47 87 30 50 34 219 43 42 97 227 57 40 28 45 141 30 111 84 164 131 99 60 135 181 88 44 147 58 104 199 60 202 24 46 53 56 116 105 79 48 26 34 59 79 52 31 131 166 29 38 35 25 146 119 275 33 30 119 84 95 32 57 280 106 92 47 309 116 52 310 33 85 100 53 114 35 23 84 79 70 7...

result:

wrong answer 1st lines differ - expected: '66 57 91 47 76 25 193 80 42 12... 173 79 85 50 80 71 47 22 56 49', found: '30 57 91 47 76 25 193 80 42 12...173 79 85 50 80 71 30 22 56 49 '