답안 #811916

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
811916 2023-08-07T06:26:34 Z Cookie One-Way Streets (CEOI17_oneway) C++14
100 / 100
131 ms 33904 KB
#include<bits/stdc++.h>
#include<fstream>
using namespace std;
//ifstream fin("FEEDING.INP");
//ofstream fout("FEEDING.OUT");
#define sz(a) (int)a.size()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
const int base = 74;
const ll mod = 1e9 + 7, inf = 1e9, mxv = 1005, mxn = 2e5 + 5;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, m, p;
int num[mxn + 1], low[mxn + 1], tt = 0, comp = 0, idcomp[mxn + 1], dep[mxn + 1];
int toedge[mxn + 1], pup[mxn + 1], pdown[mxn + 1], up[mxn + 1][17], ans[mxn + 1];
bool vis[mxn + 1], bridge[mxn + 1];
vt<pii>adj[mxn + 1], g[mxn + 1];
void dfs(int s, int pre){
    low[s] = num[s] = ++tt;
    for(int i = 0; i < sz(adj[s]); i++){
        int v = adj[s][i].first, id = adj[s][i].second;
        if(id % m == pre % m)continue;
        if(!num[v]){
            dfs(v, id);
            low[s] = min(low[s], low[v]);
            if(low[v] > num[s]){
                bridge[id] = 1;
                if(id < m)bridge[id + m] = 1;
                else bridge[id - m] = 1;
            }
        }else low[s] = min(low[s], num[v]);
    }
}
void dfs2(int s){
    idcomp[s] = comp; vis[s] = 1;
    for(int i = 0; i < sz(adj[s]); i++){
        int v = adj[s][i].first, id = adj[s][i].second;
        if(!bridge[id] && !vis[v]){
            dfs2(v);
        }
    }
}
void dfs3(int s, int pre){
   
    vis[s] = 1;
    for(int i = 0; i < sz(g[s]); i++){
        int v = g[s][i].first, id = g[s][i].second;
        if(v == pre)continue;
        dep[v] = dep[s] + 1;
        up[v][0] = s; toedge[v] = id;
        dfs3(v, s);
    }
}
int lca(int u, int v){
    if(dep[u] < dep[v])swap(u, v);
    int k = dep[u] - dep[v];
    for(int i = 0; i < 17; i++){
        if(k & (1 << i)){
            u = up[u][i];
        }
    }
    if(u == v)return(u);
    for(int i = 16; i >= 0; i--){
        if(up[u][i] != up[v][i]){
            u = up[u][i]; v = up[v][i];
        }
    }
    return(up[u][0]);
}
void dfs4(int s, int pre){
    for(int i = 0; i < sz(g[s]); i++){
        int v = g[s][i].first, id = g[s][i].second;
        if(v == pre)continue;
        assert(dep[v] == dep[s] + 1);
        dfs4(v, s);
        pup[s] += pup[v]; pdown[s] += pdown[v];
    }
}
signed main()
{
     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n >> m;
    for(int i = 0; i < m; i++){
        int u, v; cin >> u >> v;
        adj[u].pb(make_pair(v, i));  adj[v].pb(make_pair(u, i + m));
    }
    for(int i = 1; i <= n; i++){
        if(!num[i]){
            dfs(i, -1);
        }
    }
    
    for(int i = 1; i <= n; i++){
        if(!vis[i]){
            comp++;
            dfs2(i);
        }
    }
   
    for(int i = 1; i <= n; i++){
        for(int j = 0; j < sz(adj[i]); j++){
            int v = adj[i][j].first, id = adj[i][j].second;
            if(idcomp[i] != idcomp[v]){
              
                g[idcomp[i]].pb(make_pair(idcomp[v], id));
            }
        }
    }
    for(int i = 1; i <= comp; i++){
        vis[i] = 0; toedge[i] = -1;
        sort(g[i].begin(), g[i].end());
        g[i].resize(unique(g[i].begin(), g[i].end()) - g[i].begin());
    }
   
    vt<int>root;
    for(int i = 1; i <= comp; i++){
        if(!vis[i]){
            root.pb(i);
            dfs3(i, -1);
        }
    }
    
    for(int i = 1; i < 17; i++){
        for(int j = 1; j <= comp; j++){
            up[j][i] = up[up[j][i - 1]][i - 1];
        }
    }
    cin >> p;
    for(int i = 0; i < p; i++){
        int u, v; cin >> u >> v;
        u = idcomp[u]; v = idcomp[v];
        if(u == v)continue;
        int l = lca(u, v);
        //cout << u << " " << v << " " << l << "\n";
        pup[u]++; pdown[v]++; pup[l]--; pdown[l]--;
    }
    for(int i = 0; i < sz(root); i++){
        assert(dep[root[i]] == 0);
        dfs4(root[i], -1);
    }
    
    for(int i = 1; i <= comp; i++){
        //cout << pup[i] << " ";
        
        if(pup[i]){
            assert(!pdown[i]);
            ans[toedge[i] % m] = 1 + ((toedge[i] / m) ^ 1);
        }else if(pdown[i]){
            ans[toedge[i] % m] = 1 + (toedge[i] / m);
        }
    }
    for(int i = 0; i < m; i++){
        if(ans[i] == 0)cout << 'B';
        else if(ans[i] == 1)cout << 'R';
        else cout << 'L';

    }
    
    return(0);
}

Compilation message

oneway.cpp: In function 'void dfs4(int, int)':
oneway.cpp:81:32: warning: unused variable 'id' [-Wunused-variable]
   81 |         int v = g[s][i].first, id = g[s][i].second;
      |                                ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9732 KB Output is correct
3 Correct 6 ms 9812 KB Output is correct
4 Correct 5 ms 9860 KB Output is correct
5 Correct 7 ms 10032 KB Output is correct
6 Correct 6 ms 9812 KB Output is correct
7 Correct 6 ms 9996 KB Output is correct
8 Correct 5 ms 9876 KB Output is correct
9 Correct 5 ms 9812 KB Output is correct
10 Correct 5 ms 9812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9732 KB Output is correct
3 Correct 6 ms 9812 KB Output is correct
4 Correct 5 ms 9860 KB Output is correct
5 Correct 7 ms 10032 KB Output is correct
6 Correct 6 ms 9812 KB Output is correct
7 Correct 6 ms 9996 KB Output is correct
8 Correct 5 ms 9876 KB Output is correct
9 Correct 5 ms 9812 KB Output is correct
10 Correct 5 ms 9812 KB Output is correct
11 Correct 32 ms 15180 KB Output is correct
12 Correct 45 ms 16208 KB Output is correct
13 Correct 40 ms 17800 KB Output is correct
14 Correct 48 ms 21012 KB Output is correct
15 Correct 51 ms 22476 KB Output is correct
16 Correct 70 ms 28148 KB Output is correct
17 Correct 105 ms 29900 KB Output is correct
18 Correct 80 ms 28484 KB Output is correct
19 Correct 85 ms 31012 KB Output is correct
20 Correct 37 ms 15656 KB Output is correct
21 Correct 36 ms 16028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9732 KB Output is correct
3 Correct 6 ms 9812 KB Output is correct
4 Correct 5 ms 9860 KB Output is correct
5 Correct 7 ms 10032 KB Output is correct
6 Correct 6 ms 9812 KB Output is correct
7 Correct 6 ms 9996 KB Output is correct
8 Correct 5 ms 9876 KB Output is correct
9 Correct 5 ms 9812 KB Output is correct
10 Correct 5 ms 9812 KB Output is correct
11 Correct 32 ms 15180 KB Output is correct
12 Correct 45 ms 16208 KB Output is correct
13 Correct 40 ms 17800 KB Output is correct
14 Correct 48 ms 21012 KB Output is correct
15 Correct 51 ms 22476 KB Output is correct
16 Correct 70 ms 28148 KB Output is correct
17 Correct 105 ms 29900 KB Output is correct
18 Correct 80 ms 28484 KB Output is correct
19 Correct 85 ms 31012 KB Output is correct
20 Correct 37 ms 15656 KB Output is correct
21 Correct 36 ms 16028 KB Output is correct
22 Correct 131 ms 30976 KB Output is correct
23 Correct 125 ms 29540 KB Output is correct
24 Correct 130 ms 29624 KB Output is correct
25 Correct 114 ms 33904 KB Output is correct
26 Correct 126 ms 30704 KB Output is correct
27 Correct 105 ms 29616 KB Output is correct
28 Correct 27 ms 13060 KB Output is correct
29 Correct 49 ms 16460 KB Output is correct
30 Correct 51 ms 16904 KB Output is correct
31 Correct 52 ms 16800 KB Output is correct
32 Correct 74 ms 22636 KB Output is correct