답안 #886208

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
886208 2023-12-11T15:06:31 Z Zero_OP One-Way Streets (CEOI17_oneway) C++17
0 / 100
3000 ms 11868 KB
#include<bits/stdc++.h>

using namespace std;

#define range(v) begin(v), end(v)
#define compact(v) v.erase(unique(range(v)), end(v))

template<class T> bool minimize(T& a, T b){
    if(a > b) return a = b, true;
    return false;
}

template<class T> bool maximize(T& a, T b){
    if(a < b) return a = b, true;
    return false;
}

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const int N = 1e5 + 5;
const int Left = 0;
const int Right = 1;
const int Both = 2;
const char c[] = {'L', 'R', 'B'};

int n, m, low[N], num[N], id[N], timerDFS, timerSCC;
pair<int, int> e[N], E[N];
vector<pair<int, int>> g[N], G[N];
stack<int> stck;

void dfsTarjan(int u, int preID){
    low[u] = num[u] = ++timerDFS;
    stck.push(u);
    for(auto [id, dir] : g[u]) if(id != preID){
        int v = e[id].first ^ e[id].second ^ u;
        if(num[v]) {
            minimize(low[u], num[v]);
        }
        else{
            dfsTarjan(v, id);
            minimize(low[u], low[v]);
        }
    }
    if(low[u] == num[u]){
        ++timerSCC;
        do{
            int v = stck.top(); stck.pop();
            id[v] = timerSCC;
            low[u] = num[u] = N;
        } while(!id[u]);
    }
}

int ans[N], node[N], st[N << 2], lzy[N << 2], h[N], head[N], par[N], pos[N], sz[N], heavy[N], parID[N], ptr[N], timerHLD;

void dfsSize(int u, int preID){
    sz[u] = 1;
    heavy[u] = 0;
    for(auto [id, dir] : G[u]) if(id != preID){
        int v = u ^ E[id].first ^ E[id].second;
        h[v] = h[u] + 1;
        par[v] = u;
        parID[v] = id;
        dfsSize(v, id);
        sz[u] += sz[v];
        if(sz[heavy[u]] < sz[v]){
            heavy[u] = v;
        }
    }
}

void decompose(int u, int curHead){
    head[u] = curHead;
    if(heavy[u]){
        node[pos[heavy[u]] = ++timerHLD] = heavy[u];
        decompose(heavy[u], curHead);
        for(auto [id, dir] : G[u]){
            int v = u ^ E[id].first ^ E[id].second;
            if(v != par[u] and v != heavy[u]){
                node[pos[v] = ++timerHLD] = v;
                decompose(v, v);
            }
        }
    }
}

void apply(int id, int val){
    st[id] = val;
    lzy[id] = val;
}

void pushDown(int id){
    if(lzy[id] != -1){
        apply(id << 1, lzy[id]);
        apply(id << 1 | 1, lzy[id]);
        lzy[id] = -1;
    }
}

void update(int id, int l, int r, int u, int v, int target){
    if(v < l or u > r or l > r) return;
    if(u <= l and r <= v){
        apply(id, target);
    }
    else{
        int mid = l + r >> 1;
        pushDown(id);
        update(id << 1, l, mid, u, v, target);
        update(id << 1 | 1, mid + 1, r, u, v, target);
    }
}

void get(int id, int l, int r){ //compute after preprocessed
    if(l < r){
        int mid = l + r >> 1;
        pushDown(id);
        get(id << 1, l, mid);
        get(id << 1 | 1, mid + 1, r);
    }
    else ptr[node[l]] = st[id];
}

int getLCA(int u, int v){
    while(head[u] != head[v]){
        if(h[head[u]] < h[head[v]]) swap(u, v);
        u = par[head[u]];
    }
    return h[u] < h[v] ? u : v;
}

void updatePath(int u, int v, int target){
    while(head[u] != head[v]){
        if(h[head[u]] < h[head[v]]) swap(u, v);
        update(1, 1, timerSCC - 1, pos[head[u]], pos[u], target);
    }
    if(h[u] > h[v]) swap(u, v);
    update(1, 1, timerSCC - 1, pos[u] + 1, pos[v], target);
}

void Zero_OP(){
    cin >> n >> m;
    for(int i = 1; i <= m; ++i){
        int u, v;
        cin >> u >> v;
        g[u].push_back({i, 0});
        g[v].push_back({i, 1});
        e[i] = {u, v};
    }

    dfsTarjan(1, -1);

    for(int i = 1; i <= m; ++i){
        int u, v;
        tie(u, v) = e[i];
        E[i] = {id[u], id[v]};
        if(id[u] != id[v]){
            G[id[u]].push_back({i, 0});
            G[id[v]].push_back({i, 1});
        }
        else ans[i] = Both;
    }

    dfsSize(1, -1);
    decompose(1, -1);

    memset(lzy, -1, sizeof(lzy));
    memset(st, -1, sizeof(st));

    int q; cin >> q;
    for(int i = 1; i <= q; ++i){
        int u, v;
        cin >> u >> v;
        u = id[u], v = id[v];
        int mid = getLCA(u, v);

        updatePath(u, mid, Left);
        updatePath(mid, v, Right);
    }

    get(1, 1, timerSCC - 1);
    for(int i = 2; i <= timerSCC; ++i){
        int g = parID[i];
        int target = ptr[i];
        if(target == -1) ans[g] = Both;
        else{
            ans[g] = target;
            if(E[g].first != par[E[g].second]) ans[g] ^= 1;
        }
    }

    for(int i = 1; i <= m; ++i){
        cout << c[ans[i]];
    }
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    #define task "antuvu"
    if(fopen(task".inp", "r")){
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }

    Zero_OP();

    return 0;
}

Compilation message

oneway.cpp: In function 'void update(int, int, int, int, int, int)':
oneway.cpp:108:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  108 |         int mid = l + r >> 1;
      |                   ~~^~~
oneway.cpp: In function 'void get(int, int, int)':
oneway.cpp:117:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  117 |         int mid = l + r >> 1;
      |                   ~~^~~
oneway.cpp: In function 'int main()':
oneway.cpp:204:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  204 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
oneway.cpp:205:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  205 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3043 ms 11868 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3043 ms 11868 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3043 ms 11868 KB Time limit exceeded
2 Halted 0 ms 0 KB -