Submission #137895

# Submission time Handle Problem Language Result Execution time Memory
137895 2019-07-28T13:30:46 Z thebes One-Way Streets (CEOI17_oneway) C++14
100 / 100
253 ms 31492 KB
#include <bits/stdc++.h>
using namespace std;

const int MN = 1e5+5, LG = 19;
int N, M, Q, S, i, x, y, vis[MN][2], sp[MN][LG], cmp[MN], st[MN], nxt, ans[MN], u[MN], d[MN], dep[MN], cnt, vs[MN];
vector<pair<int,int>> adj[MN], cadj[MN];
stack<int> s;
pair<int,int> ree[MN];
string ans2;

void dfs(int n,int id){
    vis[n][0]=vis[n][1]=++nxt;
    st[n]=1; s.push(n);
    for(auto v : adj[n]){
        if(v.second==id) continue;
        if(!vis[v.first][0]){
            dfs(v.first, v.second);
            vis[n][1]=min(vis[n][1],vis[v.first][1]);
        }
        else if(st[v.first]){
            vis[n][1]=min(vis[n][1],vis[v.first][0]);
        }
    }
    if(vis[n][0]==vis[n][1]){
        S++;
        while(s.size()&&s.top()!=n){
            int x = s.top(); s.pop();
            cmp[x] = S; st[x] = 0;
        }
        int x = s.top(); s.pop();
        cmp[x] = S; st[x] = 0;
    }
}

void ddfs(int n,int p,int d){
    sp[n][0] = p; dep[n] = d; vs[n]=1;
    for(auto v : cadj[n]){
        if(v.first==p) continue;
        ddfs(v.first, n, d+1);
    }
}

int lca(int x,int y){
    if(dep[x]<dep[y]) swap(x,y);
    for(int i=LG-1;i>=0;i--){
        if((1<<i)<=dep[x]-dep[y])
            x = sp[x][i];
    }
    if(x==y) return x;
    for(int i=LG-1;i>=0;i--){
        if(sp[x][i]!=sp[y][i]){
            x = sp[x][i];
            y = sp[y][i];
        }
    }
    return sp[x][0];
}

pair<int,int> solve(int n,int p){
    int a=u[n], b=d[n]; vs[n]=1;
    for(auto v : cadj[n]){
        if(v.first==p) continue;
        auto t = solve(v.first, n);
        a += t.first, b += t.second;
    }
    if(a>0){
        for(int i=0;i<cadj[n].size();i++){
            if(cadj[n][i].first==p){
                ans[cadj[n][i].second]=1;
                break;
            }
        }
    }
    else if(b>0){
        for(int i=0;i<cadj[n].size();i++){
            if(cadj[n][i].first==p){
                ans[cadj[n][i].second]=2;
                break;
            }
        }
    }
    return make_pair(a,b);
}

int main(){
    for(scanf("%d%d",&N,&M),i=1;i<=M;i++){
        scanf("%d%d",&x,&y);
        adj[x].push_back({y,i});
        adj[y].push_back({x,i});
        ree[i]={x,y};
    }
    for(i=1;i<=N;i++){
        if(!vis[i][0]) dfs(i,0);
    }
    for(i=1;i<=N;i++){
        for(auto v : adj[i]){
            if(cmp[v.first]!=cmp[i])
                cadj[cmp[i]].push_back({cmp[v.first],v.second});
        }
    }
    for(i=1;i<=N;i++){
        if(!vs[i]) ddfs(i,0,1);
    }
    for(int j=1;j<LG;j++){
        for(i=1;i<=S;i++)
            sp[i][j]=sp[sp[i][j-1]][j-1];
    }
    for(scanf("%d",&Q),i=1;i<=Q;i++){
        scanf("%d%d",&x,&y);
        if(cmp[x]==cmp[y]) continue;
        x = cmp[x], y = cmp[y];
        int a = lca(x,y);
        u[x]++; u[a]--;
        d[y]++; d[a]--;
    }
    memset(vs,0,sizeof(vs));
    for(i=1;i<=N;i++){
        if(!vs[i]) solve(i, 0);
    }
    for(i=1;i<=M;i++){
        if(ans[i]){
            tie(x,y)=ree[i];
            if(dep[cmp[x]]<dep[cmp[y]]){
                if(ans[i]==2) ans2.push_back('R');
                else ans2.push_back('L');
            }
            else{
                if(ans[i]==1) ans2.push_back('R');
                else ans2.push_back('L');
            }
        }
        else ans2.push_back('B');
    }
    printf("%s\n",ans2.c_str());
    return 0;
}

Compilation message

oneway.cpp: In function 'std::pair<int, int> solve(int, int)':
oneway.cpp:67:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i=0;i<cadj[n].size();i++){
                     ~^~~~~~~~~~~~~~~
oneway.cpp:75:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i=0;i<cadj[n].size();i++){
                     ~^~~~~~~~~~~~~~~
oneway.cpp: In function 'int main()':
oneway.cpp:86:28: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(scanf("%d%d",&N,&M),i=1;i<=M;i++){
         ~~~~~~~~~~~~~~~~~~~^~~~
oneway.cpp:87:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d",&x,&y);
         ~~~~~^~~~~~~~~~~~~~
oneway.cpp:108:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(scanf("%d",&Q),i=1;i<=Q;i++){
         ~~~~~~~~~~~~~~^~~~
oneway.cpp:109:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d",&x,&y);
         ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5500 KB Output is correct
2 Correct 6 ms 5496 KB Output is correct
3 Correct 7 ms 5624 KB Output is correct
4 Correct 7 ms 5624 KB Output is correct
5 Correct 7 ms 5596 KB Output is correct
6 Correct 7 ms 5672 KB Output is correct
7 Correct 7 ms 5628 KB Output is correct
8 Correct 7 ms 5624 KB Output is correct
9 Correct 7 ms 5624 KB Output is correct
10 Correct 7 ms 5624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5500 KB Output is correct
2 Correct 6 ms 5496 KB Output is correct
3 Correct 7 ms 5624 KB Output is correct
4 Correct 7 ms 5624 KB Output is correct
5 Correct 7 ms 5596 KB Output is correct
6 Correct 7 ms 5672 KB Output is correct
7 Correct 7 ms 5628 KB Output is correct
8 Correct 7 ms 5624 KB Output is correct
9 Correct 7 ms 5624 KB Output is correct
10 Correct 7 ms 5624 KB Output is correct
11 Correct 60 ms 13260 KB Output is correct
12 Correct 69 ms 15224 KB Output is correct
13 Correct 96 ms 18040 KB Output is correct
14 Correct 105 ms 21988 KB Output is correct
15 Correct 109 ms 23128 KB Output is correct
16 Correct 146 ms 24696 KB Output is correct
17 Correct 146 ms 26668 KB Output is correct
18 Correct 135 ms 25304 KB Output is correct
19 Correct 139 ms 28152 KB Output is correct
20 Correct 71 ms 16504 KB Output is correct
21 Correct 68 ms 16504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5500 KB Output is correct
2 Correct 6 ms 5496 KB Output is correct
3 Correct 7 ms 5624 KB Output is correct
4 Correct 7 ms 5624 KB Output is correct
5 Correct 7 ms 5596 KB Output is correct
6 Correct 7 ms 5672 KB Output is correct
7 Correct 7 ms 5628 KB Output is correct
8 Correct 7 ms 5624 KB Output is correct
9 Correct 7 ms 5624 KB Output is correct
10 Correct 7 ms 5624 KB Output is correct
11 Correct 60 ms 13260 KB Output is correct
12 Correct 69 ms 15224 KB Output is correct
13 Correct 96 ms 18040 KB Output is correct
14 Correct 105 ms 21988 KB Output is correct
15 Correct 109 ms 23128 KB Output is correct
16 Correct 146 ms 24696 KB Output is correct
17 Correct 146 ms 26668 KB Output is correct
18 Correct 135 ms 25304 KB Output is correct
19 Correct 139 ms 28152 KB Output is correct
20 Correct 71 ms 16504 KB Output is correct
21 Correct 68 ms 16504 KB Output is correct
22 Correct 253 ms 27824 KB Output is correct
23 Correct 220 ms 26284 KB Output is correct
24 Correct 204 ms 26528 KB Output is correct
25 Correct 237 ms 31492 KB Output is correct
26 Correct 231 ms 27728 KB Output is correct
27 Correct 211 ms 26424 KB Output is correct
28 Correct 50 ms 9692 KB Output is correct
29 Correct 99 ms 17272 KB Output is correct
30 Correct 100 ms 17528 KB Output is correct
31 Correct 100 ms 17656 KB Output is correct
32 Correct 144 ms 22136 KB Output is correct