QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#65390#4580. Bicycle TourMostafa_MoharramWA 7ms10460kbC++173.2kb2022-11-30 09:59:352022-11-30 09:59:38

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-11-30 09:59:38]
  • 评测
  • 测评结果:WA
  • 用时:7ms
  • 内存:10460kb
  • [2022-11-30 09:59:35]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

using ld = long double;
const int N = 2e5 + 5;
struct Edge {
    int u, v, w;
    Edge(int u, int v, int w): u(u), v(v), w(w) {}
    bool operator < (const Edge &other) const {
        return w < other.w;
    }
};
vector<Edge> e;

const int MP = 19;
vector<int> adj[N];
int P[N], lvl[N];
int lift[MP][N];

struct Dsu {
    int par[N], rnk[N];
    void init() {
        for (int i = 0; i < N; ++i)
            par[i] = i, rnk[i] = 1;
    }
    int findParent(int u){
        return par[u] = (par[u] == u ? u : findParent(par[u]));
    }
    bool merge(int u, int v){
        u = findParent(u);
        v = findParent(v);
        if(u == v)return false;
        if(rnk[u] < rnk[v])
            swap(u,v);
        par[v] = u;
        rnk[u] += rnk[v];
        return true;
    }
    void join(int u, int v){
        u = findParent(u);
        v = findParent(v);
        par[u] = v;
    }
};



void dfs(int u, int pr, int l){
    lift[0][u] = pr;
    for (int p = 1; p < MP; ++p)
        if (~lift[p - 1][u])
            lift[p][u] = lift[p - 1][lift[p - 1][u]];
    P[u] = pr;
    lvl[u] = l;
    for(auto &v: adj[u])
        if(v != pr)
            dfs(v, u,l + 1);
}

int raise(int x, int k) {
    if (k == 0)
        return x;
    for (int i = 0; ~x and i < MP; ++i)
        if ((k >> i) & 1)
            x = lift[i][x];
    return x;
}

int LCA(int u, int v) {
    if (u == v) return u;
    if (lvl[u] < lvl[v])
        swap(u, v);
    u = raise(u, lvl[u] - lvl[v]);
    if (u == v) return u;
    for (int i = MP - 1; i >= 0; --i)
        if (lift[i][u] != lift[i][v]) {
            u = lift[i][u];
            v = lift[i][v];
        }
    return lift[0][u];
}

int main() {
    ios::sync_with_stdio(false); cout.tie(nullptr); cin.tie(nullptr);
    int n , m;
    cin >> n >> m;
    vector<int> h(n + 1);
    for(int i = 1; i <= n; ++i)
        cin >> h[i];
    for(int i = 0; i < m; ++i){
        int u, v, w;
        cin >> u >> v;
        w = abs(h[u] - h[v]);
        e.emplace_back(u,v,w);
    }

    sort(e.begin(), e.end());
    Dsu mst;
    mst.init();
    vector<Edge> notUsed;
    for(int i = 0; i < m; ++i){
        int u,v,w;
        u = e[i].u; v = e[i].v; w = e[i].w;
        if(!mst.merge(u,v)){ // merged before, so skip
            notUsed.emplace_back(e[i]);
        }else{
            adj[u].emplace_back(v);
            adj[v].emplace_back(u);
        }
    }

    lvl[0] = INT_MIN;
    dfs(1, 0, 0);
    vector<int> ans(n + 1, -1);
    mst.init();
    for(int i = 0; i < notUsed.size(); ++i){
        Edge e = notUsed[i];
        int u = e.u, v = e.v, w = e.w;
        u = mst.findParent(u);
        v = mst.findParent(v);
        int lca = LCA(u,v);
        while(u and lvl[u] >= lvl[lca]){
            ans[u] = w;
            mst.join(u, P[u]);
            u = mst.findParent(u);
        }
        while(v and lvl[v] >= lvl[lca]){
            ans[v] = w;
            mst.join(v, P[v]);
            v = mst.findParent(v);
        }
    }
    for(int i = 1; i <= n; ++i)
        cout << ans[i] << ' ';

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 7ms
memory: 9812kb

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

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

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

input:

2 1
10 20
1 2

output:

-1 -1 

result:

ok single line: '-1 -1 '

Test #5:

score: -100
Wrong Answer
time: 6ms
memory: 10460kb

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:

18 33 31 24 10 30 31 30 22 30 28 32 10 9 26 34 14 30 17 9 13 21 47 33 23 22 13 17 11 32 11 26 22 35 18 13 14 0 24 13 11 42 33 22 9 34 22 22 29 7 49 7 14 35 13 34 14 8 15 31 10 20 21 24 66 9 20 24 29 6 30 16 26 9 6 27 8 31 21 17 12 26 18 17 10 33 28 9 33 15 14 14 24 22 49 66 45 24 16 7 23 22 15 18 22...

result:

wrong answer 1st lines differ - expected: '55 33 46 34 10 33 34 30 22 33 ...45 15 71 10 52 18 16 30 10 34 4', found: '18 33 31 24 10 30 31 30 22 30 ...2 15 34 10 48 18 16 30 10 27 4 '