Submission #877172

# Submission time Handle Problem Language Result Execution time Memory
877172 2023-11-23T02:35:34 Z phong Rigged Roads (NOI19_riggedroads) C++17
100 / 100
293 ms 70092 KB
#include<bits/stdc++.h>

#define ll long long
const int nmax = 3e5 + 5;
const ll oo = 1e18;
const int lg = 20;
const int bs = 16785;
const int ts = 16767;
const ll mod = 1e9 + 7;
#define pii pair<int, int>
#define fi first
#define se second
#define debug(a, n) for(int i = 1; i <= n; ++i) cout << a[i] << ' ';
using namespace std;

int n, m, c[nmax];
vector<pii> adj[nmax];
pii a[nmax];
int ans[nmax], h[nmax], up[nmax][lg + 1];
bool vis[nmax], cc[nmax];
int cost[nmax];
int r[nmax];

int get(int u){
    return r[u] ? r[u] = get(r[u]) : u;
}

void dfs(int u, int p, int W){
    cost[u] = W;
    for(auto [w, v] : adj[u]){
        if(v == p) continue;
        h[v] = h[u] + 1;
        up[v][0] = u;
        for(int j = 1; j <= lg; ++j) up[v][j] = up[up[v][j - 1]][j - 1];
        dfs(v, u, w);
    }
}

int lca(int u, int v){
    if(h[u] != h[v]){
        if(h[u] < h[v]) swap(u, v);
        int k = h[u] - h[v];
        for(int j = 0;j <= __lg(k); ++j){
            if(k >> j & 1){
                u = up[u][j];
            }
        }
    }
    if(u == v) return u;
    for(int j = __lg(h[u]); j >= 0; --j){
        if(up[u][j] != up[v][j]){
            u = up[u][j];
            v = up[v][j];
        }
    }
    return up[u][0];
}

int main(){
    ios_base::sync_with_stdio(NULL);
    cin.tie(0);cout.tie(0);
//    freopen("TRAVEL.inp", "r", stdin);
//    freopen("TRAVEL.out", "w", stdout);
    cin >> n >> m;
    for(int i = 1, u, v; i <= m; ++i){
        cin >> u >> v;
        a[i] = {u, v};
    }
    for(int i = 1, x, u, v; i < n; ++i){
        cin >> x;
        u = a[x].fi, v = a[x].se;
        adj[u].push_back({x, v});
        adj[v].push_back({x, u});
        vis[x] = 1;
    }
    dfs(1, -1, 0);
    int cur = 0;
    vector<int> tmp;
    for(int i = 1, u, v; i <= m; ++i){
        u = a[i].fi, v = a[i].se;
        if(h[u] < h[v]) swap(u, v);
        if(ans[i]) continue;
        if(!vis[i]){
            int x = lca(u, v);
            u = get(u); v = get(v);
            while(h[u] > h[x]){
                int it1 = u;
                int it2 = get(up[u][0]);
                r[it1] = it2;
                tmp.push_back(cost[u]);
                u = get(u);
            }
             while(h[v] > h[x]){
                int it1 = v;
                int it2 = get(up[v][0]);
                r[it1] = it2;
                tmp.push_back(cost[v]);
                v = get(v);
            }
            sort(tmp.begin(), tmp.end());
            for(auto id : tmp) ans[id] = ++cur;
            tmp.clear();
            ans[i] = ++cur;
        }
        else{
            ans[i] = ++cur;
            u = get(u), v = get(v);
            r[u] = v;
        }
    }
    for(int i = 1; i <= m; ++i) cout << ans[i] << ' ';
}
/*
4 3 2000
1 2 1050
2 3 1000
2 4 1030
2 1030
1 1020
1 890


*/
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14940 KB Output is correct
2 Correct 2 ms 14940 KB Output is correct
3 Correct 2 ms 14940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14940 KB Output is correct
2 Correct 2 ms 14940 KB Output is correct
3 Correct 2 ms 14940 KB Output is correct
4 Correct 3 ms 14940 KB Output is correct
5 Correct 3 ms 14940 KB Output is correct
6 Correct 3 ms 14940 KB Output is correct
7 Correct 3 ms 15052 KB Output is correct
8 Correct 3 ms 14940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 29636 KB Output is correct
2 Correct 67 ms 34760 KB Output is correct
3 Correct 58 ms 22932 KB Output is correct
4 Correct 106 ms 57016 KB Output is correct
5 Correct 109 ms 57220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 77 ms 38836 KB Output is correct
2 Correct 45 ms 23632 KB Output is correct
3 Correct 24 ms 20572 KB Output is correct
4 Correct 52 ms 32600 KB Output is correct
5 Correct 22 ms 21852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 155 ms 60072 KB Output is correct
2 Correct 175 ms 68460 KB Output is correct
3 Correct 40 ms 30412 KB Output is correct
4 Correct 60 ms 36952 KB Output is correct
5 Correct 217 ms 70092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 148 ms 47644 KB Output is correct
2 Correct 93 ms 37220 KB Output is correct
3 Correct 274 ms 63220 KB Output is correct
4 Correct 261 ms 60920 KB Output is correct
5 Correct 16 ms 19036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14940 KB Output is correct
2 Correct 2 ms 14940 KB Output is correct
3 Correct 2 ms 14940 KB Output is correct
4 Correct 3 ms 14940 KB Output is correct
5 Correct 3 ms 14940 KB Output is correct
6 Correct 3 ms 14940 KB Output is correct
7 Correct 3 ms 15052 KB Output is correct
8 Correct 3 ms 14940 KB Output is correct
9 Correct 39 ms 29636 KB Output is correct
10 Correct 67 ms 34760 KB Output is correct
11 Correct 58 ms 22932 KB Output is correct
12 Correct 106 ms 57016 KB Output is correct
13 Correct 109 ms 57220 KB Output is correct
14 Correct 77 ms 38836 KB Output is correct
15 Correct 45 ms 23632 KB Output is correct
16 Correct 24 ms 20572 KB Output is correct
17 Correct 52 ms 32600 KB Output is correct
18 Correct 22 ms 21852 KB Output is correct
19 Correct 155 ms 60072 KB Output is correct
20 Correct 175 ms 68460 KB Output is correct
21 Correct 40 ms 30412 KB Output is correct
22 Correct 60 ms 36952 KB Output is correct
23 Correct 217 ms 70092 KB Output is correct
24 Correct 148 ms 47644 KB Output is correct
25 Correct 93 ms 37220 KB Output is correct
26 Correct 274 ms 63220 KB Output is correct
27 Correct 261 ms 60920 KB Output is correct
28 Correct 16 ms 19036 KB Output is correct
29 Correct 285 ms 58924 KB Output is correct
30 Correct 293 ms 60016 KB Output is correct
31 Correct 245 ms 58580 KB Output is correct
32 Correct 87 ms 23384 KB Output is correct
33 Correct 237 ms 59476 KB Output is correct