답안 #491119

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
491119 2021-11-30T15:33:22 Z socpite One-Way Streets (CEOI17_oneway) C++14
100 / 100
229 ms 27300 KB
#include<bits/stdc++.h>
using namespace std;

#define maxn 100001
#define f first
#define s second

int n, q, timedfs = 1, ccnt = 0, m;
int sprasetable[maxn][18];
stack<int> s;
vector<vector<pair<int, int>>> graph;
vector<vector<pair<int, int>>> tree;
vector<int> edgeup;
vector<pair<int, int>> edge;
vector<int> ans;
string output;
int level[maxn];
int low[maxn];
int up[maxn];
int pos[maxn];
int num[maxn];

void build(int x){
    if(x == 18)return;
    for(int i = 1; i <= ccnt; i++){
        if(sprasetable[i][x-1] != -1)sprasetable[i][x] = sprasetable[sprasetable[i][x-1]][x-1];
        else sprasetable[i][x] = -1;
    }
    build(x+1);
}

void DFSt(int x, int p){
    low[x]=num[x]=timedfs++;
    s.push(x);
    for(auto u: graph[x]){
        if(u.s == p)continue;
        if(!num[u.f]){
            DFSt(u.f, u.s);
            low[x]=min(low[u.f], low[x]);
        }
        else{
            if(num[u.f] < num[x])low[x]=min(low[x], low[u.f]);
            else low[u.f]=min(low[u.f], num[x]);
        }
    }
    if(low[x] == num[x]){
        ccnt++;
        while(!s.empty() && s.top() != x){
            pos[s.top()]=ccnt;
            s.pop();
        }
        if(!s.empty()){
            pos[s.top()]=ccnt;
            s.pop();
        }
    }
}

int travelup(int x, int distance){
    for(int i = 0; i < 18; i++){
        if(distance & (1 << i))x = sprasetable[x][i];
        if(x == -1)return -1;
    }
    return x;
}

int query(int a, int b){
    if(level[a] < level[b])swap(a, b);
    int distance = level[a]-level[b];
    a = travelup(a, distance);
    int curr=17;
    for(int i = curr; i >= 0; i--){
        if(sprasetable[a][i] != sprasetable[b][i]){
            a = sprasetable[a][i];
            b = sprasetable[b][i];
        }
    }
    if(a == b)return a;
    else return sprasetable[a][0];
}

void DFS(int x, int p){
    sprasetable[x][0]=p;
    for(auto u: tree[x]){
        if(u.f == p)continue;
        level[u.f]=level[x]+1;
        edgeup[u.f]=u.s;
        DFS(u.f, x);
    }
}

int Find(int x){
    if(up[x] < 0)return x;
    else{
        up[x]=Find(up[x]);
        return up[x];
    }
}

void Union(int a, int b){
    a = Find(a);
    b = Find(b);
    up[a]=b;
}

int main(){
    cin >> n >> m;
    graph.resize(n+1);
    edge.resize(m);
    ans.assign(m, 0);
    for(int i = 0; i < m; i++){
        int a, b;
        cin >> a >> b;
        graph[a].push_back({b, i});
        graph[b].push_back({a, i});
        edge[i]={a, b};
    }
    for(int i = 1; i <= n; i++)if(!pos[i])DFSt(i, -1);
    graph.clear();
    edgeup.resize(n+1);
    tree.resize(ccnt+1);
    for(int i = 0; i < m; i++){
        int a = pos[edge[i].f];
        int b = pos[edge[i].s];
        if(a != b){
            tree[a].push_back({b, i});
            tree[b].push_back({a, i});
        }
    }
    for(int i = 1; i <= ccnt; i++)if(!sprasetable[i][0])DFS(i, -1);
    for(int i = 1; i <= n; i++)up[i]=-1;
    //for(int i = 1; i <= n; i++)cout << pos[i] << endl;
    build(1);
    cin >> q;
    for(int i = 0; i < q; i++){
        int a, b;
        cin >> a >> b;
        a=pos[a];
        b=pos[b];
        int anc = query(a, b);
        a = Find(a);
        while(level[anc] < level[a]){
            ans[edgeup[a]]=1;
            Union(a, sprasetable[a][0]);
            a = sprasetable[a][0];
            a = Find(a);
        }
        b = Find(b);
        while(level[anc] < level[b]){
            ans[edgeup[b]]=-1;
            Union(b, sprasetable[b][0]);
            b = sprasetable[b][0];
            b = Find(b);
        }
    }
    for(int i = 0; i < m; i++){
        if(!ans[i])output+='B';
        else{
            int tmp = ans[i];
            if(edgeup[pos[edge[i].f]] == i)tmp*=-1;
            if(tmp == 1)output+='L';
            else output+='R';
        }
    }
    cout << output << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 2 ms 460 KB Output is correct
6 Correct 1 ms 324 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 2 ms 460 KB Output is correct
6 Correct 1 ms 324 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 66 ms 6468 KB Output is correct
12 Correct 93 ms 7856 KB Output is correct
13 Correct 76 ms 9964 KB Output is correct
14 Correct 82 ms 13828 KB Output is correct
15 Correct 93 ms 15360 KB Output is correct
16 Correct 114 ms 19592 KB Output is correct
17 Correct 103 ms 21420 KB Output is correct
18 Correct 153 ms 19564 KB Output is correct
19 Correct 99 ms 22792 KB Output is correct
20 Correct 80 ms 7948 KB Output is correct
21 Correct 67 ms 7628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 2 ms 460 KB Output is correct
6 Correct 1 ms 324 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 66 ms 6468 KB Output is correct
12 Correct 93 ms 7856 KB Output is correct
13 Correct 76 ms 9964 KB Output is correct
14 Correct 82 ms 13828 KB Output is correct
15 Correct 93 ms 15360 KB Output is correct
16 Correct 114 ms 19592 KB Output is correct
17 Correct 103 ms 21420 KB Output is correct
18 Correct 153 ms 19564 KB Output is correct
19 Correct 99 ms 22792 KB Output is correct
20 Correct 80 ms 7948 KB Output is correct
21 Correct 67 ms 7628 KB Output is correct
22 Correct 206 ms 22188 KB Output is correct
23 Correct 228 ms 21832 KB Output is correct
24 Correct 229 ms 21804 KB Output is correct
25 Correct 212 ms 27300 KB Output is correct
26 Correct 229 ms 23224 KB Output is correct
27 Correct 205 ms 21956 KB Output is correct
28 Correct 73 ms 4164 KB Output is correct
29 Correct 131 ms 9684 KB Output is correct
30 Correct 130 ms 9924 KB Output is correct
31 Correct 147 ms 10352 KB Output is correct
32 Correct 183 ms 15088 KB Output is correct