Submission #751475

# Submission time Handle Problem Language Result Execution time Memory
751475 2023-05-31T15:51:20 Z urosk One-Way Streets (CEOI17_oneway) C++14
100 / 100
840 ms 93608 KB
// __builtin_popcount(x)
// __builtin_popcountll(x)
#define here cerr<<"===========================================\n"
#include <bits/stdc++.h>
#define ld double
#define ll long long
#define ull unsigned long long
#define llinf 100000000000000000LL // 10^17
#define iinf 2000000000 // 2*10^9
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define sz(a) int(a.size())
#define all(a) a.begin(),a.end()
#define ceri(a,l,r) {for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;}
using namespace std;

#define maxn 100005
ll n,m;
map<pll,ll> cnt;
map<pll,bool> ans;
ll con[maxn];
vector<ll> g[maxn];
ll in[maxn];
vector<pll> edge;
ll it = 1;
ll low[maxn];
bool vis[maxn];
stack<ll> stk;
map<pll,bool> bio;
map<pll,bool> most;
ll dsu[maxn];
ll comp = 1;
ll csz = 0;
void dfs(ll u,ll par){
    low[u] = in[u] = it++;
    vis[u] = 1;
    stk.push(u);
    bool moze =1;
    if(cnt[{u,par}]>1) moze = 0;
    for(ll s : g[u]){
        if(s==par){
            if(cnt[{u,par}]==1) continue;
            continue;
        }
        if(!vis[s]) dfs(s,u);
        low[u] = min(low[u],low[s]);
    }
    if(u==par) return;
    if(moze&&low[u]>in[par]){
        ++csz;
        while(stk.size()){
            ll to = stk.top();
            stk.pop();
            dsu[to] = csz;
            if(to==u) break;
        }
    }
}
ll root(ll x){
    while(x!=con[x]){
        con[x] = con[con[x]];
        x = con[x];
    }
    return x;
}
void upd(ll x,ll y){
    x = root(x);
    y = root(y);
    con[x] = con[y];
}
ll q;
#define lg 25
ll in2[maxn];
ll out2[maxn];
vector<ll> v[maxn];
ll st[maxn][lg];
void dfs2(ll u,ll par){
    vis[u] =1;
    st[u][0] = par;
    in2[u] = it++;
    for(ll s : v[u]){
        if(s==par) continue;
        dfs2(s,u);
    }
    out2[u] = it-1;
}
bool intree(ll x,ll y){
    return in2[x]>=in2[y]&&out2[y]>=out2[x];
}
ll lca(ll x,ll y){
    if(intree(x,y)) return y;
    if(intree(y,x)) return x;
    for(ll j = lg-1;j>=0;j--){
        if(!intree(x,st[y][j])) y = st[y][j];
    }
    return st[y][0];
}
map<pll,ll> mp;
ll poc[maxn];
ll kr[maxn];
void dfs3(ll u,ll par){
    vis[u] = 1;
    for(ll s : v[u]){
        if(s==par) continue;
        dfs3(s,u);
        poc[u]+=poc[s];
        kr[u]+=kr[s];
    }
    mp[{u,par}] = poc[u]-kr[u];
    mp[{par,u}] = poc[u]-kr[u];
}
map<pll,pll> tru;
int main(){
    ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
    cin >> n >> m;
    for(ll i = 1;i<=m;i++){
        ll x,y; cin >> x >> y;
        cnt[{x,y}]++;
        cnt[{y,x}]++;
        edge.pb({x,y});
        g[x].pb(y);
        g[y].pb(x);
    }
    fill(vis,vis+n+1,0);
    for(ll i = 1;i<=n;i++) if(vis[i]==0) dfs(i,i);
    fill(vis,vis+n+1,0);
    iota(con+1,con+n+1,1);
    //here;
    //cerr<<"DSU: "<<endl;
    //ceri(dsu,1,n);
    for(pll p : edge){
        ll x = p.fi;
        ll y = p.sc;
        if(root(dsu[x])==root(dsu[y])) continue;
        tru[{x,y}] = {dsu[x],dsu[y]};
        tru[{y,x}] = {dsu[y],dsu[x]};
        v[dsu[x]].pb(dsu[y]);
        v[dsu[y]].pb(dsu[x]);
        upd(dsu[x],dsu[y]);
    }
    it = 1;
    fill(vis,vis+n+1,0);
    for(ll i = 1;i<=n;i++) if(vis[i]==0) dfs2(i,i);
    for(ll j = 1;j<lg;j++) for(ll i = 1;i<=n;i++) st[i][j] = st[st[i][j-1]][j-1];
    cin >> q;
    bool moe = 1;
    vector<pll> upit;
    for(ll i = 1;i<=q;i++){
        ll x,y; cin >> x >> y;
        upit.pb({x,y});
        x = dsu[x];
        y = dsu[y];
        if(x==y) continue;
        if(st[x][lg-1]!=st[y][lg-1]) moe = 0;
        poc[x]++;
        kr[y]++;
        ll z = lca(x,y);
        poc[z]--;
        kr[z]--;
        //cerr<<"lca: "<<x<< " "<<y<< " "<<z<<endl;
    }
    //here;
    //cerr<<"DSU: "<<endl;
    //ceri(dsu,1,n);
    fill(vis,vis+n+1,0);
    //for(ll i = 1;i<=n;i++) cerr<<poc[i]-kr[i]<<" ";
    //cerr<<endl;
    for(ll i = 1;i<=n;i++) if(vis[i]==0) dfs3(i,i);
    //for(ll i = 1;i<=n;i++) cerr<<poc[i]-kr[i]<<" ";
    //cerr<<endl;
    //here;
    for(ll ii = 0;ii<m;ii++){
        pll p = edge[ii];
        if(p.fi==p.sc){
            cout<<'B';
            continue;
        }
        if(cnt[p]>1) cout<<'B';
        else{
            if(dsu[p.fi]==dsu[p.sc]){
                cout<<'B';
                //cerr<<ii<<" "<<dsu[p.fi]<<endl;
                continue;
            }
            ll x = mp[tru[p]];
            if(x==0) cout<<'B';
            else if(x>0){
                if(in2[dsu[p.fi]]>in2[dsu[p.sc]]) cout<<'R';
                else cout<<'L';
            }else{
                if(in2[dsu[p.fi]]>in2[dsu[p.sc]]) cout<<'L';
                else cout<<'R';
            }
        }
    }
    cout<<endl;
    return 0;
}

Compilation message

oneway.cpp: In function 'int main()':
oneway.cpp:152:10: warning: variable 'moe' set but not used [-Wunused-but-set-variable]
  152 |     bool moe = 1;
      |          ^~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5076 KB Output is correct
2 Correct 3 ms 5064 KB Output is correct
3 Correct 5 ms 5460 KB Output is correct
4 Correct 5 ms 5716 KB Output is correct
5 Correct 6 ms 5880 KB Output is correct
6 Correct 4 ms 5328 KB Output is correct
7 Correct 5 ms 5844 KB Output is correct
8 Correct 5 ms 5844 KB Output is correct
9 Correct 4 ms 5332 KB Output is correct
10 Correct 4 ms 5324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5076 KB Output is correct
2 Correct 3 ms 5064 KB Output is correct
3 Correct 5 ms 5460 KB Output is correct
4 Correct 5 ms 5716 KB Output is correct
5 Correct 6 ms 5880 KB Output is correct
6 Correct 4 ms 5328 KB Output is correct
7 Correct 5 ms 5844 KB Output is correct
8 Correct 5 ms 5844 KB Output is correct
9 Correct 4 ms 5332 KB Output is correct
10 Correct 4 ms 5324 KB Output is correct
11 Correct 290 ms 30860 KB Output is correct
12 Correct 280 ms 36012 KB Output is correct
13 Correct 389 ms 44408 KB Output is correct
14 Correct 407 ms 58580 KB Output is correct
15 Correct 475 ms 64052 KB Output is correct
16 Correct 837 ms 82056 KB Output is correct
17 Correct 576 ms 84620 KB Output is correct
18 Correct 789 ms 82040 KB Output is correct
19 Correct 512 ms 87204 KB Output is correct
20 Correct 310 ms 40900 KB Output is correct
21 Correct 224 ms 39956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5076 KB Output is correct
2 Correct 3 ms 5064 KB Output is correct
3 Correct 5 ms 5460 KB Output is correct
4 Correct 5 ms 5716 KB Output is correct
5 Correct 6 ms 5880 KB Output is correct
6 Correct 4 ms 5328 KB Output is correct
7 Correct 5 ms 5844 KB Output is correct
8 Correct 5 ms 5844 KB Output is correct
9 Correct 4 ms 5332 KB Output is correct
10 Correct 4 ms 5324 KB Output is correct
11 Correct 290 ms 30860 KB Output is correct
12 Correct 280 ms 36012 KB Output is correct
13 Correct 389 ms 44408 KB Output is correct
14 Correct 407 ms 58580 KB Output is correct
15 Correct 475 ms 64052 KB Output is correct
16 Correct 837 ms 82056 KB Output is correct
17 Correct 576 ms 84620 KB Output is correct
18 Correct 789 ms 82040 KB Output is correct
19 Correct 512 ms 87204 KB Output is correct
20 Correct 310 ms 40900 KB Output is correct
21 Correct 224 ms 39956 KB Output is correct
22 Correct 595 ms 87540 KB Output is correct
23 Correct 695 ms 84520 KB Output is correct
24 Correct 840 ms 84664 KB Output is correct
25 Correct 578 ms 93608 KB Output is correct
26 Correct 722 ms 86852 KB Output is correct
27 Correct 670 ms 84652 KB Output is correct
28 Correct 98 ms 13580 KB Output is correct
29 Correct 369 ms 42772 KB Output is correct
30 Correct 280 ms 42868 KB Output is correct
31 Correct 310 ms 43792 KB Output is correct
32 Correct 322 ms 55764 KB Output is correct