Submission #116300

# Submission time Handle Problem Language Result Execution time Memory
116300 2019-06-12T04:52:45 Z JohnTitor One-Way Streets (CEOI17_oneway) C++11
100 / 100
249 ms 24952 KB
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, j, k) for(int i=(j); i<=(k); i++)
#define FFOR(i, j, k) for(int i=(j); i<(k); i++)
#define DFOR(i, j, k) for(int i=(j); i>=(k); i--)
#define bug(x) cerr<<#x<<" = "<<(x)<<'\n'
#define pb push_back
#define mp make_pair
#define bit(s, i) (((s)>>(i))&1LL)
#define mask(i) ((1LL<<(i)))
#define builtin_popcount __builtin_popcountll
#define __builtin_popcount __builtin_popcountll
using ll=long long; using ld=long double;
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); const ld pi=acos(0)*2;
template <typename T> inline void read(T &x){char c; bool nega=0; while((!isdigit(c=getchar()))&&(c!='-')); if(c=='-'){nega=1; c=getchar();} x=c-48; while(isdigit(c=getchar())) x=x*10+c-48; if(nega) x=-x;}
template <typename T> inline void writep(T x){if(x>9) writep(x/10); putchar(x%10+48);}
template <typename T> inline void write(T x){if(x<0){ putchar('-'); x=-x;} writep(x);}
template <typename T> inline void writeln(T x){write(x); putchar('\n');}
#define taskname "OneWay"
int n, m, q;
class edge{
public:
    int u, v, x;
    void input(){
        read(u);
        read(v);
        x=u^v;
    }
    int get_v(int u){
        return x^u;
    }
} e[100001];
char ans[100001];
vector <int> g[100001];
bool done[100001];
int p[100001];
int cnt=0;
int num[100001];
int low[100001];
bool b[100001];
int cp[100001];
int pc[100001];
int h[100001];
int f[100001][20];
void dfs(int u){
    cnt++;
    num[u]=low[u]=cnt;
    for(int i: g[u]) if(done[i]) p[u]=i;
    for(int i: g[u]) if(!done[i]){
        done[i]=1;
        int v=e[i].get_v(u);
        if(num[v]) low[u]=min(low[u], num[v]);
        else{
            dfs(v);
            low[u]=min(low[u], low[v]);
        }
    }

    for(int i: g[u]){
        int v=e[i].get_v(u);
        if(p[v]==i) if(low[v]>num[u]) b[i]=1;
    }
}
int r[100001];
int root(int u){
    if(r[u]<0) return u;
    return r[u]=root(r[u]);
}
void unite(int u, int v){
    u=root(u);
    v=root(v);
    if(u==v) return;
    if(r[u]>r[v]) swap(u, v);
    r[u]+=r[v];
    r[v]=u;
}
void dfslca(int u){
    done[u]=1;
    for(int i=0; f[f[u][i]][i]; i++) f[u][i+1]=f[f[u][i]][i];
    for(int i: g[u]){
        int v=e[i].get_v(u);
        if(done[v]) continue;
        p[v]=i;
        f[v][0]=u;
        h[v]=h[u]+1;
        dfslca(v);
    }
}
int lca(int u, int v){
    if(h[u]<h[v]) swap(u, v);
    DFOR(i, __lg(h[u]), 0) if(h[f[u][i]]>=h[v]) u=f[u][i];
    if(u==v) return u;
    DFOR(i, __lg(h[u]), 0) if(f[u][i]!=f[v][i]){
        u=f[u][i];
        v=f[v][i];
    }
    return f[u][0];
}
void dfstree(int u){
    done[u]=1;
    for(int i: g[u]){
        int v=e[i].get_v(u);
        if(p[v]!=i) continue;
        dfstree(v);
        pc[u]=max(pc[u], pc[v]-1);
        cp[u]=max(cp[u], cp[v]-1);
    }
    if(pc[u]){
        assert(cp[u]==0);
        ans[p[u]]="RL"[u==e[p[u]].u];
    }
    else if(cp[u]){
        ans[p[u]]="LR"[u==e[p[u]].u];
    }
}
int main(){
    #ifdef Aria
        if(fopen(taskname".in", "r"))
            freopen(taskname".in", "r", stdin);
    #endif // Aria
    read(n);
    read(m);
    FOR(i, 1, m) ans[i]='B';
    FOR(i, 1, m){
        e[i].input();
        if(e[i].x){
            g[e[i].u].pb(i);
            g[e[i].v].pb(i);
        }
    }
    FOR(i, 1, n) if(!num[i]) dfs(i);
    FOR(i, 1, n) r[i]=-1;
    FOR(i, 1, m) if(!b[i]) unite(e[i].u, e[i].v);
    FOR(i, 1, n) g[i].clear();
    FOR(i, 1, m){
        e[i].u=root(e[i].u);
        e[i].v=root(e[i].v);
        e[i].x=e[i].u^e[i].v;
        if(e[i].x){
            g[e[i].u].pb(i);
            g[e[i].v].pb(i);
        }
    }
    FOR(i, 1, n) done[i]=0;
    FOR(i, 1, n) if((i==root(i))&&(!done[i])){
        p[i]=0;
        h[i]=1;
        dfslca(i);
    }
    read(q);
    {
        int u, v;
        FOR(i, 1, q){
            read(u);
            read(v);
            u=root(u);
            v=root(v);
            int r=lca(u, v);
            cp[u]=max(cp[u], h[u]-h[r]);
            pc[v]=max(pc[v], h[v]-h[r]);
        }
    }
    FOR(i, 1, n) done[i]=0;
    FOR(i, 1, n) if((i==root(i))&&(!done[i])) dfstree(i);
    FOR(i, 1, m) putchar(ans[i]);
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2688 KB Output is correct
2 Correct 4 ms 2688 KB Output is correct
3 Correct 5 ms 2816 KB Output is correct
4 Correct 4 ms 2944 KB Output is correct
5 Correct 4 ms 2944 KB Output is correct
6 Correct 4 ms 2816 KB Output is correct
7 Correct 4 ms 2944 KB Output is correct
8 Correct 5 ms 2944 KB Output is correct
9 Correct 4 ms 2816 KB Output is correct
10 Correct 4 ms 2816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2688 KB Output is correct
2 Correct 4 ms 2688 KB Output is correct
3 Correct 5 ms 2816 KB Output is correct
4 Correct 4 ms 2944 KB Output is correct
5 Correct 4 ms 2944 KB Output is correct
6 Correct 4 ms 2816 KB Output is correct
7 Correct 4 ms 2944 KB Output is correct
8 Correct 5 ms 2944 KB Output is correct
9 Correct 4 ms 2816 KB Output is correct
10 Correct 4 ms 2816 KB Output is correct
11 Correct 35 ms 7288 KB Output is correct
12 Correct 44 ms 9336 KB Output is correct
13 Correct 62 ms 13944 KB Output is correct
14 Correct 75 ms 17656 KB Output is correct
15 Correct 83 ms 18808 KB Output is correct
16 Correct 111 ms 18040 KB Output is correct
17 Correct 115 ms 19704 KB Output is correct
18 Correct 137 ms 19148 KB Output is correct
19 Correct 108 ms 21852 KB Output is correct
20 Correct 42 ms 9336 KB Output is correct
21 Correct 45 ms 12208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2688 KB Output is correct
2 Correct 4 ms 2688 KB Output is correct
3 Correct 5 ms 2816 KB Output is correct
4 Correct 4 ms 2944 KB Output is correct
5 Correct 4 ms 2944 KB Output is correct
6 Correct 4 ms 2816 KB Output is correct
7 Correct 4 ms 2944 KB Output is correct
8 Correct 5 ms 2944 KB Output is correct
9 Correct 4 ms 2816 KB Output is correct
10 Correct 4 ms 2816 KB Output is correct
11 Correct 35 ms 7288 KB Output is correct
12 Correct 44 ms 9336 KB Output is correct
13 Correct 62 ms 13944 KB Output is correct
14 Correct 75 ms 17656 KB Output is correct
15 Correct 83 ms 18808 KB Output is correct
16 Correct 111 ms 18040 KB Output is correct
17 Correct 115 ms 19704 KB Output is correct
18 Correct 137 ms 19148 KB Output is correct
19 Correct 108 ms 21852 KB Output is correct
20 Correct 42 ms 9336 KB Output is correct
21 Correct 45 ms 12208 KB Output is correct
22 Correct 249 ms 21904 KB Output is correct
23 Correct 209 ms 20344 KB Output is correct
24 Correct 227 ms 20344 KB Output is correct
25 Correct 230 ms 24952 KB Output is correct
26 Correct 219 ms 21624 KB Output is correct
27 Correct 194 ms 20344 KB Output is correct
28 Correct 20 ms 6392 KB Output is correct
29 Correct 61 ms 10104 KB Output is correct
30 Correct 65 ms 11128 KB Output is correct
31 Correct 64 ms 10488 KB Output is correct
32 Correct 172 ms 18396 KB Output is correct