답안 #527055

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
527055 2022-02-16T21:18:57 Z vishesh312 Rigged Roads (NOI19_riggedroads) C++17
100 / 100
826 ms 94476 KB
#include "bits/stdc++.h"
using namespace std;
/*
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using ordered_set = tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>;
*/

#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define sz(x) (int)(x).size()

using ll = long long;
const int mod = 1e9+7;
const int LOG = 20;

vector<pair<int, int>> edges;
vector<vector<int>> adj, lup;
vector<int> dist;

struct UnionFind {
    vector<int> link, siz, up;
    int n;
    UnionFind(int _n) {
        n = _n;
        siz.resize(n, 1);
        link.resize(n);
        iota(all(link), 0);
        up = link;
    }
    int find(int x) {
        if (x == link[x])
            return x;
        return link[x] = find(link[x]);
    }
    bool same(int a, int b) {
        return find(a) == find(b);
    }
    void unite(int a, int b) {
        a = find(a);
        b = find(b);
        if (a == b)
            return;
        if (siz[a] < siz[b])
            swap(a, b);
        siz[a] += siz[b];
        link[b] = a;
        up[a] = (dist[up[a]] < dist[up[b]] ? up[a] : up[b]);
    }
};

void dfs(int u = 0, int p = -1) {
    for (int v: adj[u]) {
        if (v != p) {
            dist[v] = dist[u] + 1;
            lup[v][0] = u;
            for (int i = 1; i < LOG; ++i)
                lup[v][i] = lup[lup[v][i-1]][i-1];
            dfs(v, u);
        }
    }
}

int get_lca(int u, int v) {
    if (dist[u] > dist[v])
        swap(u, v);
    int dd = dist[v] - dist[u];
    for (int i = LOG - 1; i >= 0; --i)
        if (dd & (1 << i))
            v = lup[v][i];
    if (u == v)
        return u;
    for (int i = LOG - 1; i >= 0; --i) {
        if (lup[u][i] != lup[v][i]) {
            u = lup[u][i];
            v = lup[v][i];
        }
    }
    return lup[u][0];
}

void solve(int tc) {
    int n, m;
    cin >> n >> m;
    edges.resize(m); adj.resize(n); dist.resize(n); lup.resize(n, vector<int>(LOG));
    map<pair<int, int>, int> id;
    for (int i = 0; i < m; ++i) {
        auto &[u, v] = edges[i];
        cin >> u >> v;
        --u, --v;
        if (u > v)
            swap(u, v);
        id[make_pair(u, v)] = i;
    }
    vector<bool> is(m);
    for (int i = 0; i < n-1; ++i) {
        int x;
        cin >> x;
        auto [u, v] = edges[x-1];
        adj[u].push_back(v);
        adj[v].push_back(u);
        is[x-1] = true;
    }
    dfs();
    UnionFind uf(n);
    vector<int> we(m);
    int cur = 1;
    for (int i = 0; i < m; ++i) {
        auto [u, v] = edges[i];
        if (we[i])
            continue;
        if (is[i]) {
            we[i] = cur++;
            uf.unite(u, v);
            continue;
        }
        int l = get_lca(u, v);
        vector<int> vec;
        while (dist[uf.up[uf.find(u)]] > dist[l]) {
            int x = uf.up[uf.find(u)];
            vec.push_back(id[{min(x, lup[x][0]), max(x, lup[x][0])}]);
            uf.unite(u, lup[x][0]);
        }
        while (dist[uf.up[uf.find(v)]] > dist[l]) {
            int x = uf.up[uf.find(v)];
            vec.push_back(id[{min(x, lup[x][0]), max(x, lup[x][0])}]);
            uf.unite(v, lup[x][0]);
        }
        sort(all(vec));
        for (int x: vec)
            we[x] = cur++;
        we[i] = cur++;
    }
    for (int x: we)
        cout << x << " ";
}

signed main() {
    cin.tie(0)->sync_with_stdio(0);
    int tc = 1;
    //cin >> tc;
    for (int i = 1; i <= tc; ++i) solve(i);
    return 0;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 312 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 312 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 123 ms 28876 KB Output is correct
2 Correct 244 ms 42864 KB Output is correct
3 Correct 340 ms 29368 KB Output is correct
4 Correct 310 ms 78688 KB Output is correct
5 Correct 358 ms 82708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 223 ms 39332 KB Output is correct
2 Correct 136 ms 20516 KB Output is correct
3 Correct 59 ms 10672 KB Output is correct
4 Correct 162 ms 29164 KB Output is correct
5 Correct 40 ms 11568 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 300 ms 76300 KB Output is correct
2 Correct 389 ms 84540 KB Output is correct
3 Correct 73 ms 24008 KB Output is correct
4 Correct 115 ms 36140 KB Output is correct
5 Correct 373 ms 94476 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 360 ms 50744 KB Output is correct
2 Correct 220 ms 34940 KB Output is correct
3 Correct 585 ms 82428 KB Output is correct
4 Correct 581 ms 76908 KB Output is correct
5 Correct 30 ms 7796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 312 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 123 ms 28876 KB Output is correct
10 Correct 244 ms 42864 KB Output is correct
11 Correct 340 ms 29368 KB Output is correct
12 Correct 310 ms 78688 KB Output is correct
13 Correct 358 ms 82708 KB Output is correct
14 Correct 223 ms 39332 KB Output is correct
15 Correct 136 ms 20516 KB Output is correct
16 Correct 59 ms 10672 KB Output is correct
17 Correct 162 ms 29164 KB Output is correct
18 Correct 40 ms 11568 KB Output is correct
19 Correct 300 ms 76300 KB Output is correct
20 Correct 389 ms 84540 KB Output is correct
21 Correct 73 ms 24008 KB Output is correct
22 Correct 115 ms 36140 KB Output is correct
23 Correct 373 ms 94476 KB Output is correct
24 Correct 360 ms 50744 KB Output is correct
25 Correct 220 ms 34940 KB Output is correct
26 Correct 585 ms 82428 KB Output is correct
27 Correct 581 ms 76908 KB Output is correct
28 Correct 30 ms 7796 KB Output is correct
29 Correct 826 ms 76980 KB Output is correct
30 Correct 801 ms 80436 KB Output is correct
31 Correct 734 ms 80292 KB Output is correct
32 Correct 359 ms 27720 KB Output is correct
33 Correct 674 ms 80064 KB Output is correct