답안 #631820

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
631820 2022-08-18T19:39:52 Z NintsiChkhaidze One-Way Streets (CEOI17_oneway) C++14
0 / 100
5 ms 7764 KB
#include <bits/stdc++.h>
#define s second
#define f first
#define pb push_back
#define left (h<<1),l,((l+r)>>1)
#define right ((h<<1)|1),((l+r)>>1) + 1,r
using namespace std;
 
const int N = 100005;
int tin[N],low[N],a[N],b[N],ID[N],in[N],out[N],dp[N],d[25][N],n1[N],n2[N];
int t[5][4*N];
bool O[N],C[N],bridge[N],vis[N],op[N],cl[N],F[N];
vector <int> adj[N],vec[N];
vector <pair<int,int> > v[N];
int k,idx,cnt,n;
 
void upd(int h,int l,int r,int idx,int val){
    if (l == r){
        if (val == 0) t[1][h] = t[2][h] = val;
        else{
            t[1][h] = min(t[1][h],val);
            t[2][h] = max(t[2][h],val);
        }
        return;
    }
    if (idx > ((l + r)>>1)) upd(right,idx,val);
    else upd(left,idx,val);
    
    t[1][h] = min(t[1][h*2],t[1][h*2+1]);
    t[2][h] = max(t[2][h*2],t[2][h*2+1]);
}
pair <int,int> get(int h,int l,int r,int L,int R){
    if (r < L || R < l) return {0,0};
    if (L <= l && r <= R) return {t[1][h],t[2][h]};
    pair <int,int> a = get(left,L,R),b = get(right,L,R);
    return {min(a.f,b.f),max(a.s,b.s)};
}

void dfs(int x,int par){
    vis[x] = 1;
    tin[x] = low[x] = cnt++;
    for (auto [to,id]: v[x]){
        if (to == par) continue;
        if (vis[to]){
            low[x] = min(low[x], tin[to]);
        }else{
            dfs(to,x);
            low[x] = min(low[x], low[to]);
            if (low[to] > tin[x]) bridge[id] = 1;
        }
    }
}
void dfs1(int x){
    ID[x] = idx;
    vis[x] = 1;
    for (auto [to,id]: v[x]){
        if (!vis[to] && !bridge[id]) dfs1(to);
        else if (!vis[to] && bridge[id]) vec[idx].pb(to);
    }
}
void dfs2(int x,int par){
    vis[x] = 1;
    in[x] = ++cnt;
    d[0][x] = par;
    dp[x] = dp[par] + 1;
    for (int to: adj[x])
        if (!vis[to]) dfs2(to,x);
    out[x] = cnt;
}

void dfs3(int x,int par){
    vis[x] = 1;
    for (int to: adj[x]) if (to != par) dfs3(to,x);
    
    // if (x == 860 || x == 360 || x == 310 || x == 687) cout<<"x = "<<x<<" "<<in[x]<<" "<<out[x]<<endl;
    if (op[x]) {
        upd(1,1,n,in[x],-1);
        // if (x == 860 || x == 360 || x == 310 || x == 687) cout<<"o: "<<x<<" "<<in[x]<<endl;
    }
    if (cl[x]) {
        upd(1,1,n,in[x],1e9);
        // if (x == 860 || x == 360 || x == 310 || x == 687) cout<<"c: "<<x<<endl;
    }
    for (int id: vec[x]) {
        upd(1,1,n,in[n1[id]],0);
        upd(1,1,n,in[n2[id]],0);
        // if (x == 860 || x == 360 || x == 310 || x == 687) cout<<"V: "<<id<<endl;
    }
    pair <int,int> res = get(1,1,n,in[x],out[x]);
    O[x] = (res.f == -1);
    C[x] = (res.s == 1e9);
}
int lca(int x,int y){
    if (dp[x] < dp[y]) swap(x,y);
    for (int j = 18; j >= 0; j--)
        if (dp[d[j][x]] >= dp[y]) x = d[j][x];
    if (x == y) return x;
    for (int j = 18; j >=0; j--)
        if(d[j][x] != d[j][y]) x = d[j][x],y = d[j][y];
    return d[0][x];
}

void DFS(int x){
    vis[x] = 1;
    for (int to: adj[x])
        if (!vis[to] && dp[to] > dp[x]) DFS(to);
}
int main (){
    ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
    int m;
    cin>>n>>m;
    for (int i = 1; i <= m; i++){
        cin>>a[i]>>b[i];
        v[a[i]].pb({b[i],i});
        v[b[i]].pb({a[i],i});
    }
 
    for (int i= 1; i <= n; i++)
        if (!vis[i]) dfs(i,i); 
 
    for (int i = 1; i <= n; i++) {
        sort(v[i].begin(),v[i].end());
        for (int j = 1; j < v[i].size(); j++)
            if (v[i][j].f == v[i][j - 1].f) bridge[v[i][j].s] = bridge[v[i][j - 1].s] = 0;
    }
    
    for (int i= 1; i <= n; i++) vis[i] = 0;
    idx = 0;
    for (int i = 1; i <= n; i++)
        if (!vis[i]) idx++,dfs1(i);
    
    for (int i = 1; i <= n; i++)
        for (int x: vec[i])
            adj[i].pb(ID[x]),adj[ID[x]].pb(i);
            
    for (int i = 1; i <= n; i++)
        vec[i].clear(),vis[i] = 0;
    
    cnt=0;
    for (int i = 1; i <= n; i++) 
        if (!vis[i]) dfs2(i,i);
        
    for (int j = 1; j < 19; j++)
        for (int i = 1; i <= n; i++)
            d[j][i] = d[j - 1][d[j - 1][i]];
            
    cin>>k;
    for (int i = 1; i <= k; i++){
        cin>>n1[i]>>n2[i];
        n1[i] = ID[n1[i]],n2[i] = ID[n2[i]];
        
        int c = lca(n1[i],n2[i]);
        vec[c].pb(i);
        op[n1[i]] = cl[n2[i]] = 1;
    }
    
    for (int i = 1; i <= n; i++) vis[i] = 0;
    for (int i = 1; i <= n; i++) if (!vis[i]) dfs3(i,i);
    
    for (int i = 1; i <= m; i++){
        if (!bridge[i]) cout<<'B';
        else{
            int x = ID[a[i]],y = ID[b[i]];
            if (dp[x] >= dp[y]){
                if(O[x] && !C[x]) cout<<'R';
                else if (!O[x] && C[x]) cout<<'L';
                else cout<<'B';
            }else{
                if(O[y] && !C[y]) cout<<'L';
                else if (!O[y] && C[y]) cout<<'R';
                else cout<<'B';
            }
        }
    }

}

Compilation message

oneway.cpp: In function 'void dfs(int, int)':
oneway.cpp:42:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   42 |     for (auto [to,id]: v[x]){
      |               ^
oneway.cpp: In function 'void dfs1(int)':
oneway.cpp:56:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   56 |     for (auto [to,id]: v[x]){
      |               ^
oneway.cpp: In function 'int main()':
oneway.cpp:123:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  123 |         for (int j = 1; j < v[i].size(); j++)
      |                         ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7508 KB Output is correct
2 Correct 4 ms 7440 KB Output is correct
3 Correct 4 ms 7636 KB Output is correct
4 Correct 4 ms 7764 KB Output is correct
5 Correct 5 ms 7764 KB Output is correct
6 Correct 4 ms 7628 KB Output is correct
7 Correct 5 ms 7764 KB Output is correct
8 Correct 4 ms 7764 KB Output is correct
9 Incorrect 5 ms 7636 KB Output isn't correct
10 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7508 KB Output is correct
2 Correct 4 ms 7440 KB Output is correct
3 Correct 4 ms 7636 KB Output is correct
4 Correct 4 ms 7764 KB Output is correct
5 Correct 5 ms 7764 KB Output is correct
6 Correct 4 ms 7628 KB Output is correct
7 Correct 5 ms 7764 KB Output is correct
8 Correct 4 ms 7764 KB Output is correct
9 Incorrect 5 ms 7636 KB Output isn't correct
10 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7508 KB Output is correct
2 Correct 4 ms 7440 KB Output is correct
3 Correct 4 ms 7636 KB Output is correct
4 Correct 4 ms 7764 KB Output is correct
5 Correct 5 ms 7764 KB Output is correct
6 Correct 4 ms 7628 KB Output is correct
7 Correct 5 ms 7764 KB Output is correct
8 Correct 4 ms 7764 KB Output is correct
9 Incorrect 5 ms 7636 KB Output isn't correct
10 Halted 0 ms 0 KB -