답안 #1089311

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1089311 2024-09-16T09:48:09 Z TrinhKhanhDung One-Way Streets (CEOI17_oneway) C++14
0 / 100
2 ms 5720 KB
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(),v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define oo (ll)1e18
#define INF (ll)1e9
#define MOD (ll)(1e9 + 7)

using namespace std;

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

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

template<class T1, class T2>
    void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}

template<class T1, class T2>
    void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}

template<class T>
    void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}

const int MAX = 1e5 + 10, LOG = 17;

int N, M, T, K;
vector<int> adj[MAX], nadj[MAX];
int timer, low[MAX], num[MAX], gr[MAX], h[MAX], d[MAX][LOG], in[MAX], out[MAX], tmr, ans[MAX];
ll val[MAX];
pair<int, int> edge[MAX];
vector<pair<int, int>> edges, prs;
stack<int> st;
map<pair<int, int>, int> mp;

void tarjan(int u, int p = -1){
    low[u] = num[u] = ++timer;
    st.push(u);
    for(int v: adj[u]){
        if(v == p) continue;
        if(!num[v]) tarjan(v, u);
        minimize(low[u], low[v]);
    }
    if(low[u] == num[u]){
        K++;
        while(true){
            int v = st.top(); st.pop();
            num[v] = low[v] = INF;
            gr[v] = K;
            if(v == u) break;
        }
    }
}

void dfs(int u, int p = -1){
    in[u] = ++tmr;
    edge[tmr] = make_pair(p, u);
    for(int v: nadj[u]){
        if(v == p) continue;
        h[v] = h[u] + 1;
        d[v][0] = u;
        for(int i = 1; i < LOG; i++) d[v][i] = d[d[v][i - 1]][i - 1];
        dfs(v, u);
    }
    out[u] = tmr;
}

int lca(int u, int v){
    if(h[u] < h[v]) swap(u, v);
    int delta = h[u] - h[v];
    for(int i = 0; i < LOG; i++) if(BIT(delta, i)){
        u = d[u][i];
    }
    if(u == v) return u;
    for(int i = LOG - 1; i >= 0; i--) if(d[u][i] != d[v][i]){
        u = d[u][i];
        v = d[v][i];
    }
    return d[v][0];
}

void solve(){
    cin >> N >> M;
    for(int i = 1; i <= M; i++){
        int u, v; cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
        edges.push_back({u, v});
    }
    cin >> T;
    for(int i = 1; i <= T; i++){
        int u, v; cin >> u >> v;
        prs.push_back({u, v});
    }

    for(int i = 1; i <= N; i++) if(!num[i]){
        tarjan(i);
    }

    for(int i = 0; i < sz(edges); i++){
        auto o = edges[i];
        int u = gr[o.fi], v = gr[o.se];
        if(u != v){
            mp[make_pair(u, v)] = i;
            nadj[u].push_back(v);
            nadj[v].push_back(u);
        }
    }
    for(int i = 1; i <= K; i++){
        cps(nadj[i]);
    }
    for(int i = 1; i <= K; i++) if(in[i] == 0){
        dfs(i);
    }
    for(auto o: prs){
        int u = gr[o.fi], v = gr[o.se];
        int p = lca(u, v);
        val[in[u]]++;
        val[in[p]]--;
        val[in[v]] += MASK(20);
        val[in[p]] -= MASK(20);
    }

    for(int i = K; i >= 1; i--){
        val[i] += val[i + 1];
    }

    for(int i = 2; i <= K; i++){
        ll c = val[in[i]] - val[out[i] + 1];
        int u = d[i][0], v = i;
        if(mp.count(make_pair(u, v))){
            int id = mp[make_pair(u, v)];
            if(c >= MASK(20)) ans[id] = 1;
            else if(c >= 1) ans[id] = 2;
        }
        else{
            int id = mp[make_pair(v, u)];
            if(c >= MASK(20)) ans[id] = 2;
            else ans[id] = 1;
        }
    }

    for(int i = 0; i < M; i++){
        if(!ans[i]) cout << "B";
        else if(ans[i] == 1) cout << "R";
        else cout << "L";
    }
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    // freopen("order.inp","r",stdin);
    // freopen("order.out","w",stdout);

    int t = 1;
//    cin >> t;
    while(t--){
        solve();
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 5720 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 5720 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 5720 KB Output isn't correct
2 Halted 0 ms 0 KB -