Submission #255409

# Submission time Handle Problem Language Result Execution time Memory
255409 2020-07-31T21:45:41 Z giorgikob One-Way Streets (CEOI17_oneway) C++14
100 / 100
279 ms 24956 KB
#include<bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define ll long long
using namespace std;

const int N = 1e5 + 5;

int n,m,p;
int x,y;
int P[N][21];
vector<pair<int,int>>gr[N];
pair<int,int> edge[N];
char answer[N];
int in[N], out[N];
int timer, root;
vector<int> roots;
int A[N], B[N], fix[N], fixA[N], fixB[N], lvl[N], dp[N];

void dfs(int x,int par,int pp){

        timer++;
        in[x] = timer;
        P[x][0] = pp;
        for(int i = 1; i <= 20; i++){
                P[x][i] = P[P[x][i-1]][i-1];
        }
        //cout << x << endl;
        fix[x] = 1;
        for(auto p : gr[x]){
                int to = p.ff;
                int ind = p.ss;
                if(!lvl[to]){
                        lvl[to] = lvl[x] + 1;
                        dfs(to,ind,x);
                        dp[x] += dp[to];
                        continue;
                }
                if(ind == par) continue;
                if(lvl[to] < lvl[x]){
                        dp[x]++;
                } else {
                        dp[x]--;
                }
        }

        //cout << dp[x] << " " << x << endl;
        //if(dp[x] == 0 && x != root){
                //cout << x << endl;
        //}
        out[x] = timer;
}

inline bool check(int x,int y){
        return in[x] <= in[y] && out[x] >= out[y];
}

inline int lca(int x,int y){

        if(check(x,y)) return x;
        if(check(y,x)) return y;

        for(int i = 20; i >= 0; i--){
                if(P[x][i] && !check(P[x][i],y)){
                        x = P[x][i];
                }
        }

        return P[x][0];
}

void dfsA(int x,int ind){

        fixA[x] = 1;
        for(auto p : gr[x]){
                int to = p.ff;
                int ind = p.ss;
                if(fixA[to]) continue;
                dfsA(to,ind);
                //cout << to << "<---" << x << endl;
                A[x] += A[to];
        }
        //cout << A[x] << " " << x<< endl;
        if(A[x] && dp[x] == 0){
                //cout << x << endl;
                if(edge[ind].ff == x) answer[ind] = 'R'; else answer[ind] = 'L';
        }
}

void dfsB(int x,int ind){

        fixB[x] = 1;
        for(auto p : gr[x]){
                int to = p.ff;
                int ind = p.ss;
                if(fixB[to]) continue;
                dfsB(to,ind);
                B[x] += B[to];
        }

        if(B[x] && dp[x] == 0){
                if(edge[ind].ff == x) answer[ind] = 'L'; else answer[ind] = 'R';
        }
}

int main(){

        cin >> n >> m;
        for(int i = 1; i <= m; i++){
                int x,y;
                cin >> x >> y;
                edge[i] = {x,y};
                gr[x].pb({y,i});
                gr[y].pb({x,i});
        }

        for(int i = 1; i <= n; i++){
                if(fix[i]) continue;
                root = i; lvl[i] = 1;
                roots.pb(root);
                dfs(i,0,0);
        }
        //cout << "done dfs" << endl;

        cin >> p;
        while(p--){
                int x,y;
                cin >> x >> y;
                int z = lca(x,y); //cout << z << endl;
                A[x] ++; A[z] --;
                B[y] ++; B[z] --;
        }

        //cout << "done p" << endl;

        for(int i = 1; i <= m; i++) answer[i] = 'B';

        for(auto root : roots){
                dfsA(root, 0);
        }

        //cout << "done dfsA" << endl;

        for(auto root : roots){
                dfsB(root, 0);
        }

        for(int i = 1; i <= m; i++){
                cout << answer[i] ;
        }

        cout << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2688 KB Output is correct
2 Correct 2 ms 2688 KB Output is correct
3 Correct 3 ms 2816 KB Output is correct
4 Correct 3 ms 2944 KB Output is correct
5 Correct 3 ms 2944 KB Output is correct
6 Correct 3 ms 2816 KB Output is correct
7 Correct 3 ms 2944 KB Output is correct
8 Correct 3 ms 2944 KB Output is correct
9 Correct 3 ms 2816 KB Output is correct
10 Correct 3 ms 2816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2688 KB Output is correct
2 Correct 2 ms 2688 KB Output is correct
3 Correct 3 ms 2816 KB Output is correct
4 Correct 3 ms 2944 KB Output is correct
5 Correct 3 ms 2944 KB Output is correct
6 Correct 3 ms 2816 KB Output is correct
7 Correct 3 ms 2944 KB Output is correct
8 Correct 3 ms 2944 KB Output is correct
9 Correct 3 ms 2816 KB Output is correct
10 Correct 3 ms 2816 KB Output is correct
11 Correct 105 ms 10616 KB Output is correct
12 Correct 124 ms 12664 KB Output is correct
13 Correct 150 ms 15664 KB Output is correct
14 Correct 173 ms 19644 KB Output is correct
15 Correct 183 ms 20848 KB Output is correct
16 Correct 159 ms 20344 KB Output is correct
17 Correct 166 ms 21496 KB Output is correct
18 Correct 168 ms 20308 KB Output is correct
19 Correct 165 ms 22432 KB Output is correct
20 Correct 131 ms 14520 KB Output is correct
21 Correct 121 ms 14200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2688 KB Output is correct
2 Correct 2 ms 2688 KB Output is correct
3 Correct 3 ms 2816 KB Output is correct
4 Correct 3 ms 2944 KB Output is correct
5 Correct 3 ms 2944 KB Output is correct
6 Correct 3 ms 2816 KB Output is correct
7 Correct 3 ms 2944 KB Output is correct
8 Correct 3 ms 2944 KB Output is correct
9 Correct 3 ms 2816 KB Output is correct
10 Correct 3 ms 2816 KB Output is correct
11 Correct 105 ms 10616 KB Output is correct
12 Correct 124 ms 12664 KB Output is correct
13 Correct 150 ms 15664 KB Output is correct
14 Correct 173 ms 19644 KB Output is correct
15 Correct 183 ms 20848 KB Output is correct
16 Correct 159 ms 20344 KB Output is correct
17 Correct 166 ms 21496 KB Output is correct
18 Correct 168 ms 20308 KB Output is correct
19 Correct 165 ms 22432 KB Output is correct
20 Correct 131 ms 14520 KB Output is correct
21 Correct 121 ms 14200 KB Output is correct
22 Correct 279 ms 22648 KB Output is correct
23 Correct 265 ms 21368 KB Output is correct
24 Correct 278 ms 21496 KB Output is correct
25 Correct 277 ms 24956 KB Output is correct
26 Correct 276 ms 22264 KB Output is correct
27 Correct 277 ms 21368 KB Output is correct
28 Correct 120 ms 6776 KB Output is correct
29 Correct 230 ms 15352 KB Output is correct
30 Correct 230 ms 15352 KB Output is correct
31 Correct 237 ms 15608 KB Output is correct
32 Correct 239 ms 18808 KB Output is correct