답안 #255412

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
255412 2020-07-31T21:47:05 Z giorgikob One-Way Streets (CEOI17_oneway) C++14
100 / 100
284 ms 22776 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];
        }

        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]--;
                }
        }

        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);
                A[x] += A[to];
        }

        if(A[x] && dp[x] == 0){
                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);
        }

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

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

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


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

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

        cout << endl;
}
# 결과 실행 시간 메모리 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 2 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 4 ms 2816 KB Output is correct
# 결과 실행 시간 메모리 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 2 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 4 ms 2816 KB Output is correct
11 Correct 116 ms 9464 KB Output is correct
12 Correct 128 ms 11512 KB Output is correct
13 Correct 142 ms 14584 KB Output is correct
14 Correct 177 ms 18424 KB Output is correct
15 Correct 175 ms 19576 KB Output is correct
16 Correct 188 ms 19192 KB Output is correct
17 Correct 163 ms 20344 KB Output is correct
18 Correct 162 ms 19064 KB Output is correct
19 Correct 162 ms 21240 KB Output is correct
20 Correct 128 ms 13304 KB Output is correct
21 Correct 125 ms 13048 KB Output is correct
# 결과 실행 시간 메모리 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 2 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 4 ms 2816 KB Output is correct
11 Correct 116 ms 9464 KB Output is correct
12 Correct 128 ms 11512 KB Output is correct
13 Correct 142 ms 14584 KB Output is correct
14 Correct 177 ms 18424 KB Output is correct
15 Correct 175 ms 19576 KB Output is correct
16 Correct 188 ms 19192 KB Output is correct
17 Correct 163 ms 20344 KB Output is correct
18 Correct 162 ms 19064 KB Output is correct
19 Correct 162 ms 21240 KB Output is correct
20 Correct 128 ms 13304 KB Output is correct
21 Correct 125 ms 13048 KB Output is correct
22 Correct 284 ms 20344 KB Output is correct
23 Correct 265 ms 19064 KB Output is correct
24 Correct 262 ms 19192 KB Output is correct
25 Correct 270 ms 22776 KB Output is correct
26 Correct 275 ms 19960 KB Output is correct
27 Correct 270 ms 19064 KB Output is correct
28 Correct 119 ms 5624 KB Output is correct
29 Correct 244 ms 13048 KB Output is correct
30 Correct 244 ms 13176 KB Output is correct
31 Correct 252 ms 13432 KB Output is correct
32 Correct 234 ms 16376 KB Output is correct