Submission #948631

# Submission time Handle Problem Language Result Execution time Memory
948631 2024-03-18T09:37:49 Z GrindMachine One-Way Streets (CEOI17_oneway) C++17
60 / 100
3000 ms 31220 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);
        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];
    }
};

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);
    }

    rep1(u,n){
        assert(vis[u]);
    }

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

    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);

        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(i,m) cout << ans[i];
    cout << endl;
}

int main()
{
    fastio;

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

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

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 6236 KB Output is correct
2 Correct 2 ms 6124 KB Output is correct
3 Correct 2 ms 6232 KB Output is correct
4 Correct 3 ms 6372 KB Output is correct
5 Correct 2 ms 6492 KB Output is correct
6 Correct 2 ms 6236 KB Output is correct
7 Correct 2 ms 6492 KB Output is correct
8 Correct 2 ms 6492 KB Output is correct
9 Correct 2 ms 6232 KB Output is correct
10 Correct 2 ms 6236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 6236 KB Output is correct
2 Correct 2 ms 6124 KB Output is correct
3 Correct 2 ms 6232 KB Output is correct
4 Correct 3 ms 6372 KB Output is correct
5 Correct 2 ms 6492 KB Output is correct
6 Correct 2 ms 6236 KB Output is correct
7 Correct 2 ms 6492 KB Output is correct
8 Correct 2 ms 6492 KB Output is correct
9 Correct 2 ms 6232 KB Output is correct
10 Correct 2 ms 6236 KB Output is correct
11 Correct 30 ms 13404 KB Output is correct
12 Correct 33 ms 14160 KB Output is correct
13 Correct 38 ms 15188 KB Output is correct
14 Correct 57 ms 18604 KB Output is correct
15 Correct 54 ms 20148 KB Output is correct
16 Correct 85 ms 27988 KB Output is correct
17 Correct 174 ms 29540 KB Output is correct
18 Correct 81 ms 28208 KB Output is correct
19 Correct 160 ms 30516 KB Output is correct
20 Correct 33 ms 13248 KB Output is correct
21 Correct 31 ms 13148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 6236 KB Output is correct
2 Correct 2 ms 6124 KB Output is correct
3 Correct 2 ms 6232 KB Output is correct
4 Correct 3 ms 6372 KB Output is correct
5 Correct 2 ms 6492 KB Output is correct
6 Correct 2 ms 6236 KB Output is correct
7 Correct 2 ms 6492 KB Output is correct
8 Correct 2 ms 6492 KB Output is correct
9 Correct 2 ms 6232 KB Output is correct
10 Correct 2 ms 6236 KB Output is correct
11 Correct 30 ms 13404 KB Output is correct
12 Correct 33 ms 14160 KB Output is correct
13 Correct 38 ms 15188 KB Output is correct
14 Correct 57 ms 18604 KB Output is correct
15 Correct 54 ms 20148 KB Output is correct
16 Correct 85 ms 27988 KB Output is correct
17 Correct 174 ms 29540 KB Output is correct
18 Correct 81 ms 28208 KB Output is correct
19 Correct 160 ms 30516 KB Output is correct
20 Correct 33 ms 13248 KB Output is correct
21 Correct 31 ms 13148 KB Output is correct
22 Execution timed out 3042 ms 31220 KB Time limit exceeded
23 Halted 0 ms 0 KB -