Submission #491118

# Submission time Handle Problem Language Result Execution time Memory
491118 2021-11-30T15:32:52 Z socpite One-Way Streets (CEOI17_oneway) C++14
60 / 100
3000 ms 23984 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;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 444 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 2 ms 440 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 2 ms 332 KB Output is correct
10 Correct 1 ms 312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 444 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 2 ms 440 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 2 ms 332 KB Output is correct
10 Correct 1 ms 312 KB Output is correct
11 Correct 60 ms 7508 KB Output is correct
12 Correct 73 ms 9064 KB Output is correct
13 Correct 84 ms 11108 KB Output is correct
14 Correct 128 ms 14960 KB Output is correct
15 Correct 89 ms 16552 KB Output is correct
16 Correct 104 ms 20656 KB Output is correct
17 Correct 117 ms 22640 KB Output is correct
18 Correct 110 ms 20840 KB Output is correct
19 Correct 144 ms 23984 KB Output is correct
20 Correct 75 ms 9068 KB Output is correct
21 Correct 67 ms 8804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 444 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 2 ms 440 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 2 ms 332 KB Output is correct
10 Correct 1 ms 312 KB Output is correct
11 Correct 60 ms 7508 KB Output is correct
12 Correct 73 ms 9064 KB Output is correct
13 Correct 84 ms 11108 KB Output is correct
14 Correct 128 ms 14960 KB Output is correct
15 Correct 89 ms 16552 KB Output is correct
16 Correct 104 ms 20656 KB Output is correct
17 Correct 117 ms 22640 KB Output is correct
18 Correct 110 ms 20840 KB Output is correct
19 Correct 144 ms 23984 KB Output is correct
20 Correct 75 ms 9068 KB Output is correct
21 Correct 67 ms 8804 KB Output is correct
22 Execution timed out 3065 ms 22556 KB Time limit exceeded
23 Halted 0 ms 0 KB -