답안 #1012970

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1012970 2024-07-03T03:15:51 Z TrinhKhanhDung Rigged Roads (NOI19_riggedroads) C++14
100 / 100
402 ms 69312 KB
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(),v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define oo (ll)1e18
#define INF (ll)1e9
#define MOD (ll)(998244353)

using namespace std;

template<class T1, class T2>
    bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}

template<class T1, class T2>
    bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}

template<class T1, class T2>
    void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}

template<class T1, class T2>
    void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}

template<class T>
    void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}

const int MAX = 3e5 + 10, LOG = 19;

int N, M;
pair<int, int> edges[MAX];
vector<pair<int, int>> adj[MAX];
int d[MAX][LOG], h[MAX], mark[MAX], ans[MAX], in[MAX], ou[MAX], edgeID[MAX], verID[MAX], timer;

int tree[MAX];

void update(int p, int c){
    while(p <= N){
        tree[p] += c;
        p += p & -p;
    }
}

int getVal(int p){
    int ans = 0;
    while(p){
        ans += tree[p];
        p -= p & -p;
    }
    return ans;
}

void dfs(int u, int p){
    h[u] = h[p] + 1;
    d[u][0] = p;
    for(int i=1; i<LOG; i++){
        d[u][i] = d[d[u][i - 1]][i - 1];
    }
    in[u] = ++timer;
    for(auto o: adj[u]){
        int v = o.fi;
        int id = o.se;
        if(v == p) continue;
        edgeID[v] = id;
        verID[id] = v;
        dfs(v, u);
    }
    ou[u] = timer;
}

int lca(int u, int v){
    if(h[u] < h[v]) swap(u, v);
    int delta = h[u] - h[v];
    for(int i=0; i<LOG; i++) if(BIT(delta, i)){
        u = d[u][i];
    }
    if(u == v) return u;
    for(int i=LOG-1; i>=0; i--) if(d[u][i] != d[v][i]){
        u = d[u][i];
        v = d[v][i];
    }
    return d[u][0];
}

vector<int> node;

void path(int a, int u){
    while(true){
        int val = getVal(in[u]);
        for(int i=LOG-1; i>=0; i--) if(val == getVal(in[d[u][i]])){
            u = d[u][i];
        }
        if(in[u] <= in[a] && ou[u] >= ou[a]) break;
        node.push_back(edgeID[u]);
        update(in[u], -1);
        update(ou[u] + 1, 1);
        u = d[u][0];
        if(getVal(in[u]) == 0) break;
    }
}

void road(int u, int v){
    int a = lca(u, v);
    path(a, u);
    path(a, v);
}

void solve(){
    cin >> N >> M;
    for(int i=1; i<=M; i++){
        int u, v;
        cin >> u >> v;
        edges[i] = make_pair(u, v);
    }
    for(int i=1; i<N; i++){
        int u; cin >> u;
        mark[u] = true;
        adj[edges[u].fi].push_back(make_pair(edges[u].se, u));
        adj[edges[u].se].push_back(make_pair(edges[u].fi, u));
    }

    dfs(1, 0);
//    for(int i=1; i<=N; i++){
//        cout << in[i] << ' ' << ou[i] << ' ' << verID[edgeID[i]] << '\n';
//    }
    for(int i=1; i<=N; i++){
        update(in[i], 1);
        update(ou[i] + 1, -1);
    }
    int cur = 0;
    for(int i=1; i<=M; i++){
        if(mark[i]){
            if(ans[i] != 0) continue;
            ans[i] = ++cur;
            int u = verID[i];
            update(in[u], -1);
            update(ou[u] + 1, 1);
        }
        else{
            node.clear();
            road(edges[i].fi, edges[i].se);
            sort(ALL(node));
            for(int j: node){
                ans[j] = ++cur;
            }
            ans[i] = ++cur;
        }
    }

    for(int i=1; i<=M; i++){
        cout << ans[i] << ' ';
    }
}


int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
//    freopen("split.inp", "r", stdin);
//    freopen("split.out", "w", stdout);

    int tt = 1;
//    cin >> tt;

    while(tt--){
        solve();
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7512 KB Output is correct
2 Correct 3 ms 7520 KB Output is correct
3 Correct 3 ms 7516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7512 KB Output is correct
2 Correct 3 ms 7520 KB Output is correct
3 Correct 3 ms 7516 KB Output is correct
4 Correct 3 ms 7516 KB Output is correct
5 Correct 3 ms 7516 KB Output is correct
6 Correct 3 ms 7516 KB Output is correct
7 Correct 3 ms 7556 KB Output is correct
8 Correct 3 ms 7664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 24520 KB Output is correct
2 Correct 89 ms 31968 KB Output is correct
3 Correct 96 ms 20184 KB Output is correct
4 Correct 112 ms 54704 KB Output is correct
5 Correct 121 ms 57224 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 118 ms 33492 KB Output is correct
2 Correct 115 ms 19032 KB Output is correct
3 Correct 46 ms 13368 KB Output is correct
4 Correct 97 ms 26576 KB Output is correct
5 Correct 29 ms 15196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 186 ms 58116 KB Output is correct
2 Correct 270 ms 65284 KB Output is correct
3 Correct 46 ms 23596 KB Output is correct
4 Correct 62 ms 31844 KB Output is correct
5 Correct 250 ms 69312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 146 ms 41992 KB Output is correct
2 Correct 86 ms 31316 KB Output is correct
3 Correct 274 ms 62036 KB Output is correct
4 Correct 286 ms 57936 KB Output is correct
5 Correct 17 ms 12120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7512 KB Output is correct
2 Correct 3 ms 7520 KB Output is correct
3 Correct 3 ms 7516 KB Output is correct
4 Correct 3 ms 7516 KB Output is correct
5 Correct 3 ms 7516 KB Output is correct
6 Correct 3 ms 7516 KB Output is correct
7 Correct 3 ms 7556 KB Output is correct
8 Correct 3 ms 7664 KB Output is correct
9 Correct 47 ms 24520 KB Output is correct
10 Correct 89 ms 31968 KB Output is correct
11 Correct 96 ms 20184 KB Output is correct
12 Correct 112 ms 54704 KB Output is correct
13 Correct 121 ms 57224 KB Output is correct
14 Correct 118 ms 33492 KB Output is correct
15 Correct 115 ms 19032 KB Output is correct
16 Correct 46 ms 13368 KB Output is correct
17 Correct 97 ms 26576 KB Output is correct
18 Correct 29 ms 15196 KB Output is correct
19 Correct 186 ms 58116 KB Output is correct
20 Correct 270 ms 65284 KB Output is correct
21 Correct 46 ms 23596 KB Output is correct
22 Correct 62 ms 31844 KB Output is correct
23 Correct 250 ms 69312 KB Output is correct
24 Correct 146 ms 41992 KB Output is correct
25 Correct 86 ms 31316 KB Output is correct
26 Correct 274 ms 62036 KB Output is correct
27 Correct 286 ms 57936 KB Output is correct
28 Correct 17 ms 12120 KB Output is correct
29 Correct 402 ms 56788 KB Output is correct
30 Correct 387 ms 58828 KB Output is correct
31 Correct 326 ms 57292 KB Output is correct
32 Correct 188 ms 20052 KB Output is correct
33 Correct 266 ms 57828 KB Output is correct