Submission #1095948

# Submission time Handle Problem Language Result Execution time Memory
1095948 2024-10-03T12:46:40 Z Sunbae Rigged Roads (NOI19_riggedroads) C++17
100 / 100
271 ms 109168 KB
#include <cstdio>
#include <vector>
#include <algorithm>
#define F first
#define S second
using namespace std;
const int N = 3e5 + 5, M = 6e5 + 5, MX = 12e5 + 5;
vector<int> g[N];
pair<int,int> Edge[N]; 
tuple<int,int,int> V[N];
int ch[N], sz[N], dep[N], up[N], pos[N], top[N], mn[N], idx[N], A[N], E[M], T[M], ST[M][20], s[MX], ti, mm, n, m, val, L, R, lca;
void init(int ind, int low, int high){
    s[ind] = m;
    if(low == high){ idx[low] = ind; return;}
    int mid = low + ((high-low)>>1);
    init(ind<<1, low, mid); init(ind<<1|1, mid+1, high);
}
void upd(int ind, int low, int high){
    if(low > R || high < L) return;
    if(L <= low && high <= R){ s[ind] = min(s[ind], val); return;}
    int mid = low + ((high-low)>>1);
    upd(ind<<1, low, mid); upd(ind<<1|1, mid+1, high);
}
void dfs(int u){
    sz[E[mm++] = u] = 1;
    for(int& v: g[u]){
        g[v].erase(find(g[v].begin(), g[v].end(), up[v] = u));
        dep[v] = dep[u] + 1; dfs(v); sz[u] += sz[v];
        E[mm++] = u;
        if(sz[v] > sz[g[u][0]]) swap(v, g[u][0]);
    }
}
void efs(int u){
    pos[u] = ti++;
    for(int v: g[u]){
        top[v] = (v == g[u][0]) ? top[u] : v;
        efs(v);
    }
}
int get_lca(int u, int v){
    if(mn[u] > mn[v]) swap(u, v);
    int j = 31 - __builtin_clz(mn[v] - mn[u] + 1);
    return T[min(ST[mn[u]][j], ST[mn[v]-(1<<j)+1][j])];
}
void UPD(int u, int v){
    if(top[u] == top[v]){
        if(dep[u] > dep[v]) swap(u, v);
        L = pos[u] + (u == lca); R = pos[v];
        if(L <= R) upd(1, 0, n-1);
        return;
    }
    if(dep[top[u]] > dep[top[v]]) swap(u, v);
    L = pos[top[v]] + (top[v] == lca); R = pos[v];
    if(L <= R) upd(1, 0, n-1);
    UPD(u, up[top[v]]);
}
signed main(){
    scanf("%d %d", &n, &m);
    for(int i = 0, u, v; i<m; ++i) scanf("%d %d", &u, &v), Edge[i] = {--u, --v};
    for(int i = 0, j; i<n-1; ++i){
        scanf("%d", &j); ch[--j] = 1;
        int u = Edge[j].F, v = Edge[j].S;
        g[u].push_back(v); g[v].push_back(u);
    }
    dfs(0); efs(0);
    for(int i = mm-1; i>=0; --i) T[mn[E[i]] = i] = E[i];
    for(int i = 0; i<mm; ++i) ST[i][0] = mn[E[i]];
    for(int j = 1; j<32 - __builtin_clz(mm); ++j) for(int i = 0; i+(1<<j)-1<mm; ++i) ST[i][j] = min(ST[i][j-1], ST[i+(1<<(j-1))][j-1]);
    init(1, 0, n-1); 
    for(int i = 0; i<m; ++i){
        if(dep[Edge[i].F] > dep[Edge[i].S]) swap(Edge[i].F, Edge[i].S);
        if(!ch[i]) lca = get_lca(Edge[val = i].F, Edge[i].S), UPD(Edge[i].F, Edge[i].S);
    }
    for(int i = 0; i<m; ++i){
        int u = Edge[i].F, v = Edge[i].S;
        if(ch[val = i]) for(int x = idx[pos[v]]; x; x>>=1) val = min(val, s[x]);
        V[i] = {val, !ch[i], i};
    }
    sort(V, V+m);
    for(int i = ti = 0; i<m; ++i) A[get<2>(V[i])] = ++ti;
    for(int i = 0; i<m; ++i) printf("%d ", A[i]);
}

Compilation message

riggedroads.cpp: In function 'int main()':
riggedroads.cpp:75:13: warning: unused variable 'u' [-Wunused-variable]
   75 |         int u = Edge[i].F, v = Edge[i].S;
      |             ^
riggedroads.cpp:58:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   58 |     scanf("%d %d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~~
riggedroads.cpp:59:41: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   59 |     for(int i = 0, u, v; i<m; ++i) scanf("%d %d", &u, &v), Edge[i] = {--u, --v};
      |                                    ~~~~~^~~~~~~~~~~~~~~~~
riggedroads.cpp:61:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   61 |         scanf("%d", &j); ch[--j] = 1;
      |         ~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9564 KB Output is correct
2 Correct 2 ms 9564 KB Output is correct
3 Correct 2 ms 9564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9564 KB Output is correct
2 Correct 2 ms 9564 KB Output is correct
3 Correct 2 ms 9564 KB Output is correct
4 Correct 2 ms 9816 KB Output is correct
5 Correct 2 ms 9840 KB Output is correct
6 Correct 2 ms 9820 KB Output is correct
7 Correct 3 ms 9564 KB Output is correct
8 Correct 2 ms 9820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 60 ms 37584 KB Output is correct
2 Correct 122 ms 47048 KB Output is correct
3 Correct 144 ms 23892 KB Output is correct
4 Correct 174 ms 87072 KB Output is correct
5 Correct 188 ms 90280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 105 ms 51536 KB Output is correct
2 Correct 71 ms 25936 KB Output is correct
3 Correct 36 ms 20060 KB Output is correct
4 Correct 77 ms 39504 KB Output is correct
5 Correct 31 ms 23728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 168 ms 89392 KB Output is correct
2 Correct 202 ms 100368 KB Output is correct
3 Correct 52 ms 35848 KB Output is correct
4 Correct 74 ms 48720 KB Output is correct
5 Correct 219 ms 109168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 122 ms 65108 KB Output is correct
2 Correct 82 ms 47956 KB Output is correct
3 Correct 229 ms 96848 KB Output is correct
4 Correct 198 ms 88400 KB Output is correct
5 Correct 17 ms 17240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9564 KB Output is correct
2 Correct 2 ms 9564 KB Output is correct
3 Correct 2 ms 9564 KB Output is correct
4 Correct 2 ms 9816 KB Output is correct
5 Correct 2 ms 9840 KB Output is correct
6 Correct 2 ms 9820 KB Output is correct
7 Correct 3 ms 9564 KB Output is correct
8 Correct 2 ms 9820 KB Output is correct
9 Correct 60 ms 37584 KB Output is correct
10 Correct 122 ms 47048 KB Output is correct
11 Correct 144 ms 23892 KB Output is correct
12 Correct 174 ms 87072 KB Output is correct
13 Correct 188 ms 90280 KB Output is correct
14 Correct 105 ms 51536 KB Output is correct
15 Correct 71 ms 25936 KB Output is correct
16 Correct 36 ms 20060 KB Output is correct
17 Correct 77 ms 39504 KB Output is correct
18 Correct 31 ms 23728 KB Output is correct
19 Correct 168 ms 89392 KB Output is correct
20 Correct 202 ms 100368 KB Output is correct
21 Correct 52 ms 35848 KB Output is correct
22 Correct 74 ms 48720 KB Output is correct
23 Correct 219 ms 109168 KB Output is correct
24 Correct 122 ms 65108 KB Output is correct
25 Correct 82 ms 47956 KB Output is correct
26 Correct 229 ms 96848 KB Output is correct
27 Correct 198 ms 88400 KB Output is correct
28 Correct 17 ms 17240 KB Output is correct
29 Correct 271 ms 84460 KB Output is correct
30 Correct 245 ms 88144 KB Output is correct
31 Correct 243 ms 88744 KB Output is correct
32 Correct 151 ms 23636 KB Output is correct
33 Correct 242 ms 89964 KB Output is correct