Submission #545661

# Submission time Handle Problem Language Result Execution time Memory
545661 2022-04-05T06:34:25 Z balbit Rigged Roads (NOI19_riggedroads) C++14
100 / 100
359 ms 73640 KB
#include <bits/stdc++.h>
#define int ll
using namespace std;

#define ll long long
#define f first
#define s second
#define pii pair<int, int>

#define MX(a,b) a=max(a,b)
#define MN(a,b) a=min(a,b)
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()

#define REP(i,n) for (int i = 0; i<n; ++i)
#define REP1(i,n) for (int i = 1; i<=n; ++i)
#define pb push_back

#ifdef BALBIT
#define bug(...) cerr<<"#"<<__LINE__<<": "<<#__VA_ARGS__<<"- ", _do(__VA_ARGS__)
template<typename T> void _do( T && x) {cerr<<x<<endl;}
template<typename T, typename ...S> void _do( T && x, S && ...y) {cerr<<x<<", "; _do(y...);}
#else
#define bug(...)
#define endl '\n'
#endif // BALBIT

const int maxn = 3e5+5;

vector<pii> tree[maxn];
struct edge{
    int u,v,id;
};
vector<edge> G;
bool treeedge[maxn];
int par[maxn], parid[maxn], dep[maxn], L[maxn], R[maxn];
int dsupar[maxn], head[maxn];
int IT =0;
bool headdone[maxn];

void dfs(int v, int p) {
    L[v] = R[v] = IT++;
    for (pii e : tree[v]) {
        int u = e.f;
        if (u!= p) {
            par[u] = v; parid[u] = e.s;
            dep[u] = dep[v] + 1;
            dfs(u,v);
            R[v] = R[u];
        }
    }
}

int find(int x) {
    return x == dsupar[x] ? x : dsupar[x] = find(dsupar[x]);
}

inline bool isanc(int a, int b) {return L[a] <= L[b] && R[a] >= R[b];}

void mrg(int a, int b) {
    a = find(a); b = find(b);
    if (a==b) return;
    if (dep[head[a]] < dep[head[b]]) {
        dsupar[b] = a;
    }else{
        dsupar[a] = b;
    }
}

int AT = 1;
int ans[maxn];

void work(int a, int b, int myid) {
    bug(a,b,myid);
    bug(a, find(a), head[find(a)]);
    if (treeedge[myid]) {
        if (ans[myid] > 0) return;
        ans[myid] = AT++;
        return;
    }
    vector<int> ids;
    REP(tt, 2) {
        int x = a;
        while (1) {
            x = head[find(x)];
            bug(x, b);
            if (!isanc(x,b)) {
                headdone[x] = 1;
                ids.pb(parid[x]);
                mrg(x, par[x]);
            }else break;
        }
        swap(a,b);
    }
    bug(SZ(ids));
    sort(ALL(ids));
    for (int x : ids) bug(x);
    for (int x : ids) {
        if (ans[x]) continue;
        ans[x] = AT++;
    }
    ans[myid] = AT++;
}

signed main(){
    ios::sync_with_stdio(0), cin.tie(0);
    bug(1,2);

    int n,m; cin>>n>>m;
    REP(i,m) {
        int a,b; cin>>a>>b; --a; --b;
        G.pb({a,b,i});
    }
    REP(i, n-1) {
        int s; cin>>s;
        --s;
        treeedge[s] = 1;
        tree[G[s].u].pb({G[s].v, s});
        tree[G[s].v].pb({G[s].u, s});
    }
    bug("hey");
    REP(i,n) {
        dsupar[i] = i;
        head[i] = i;
    }
    headdone[0] = 1;
    dfs(0, -1);
    REP(i,n) {
        bug(i, L[i], R[i]);
    }
    bug("yo");
    REP(i, SZ(G)) {
        work(G[i].u, G[i].v, i);
    }
    REP(i,m) {
        cout<<ans[i]<<' ';
    }
        cout<<endl;
}

Compilation message

riggedroads.cpp: In function 'void work(long long int, long long int, long long int)':
riggedroads.cpp:97:14: warning: unused variable 'x' [-Wunused-variable]
   97 |     for (int x : ids) bug(x);
      |              ^
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7376 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7376 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 5 ms 7504 KB Output is correct
5 Correct 5 ms 7508 KB Output is correct
6 Correct 4 ms 7504 KB Output is correct
7 Correct 4 ms 7496 KB Output is correct
8 Correct 5 ms 7508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 22964 KB Output is correct
2 Correct 98 ms 30468 KB Output is correct
3 Correct 88 ms 23256 KB Output is correct
4 Correct 140 ms 53828 KB Output is correct
5 Correct 152 ms 55632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 80 ms 33608 KB Output is correct
2 Correct 50 ms 19892 KB Output is correct
3 Correct 27 ms 13808 KB Output is correct
4 Correct 59 ms 26932 KB Output is correct
5 Correct 24 ms 14968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 243 ms 60204 KB Output is correct
2 Correct 259 ms 67240 KB Output is correct
3 Correct 54 ms 24492 KB Output is correct
4 Correct 103 ms 32796 KB Output is correct
5 Correct 311 ms 73640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 182 ms 42440 KB Output is correct
2 Correct 107 ms 30900 KB Output is correct
3 Correct 315 ms 60020 KB Output is correct
4 Correct 308 ms 57288 KB Output is correct
5 Correct 20 ms 11732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7376 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 5 ms 7504 KB Output is correct
5 Correct 5 ms 7508 KB Output is correct
6 Correct 4 ms 7504 KB Output is correct
7 Correct 4 ms 7496 KB Output is correct
8 Correct 5 ms 7508 KB Output is correct
9 Correct 52 ms 22964 KB Output is correct
10 Correct 98 ms 30468 KB Output is correct
11 Correct 88 ms 23256 KB Output is correct
12 Correct 140 ms 53828 KB Output is correct
13 Correct 152 ms 55632 KB Output is correct
14 Correct 80 ms 33608 KB Output is correct
15 Correct 50 ms 19892 KB Output is correct
16 Correct 27 ms 13808 KB Output is correct
17 Correct 59 ms 26932 KB Output is correct
18 Correct 24 ms 14968 KB Output is correct
19 Correct 243 ms 60204 KB Output is correct
20 Correct 259 ms 67240 KB Output is correct
21 Correct 54 ms 24492 KB Output is correct
22 Correct 103 ms 32796 KB Output is correct
23 Correct 311 ms 73640 KB Output is correct
24 Correct 182 ms 42440 KB Output is correct
25 Correct 107 ms 30900 KB Output is correct
26 Correct 315 ms 60020 KB Output is correct
27 Correct 308 ms 57288 KB Output is correct
28 Correct 20 ms 11732 KB Output is correct
29 Correct 341 ms 56420 KB Output is correct
30 Correct 359 ms 58312 KB Output is correct
31 Correct 331 ms 54908 KB Output is correct
32 Correct 78 ms 23008 KB Output is correct
33 Correct 315 ms 54984 KB Output is correct