답안 #948635

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
948635 2024-03-18T09:48:23 Z GrindMachine One-Way Streets (CEOI17_oneway) C++17
100 / 100
169 ms 34900 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x,y) ((x+y-1)/(y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl

#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)

template<typename T>
void amin(T &a, T b) {
    a = min(a,b);
}

template<typename T>
void amax(T &a, T b) {
    a = max(a,b);
}

#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif

/*



*/

const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;

vector<array<int,3>> adj1[N];
vector<bool> vis(N);
vector<bool> bridge(N);
vector<int> tin(N), low(N);
int timer = 1;

void dfs1(int u, int pid){
    vis[u] = 1;
    tin[u] = low[u] = timer++;

    for(auto [v,id,dir] : adj1[u]){
        if(id == pid) conts;
        if(vis[v]){
            amin(low[u],tin[v]);
        }
        else{
            dfs1(v,id);
            amin(low[u],low[v]);
            if(low[v] > tin[u]){
                bridge[id] = 1;
            }
        }
    }
}

vector<int> col(N);

void dfs2(int u, int c){
    vis[u] = 1;
    col[u] = c;

    for(auto [v,id,dir] : adj1[u]){
        if(vis[v]) conts;
        if(bridge[id]) conts;
        dfs2(v,c);
    }
}

vector<pii> adj2[N];

struct lca_algo {
    // LCA template (for graphs with 1-based indexing)
 
    int LOG = 1;
    vector<int> depth;
    vector<vector<int>> up;
    vector<int> tin, tout;
    vector<int> pid;
    vector<bool> lca_vis;
    int timer = 1;
 
    lca_algo() {
 
    }
 
    lca_algo(int n) {
        lca_init(n);
    }
 
    void lca_init(int n) {
        while ((1 << LOG) < n) LOG++;
        up = vector<vector<int>>(n + 1, vector<int>(LOG, 1));
        depth = vector<int>(n + 1);
        tin = vector<int>(n + 1);
        tout = vector<int>(n + 1);
        pid = vector<int>(n + 1, -1);
        lca_vis = vector<bool>(n + 1);

        rep1(i,n){
            if(!lca_vis[i]){
                lca_dfs(i,-1);
            }
        }
    }
 
    void lca_dfs(int node, int par) {
        lca_vis[node] = 1;
        tin[node] = timer++;
 
        for(auto [child, id] : adj2[node]) {
            if (child == par) conts;
 
            up[child][0] = node;
            rep1(j, LOG - 1) {
                up[child][j] = up[up[child][j - 1]][j - 1];
            }
 
            depth[child] = depth[node] + 1;
            pid[child] = id;

            lca_dfs(child, node);
        }
 
        tout[node] = timer-1;
    }
 
    int lift(int u, int k) {
        rep(j, LOG) {
            if (k & (1 << j)) {
                u = up[u][j];
            }
        }
 
        return u;
    }
 
    int query(int u, int v) {
        if (depth[u] < depth[v]) swap(u, v);
        int k = depth[u] - depth[v];
        u = lift(u, k);
 
        if (u == v) return u;
 
        rev(j, LOG - 1, 0) {
            if (up[u][j] != up[v][j]) {
                u = up[u][j];
                v = up[v][j];
            }
        }
 
        u = up[u][0];
        return u;
    }
 
    int get_dis(int u, int v) {
        int lca = query(u, v);
        return depth[u] + depth[v] - 2 * depth[lca];
    }
 
    bool is_ances(int u, int v){
        return tin[u] <= tin[v] and tout[u] >= tout[v];
    }
};

vector<int> dir1(N), dir2(N);

void dfs3(int u){
    vis[u] = 1;
    for(auto [v,id] : adj2[u]){
        if(vis[v]) conts;
        dfs3(v);
        dir1[u] += dir1[v];
        dir2[u] += dir2[v];
    }
}

void solve(int test_case)
{
    int n,m; cin >> n >> m;
    vector<pii> edges(m+5);
    rep1(i,m){
        int u,v; cin >> u >> v;
        edges[i] = {u,v};
        adj1[u].pb({v,i,0}), adj1[v].pb({u,i,1});
    }
    int k; cin >> k;
    vector<pii> a(k+5);
    rep1(i,k) cin >> a[i].ff >> a[i].ss;

    rep1(u,n){
        if(vis[u]) conts;
        dfs1(u,-1);
    }

    fill(all(vis),0);
    
    int ptr = 0;
    rep1(u,n){
        if(vis[u]) conts;
        dfs2(u,++ptr);
    }

    fill(all(vis),0);

    rep1(u,n){
        for(auto [v,id,dir] : adj1[u]){
            if(dir) conts;
            if(col[u] != col[v]){
                int cu = col[u], cv = col[v];
                adj2[cu].pb({cv,id}), adj2[cv].pb({cu,id});
                edges[id] = {cu,cv};
            }
        }
    }

    lca_algo LCA(ptr);
    vector<char> ans(m+5);
    rep1(i,m) ans[i] = 'B';

    rep1(i,k){
        auto [u,v] = a[i];
        u = col[u], v = col[v];
        int lca = LCA.query(u,v);
        dir1[u]++, dir1[lca]--;
        dir2[v]++, dir2[lca]--;

        /*

        while(u != lca){
            int p = LCA.up[u][0];
            int id = LCA.pid[u];
            if(u == edges[id].ff){
                ans[id] = 'R';
            }
            else{
                ans[id] = 'L';
            }

            u = p;
        }

        while(v != lca){
            int p = LCA.up[v][0];
            int id = LCA.pid[v];
            if(v == edges[id].ff){
                ans[id] = 'L';
            }
            else{
                ans[id] = 'R';
            }

            v = p;
        }

        */
    }

    rep1(c,ptr){
        if(vis[c]) conts;
        dfs3(c);
    }

    rep1(c,ptr){
        int id = LCA.pid[c];
        if(id == -1) conts;
        assert(!(dir1[c] and dir2[c]));
        auto [u,v] = edges[id];

        if(dir1[c]){
            if(LCA.depth[u] > LCA.depth[v]){
                ans[id] = 'R';
            }
            else{
                ans[id] = 'L';
            }
        }

        if(dir2[c]){
            if(LCA.depth[u] > LCA.depth[v]){
                ans[id] = 'L';
            }
            else{
                ans[id] = 'R';
            }
        }
    }

    rep1(i,m) cout << ans[i];
    cout << endl;
}

int main()
{
    fastio;

    int t = 1;
    // cin >> t;

    rep1(i, t) {
        solve(i);
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 7000 KB Output is correct
2 Correct 2 ms 7004 KB Output is correct
3 Correct 2 ms 7004 KB Output is correct
4 Correct 2 ms 7260 KB Output is correct
5 Correct 3 ms 7260 KB Output is correct
6 Correct 2 ms 7000 KB Output is correct
7 Correct 3 ms 7260 KB Output is correct
8 Correct 2 ms 7260 KB Output is correct
9 Correct 3 ms 7004 KB Output is correct
10 Correct 4 ms 7004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 7000 KB Output is correct
2 Correct 2 ms 7004 KB Output is correct
3 Correct 2 ms 7004 KB Output is correct
4 Correct 2 ms 7260 KB Output is correct
5 Correct 3 ms 7260 KB Output is correct
6 Correct 2 ms 7000 KB Output is correct
7 Correct 3 ms 7260 KB Output is correct
8 Correct 2 ms 7260 KB Output is correct
9 Correct 3 ms 7004 KB Output is correct
10 Correct 4 ms 7004 KB Output is correct
11 Correct 29 ms 13148 KB Output is correct
12 Correct 32 ms 13908 KB Output is correct
13 Correct 35 ms 14720 KB Output is correct
14 Correct 46 ms 18256 KB Output is correct
15 Correct 53 ms 19920 KB Output is correct
16 Correct 86 ms 27760 KB Output is correct
17 Correct 99 ms 29012 KB Output is correct
18 Correct 92 ms 27756 KB Output is correct
19 Correct 113 ms 30032 KB Output is correct
20 Correct 40 ms 12892 KB Output is correct
21 Correct 30 ms 12636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 7000 KB Output is correct
2 Correct 2 ms 7004 KB Output is correct
3 Correct 2 ms 7004 KB Output is correct
4 Correct 2 ms 7260 KB Output is correct
5 Correct 3 ms 7260 KB Output is correct
6 Correct 2 ms 7000 KB Output is correct
7 Correct 3 ms 7260 KB Output is correct
8 Correct 2 ms 7260 KB Output is correct
9 Correct 3 ms 7004 KB Output is correct
10 Correct 4 ms 7004 KB Output is correct
11 Correct 29 ms 13148 KB Output is correct
12 Correct 32 ms 13908 KB Output is correct
13 Correct 35 ms 14720 KB Output is correct
14 Correct 46 ms 18256 KB Output is correct
15 Correct 53 ms 19920 KB Output is correct
16 Correct 86 ms 27760 KB Output is correct
17 Correct 99 ms 29012 KB Output is correct
18 Correct 92 ms 27756 KB Output is correct
19 Correct 113 ms 30032 KB Output is correct
20 Correct 40 ms 12892 KB Output is correct
21 Correct 30 ms 12636 KB Output is correct
22 Correct 169 ms 29776 KB Output is correct
23 Correct 159 ms 30616 KB Output is correct
24 Correct 126 ms 30800 KB Output is correct
25 Correct 158 ms 34900 KB Output is correct
26 Correct 155 ms 31636 KB Output is correct
27 Correct 150 ms 30592 KB Output is correct
28 Correct 26 ms 13000 KB Output is correct
29 Correct 45 ms 15504 KB Output is correct
30 Correct 48 ms 15708 KB Output is correct
31 Correct 45 ms 15956 KB Output is correct
32 Correct 76 ms 22100 KB Output is correct