Submission #960374

# Submission time Handle Problem Language Result Execution time Memory
960374 2024-04-10T10:49:24 Z noyancanturk One-Way Streets (CEOI17_oneway) C++17
100 / 100
74 ms 25532 KB
#ifndef Local
    #pragma GCC optimize("O3,unroll-loops")
#endif
    
#include "bits/stdc++.h"
using namespace std;
    
#define pb push_back
#define int int64_t
    
const int lim=1e5+100;
int mod=998244353;
//const int mod=(1ll<<61)-1;
 
using pii=pair<int,int>;

bool isbridge[lim];
int tin[lim],tt=1;
vector<pii>v[lim];

int dfs(int node,int camefrom=lim-1){
    tin[node]=tt++;
    int seen=INT_MAX;
    for(int j=0;j<size(v[node]);j++){
        if(v[node][j].second^camefrom){
            if(!tin[v[node][j].first]){
                seen=min(seen,dfs(v[node][j].first,v[node][j].second));
            }else{
                seen=min(seen,tin[v[node][j].first]);
            }
        }
    }
    //cerr<<node<<" "<<tin[node]<<" "<<seen<<"\n";
    if(tin[node]<=seen){
        isbridge[camefrom]=1;
    }
    return seen;
}

int comp[lim],cnt=1;

void groupall(int node){
    comp[node]=cnt;
    for(int j=0;j<size(v[node]);j++){
        if(!comp[v[node][j].first]&&!isbridge[v[node][j].second]){
            groupall(v[node][j].first);
        }
    }
}

vector<pii>to[lim];
vector<pii>edges;

int vals[lim];
char ans[lim];
bool vis[lim];

int compdfs(int node,int camefrom=lim-1){
    vis[node]=1;
    //cerr<<node<<" "<<camefrom<<" visiting\n";
    for(auto[j,i]:to[node]){
        if(i^camefrom){
            vals[node]+=compdfs(j,i);
        }
    }
    if(0<vals[node]){
        ans[camefrom]=(node==comp[edges[camefrom].first])?'R':'L';
    }
    if(vals[node]<0){
        ans[camefrom]=(node==comp[edges[camefrom].first])?'L':'R';
    }
    //cerr<<node<<" "<<vals[node]<<"\n";
    return vals[node];
}

void solve(){
    int n,m;
    cin>>n>>m;
    edges.pb({0,0});
    for(int i=1;i<=m;i++){
        int x,y;
        cin>>x>>y;
        v[x].pb({y,i});
        v[y].pb({x,i});
        edges.pb({x,y});
        //cerr<<i<<" | "<<x<<" "<<y<<"\n";
    }
    for(int i=1;i<=n;i++){
        if(!tin[i]){
            dfs(i);
        }
    }
    for(int i=1;i<=n;i++){
        if(!comp[i]){
            groupall(i);
            cnt++;
        }
    }
    for(int i=1;i<=m;i++){
        if(isbridge[i]){
            //cerr<<i<<"\n";
            //cerr<<i<<" | "<<edges[i].first<<" "<<edges[i].second<<" | new bridge\n";
            to[comp[edges[i].first]].pb({comp[edges[i].second],i});
            to[comp[edges[i].second]].pb({comp[edges[i].first],i});
        }
    }
    //cerr<<"ok\n";
    for(int i=1;i<=m;i++){
        ans[i]='B';
    }
    /*
    for(int i=1;i<=n;i++){
        cerr<<comp[i]<<" ";
    }cerr<<"\n";
    for(int i=1;i<=cnt;i++){
        cerr<<i<<" :\n";
        for(auto[j,c]:to[i]){
            cerr<<j<<" "<<c<<"\n";
        }cerr<<"\n";
    }
    */
    int q;
    cin>>q;
    for(int i=0;i<q;i++){
        int x,y;
        cin>>x>>y;
        int aa=comp[x],bb=comp[y];
        vals[aa]++,vals[bb]--;
    }
    for(int i=1;i<=cnt;i++){
        if(!vis[i])compdfs(i);
    }
    for(int i=1;i<=m;i++){
        cout<<ans[i];
    }
}
    
signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
#ifdef Local  
    freopen(".in","r",stdin);
    freopen(".out","w",stdout);
#endif
    int t=1;
    //cin>>t;
    while (t--)
    {
        solve();
    }
}

Compilation message

oneway.cpp: In function 'int64_t dfs(int64_t, int64_t)':
oneway.cpp:24:18: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<std::pair<long int, long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 |     for(int j=0;j<size(v[node]);j++){
      |                 ~^~~~~~~~~~~~~~
oneway.cpp: In function 'void groupall(int64_t)':
oneway.cpp:44:18: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<std::pair<long int, long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   44 |     for(int j=0;j<size(v[node]);j++){
      |                 ~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 6744 KB Output is correct
2 Correct 2 ms 6748 KB Output is correct
3 Correct 2 ms 6748 KB Output is correct
4 Correct 2 ms 6748 KB Output is correct
5 Correct 3 ms 6800 KB Output is correct
6 Correct 2 ms 6864 KB Output is correct
7 Correct 2 ms 6748 KB Output is correct
8 Correct 2 ms 6748 KB Output is correct
9 Correct 2 ms 6748 KB Output is correct
10 Correct 2 ms 6748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 6744 KB Output is correct
2 Correct 2 ms 6748 KB Output is correct
3 Correct 2 ms 6748 KB Output is correct
4 Correct 2 ms 6748 KB Output is correct
5 Correct 3 ms 6800 KB Output is correct
6 Correct 2 ms 6864 KB Output is correct
7 Correct 2 ms 6748 KB Output is correct
8 Correct 2 ms 6748 KB Output is correct
9 Correct 2 ms 6748 KB Output is correct
10 Correct 2 ms 6748 KB Output is correct
11 Correct 36 ms 15552 KB Output is correct
12 Correct 32 ms 16576 KB Output is correct
13 Correct 46 ms 17336 KB Output is correct
14 Correct 44 ms 18108 KB Output is correct
15 Correct 45 ms 18628 KB Output is correct
16 Correct 60 ms 20768 KB Output is correct
17 Correct 57 ms 21904 KB Output is correct
18 Correct 58 ms 20672 KB Output is correct
19 Correct 50 ms 22932 KB Output is correct
20 Correct 32 ms 15592 KB Output is correct
21 Correct 32 ms 15404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 6744 KB Output is correct
2 Correct 2 ms 6748 KB Output is correct
3 Correct 2 ms 6748 KB Output is correct
4 Correct 2 ms 6748 KB Output is correct
5 Correct 3 ms 6800 KB Output is correct
6 Correct 2 ms 6864 KB Output is correct
7 Correct 2 ms 6748 KB Output is correct
8 Correct 2 ms 6748 KB Output is correct
9 Correct 2 ms 6748 KB Output is correct
10 Correct 2 ms 6748 KB Output is correct
11 Correct 36 ms 15552 KB Output is correct
12 Correct 32 ms 16576 KB Output is correct
13 Correct 46 ms 17336 KB Output is correct
14 Correct 44 ms 18108 KB Output is correct
15 Correct 45 ms 18628 KB Output is correct
16 Correct 60 ms 20768 KB Output is correct
17 Correct 57 ms 21904 KB Output is correct
18 Correct 58 ms 20672 KB Output is correct
19 Correct 50 ms 22932 KB Output is correct
20 Correct 32 ms 15592 KB Output is correct
21 Correct 32 ms 15404 KB Output is correct
22 Correct 74 ms 23216 KB Output is correct
23 Correct 72 ms 21876 KB Output is correct
24 Correct 68 ms 21788 KB Output is correct
25 Correct 68 ms 25532 KB Output is correct
26 Correct 62 ms 22716 KB Output is correct
27 Correct 69 ms 21952 KB Output is correct
28 Correct 31 ms 13788 KB Output is correct
29 Correct 42 ms 16580 KB Output is correct
30 Correct 43 ms 16576 KB Output is correct
31 Correct 48 ms 16828 KB Output is correct
32 Correct 51 ms 19592 KB Output is correct