답안 #570888

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
570888 2022-05-31T13:45:56 Z bojackduy One-Way Streets (CEOI17_oneway) C++14
0 / 100
25 ms 33928 KB
#include<iostream>
#include<queue>
#include<stack>
#include<set>
#include<algorithm>
#include<string.h>
#include<map>
#define task 
#define size() size() * 1ll 
#define all(x) (x).begin(), (x).end()
#define allr(x, sz) (x) + 1, (x) + 1 + sz
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define MASK(x) (1LL<<(x))
#define BIT(x,i) (((x)>>(i))&1)
#define numbit(x) __builtin_popcountll(x)

using namespace std;

const int N = 1e5 + 1;
const int M = 1e3 + 1;
const long long mod = 1e9 + 7;
const long long oo = 1e18 + 7;

typedef vector<int> vi;
typedef vector<pii> vii;
typedef long long ll;

template<class t>
bool mini(t &x,t y) {
    if (x > y) {
       x = y;
       return 1;
    }
return 0;
}
template<class t>
bool maxi(t &x,t y) {
    if (x < y) {
       x = y;
       return 1;
    }
return 0;
}
void file() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //freopen(task.in, r, stdin);
    //freopen(task.out, w, stdout);
return ;
}

int n, m, p;
pii e[N];
vi a[N], adj[N];
map<pii, int> mp, mp1;

stack<int> st;
int num[N], low[N];

void dfs(int u, int chu) {
    st.push(u);
    num[u] = low[u] = ++num[0];
    for (auto v: a[u]) if (v != chu) {
        if (num[v]) mini(low[u], num[v]); else {
            dfs(v, u);
            mini(low[u], low[v]);
        }
    }
    if (num[u] == low[u]) {
        int v;
        low[0]++;
        do {
            v = st.top(), st.pop();
            num[v] = low[v] = mod + low[0];
        } while (v != u);
    }
}

const int K = 30;
int cha[N], h[N], sub[N], heavy[N], head[N], pos[N], par[N][K], d[N];

void dfs1(int u, int chu) {
    sub[u] = 1;
    par[u][0] = cha[u] = chu;
    int cmax = -1;
    for (auto v: adj[u]) if (v != chu) {
        d[v] = d[u] + 1;
        dfs1(v, u);
        maxi(h[u], h[v] + 1);
        sub[u] += sub[v];
        if (maxi(cmax, sub[v])) {
            heavy[u] = v;
        }
    }
}

void hld(int u, int h) {
    pos[u] = ++pos[0];
    num[pos[0]] = u;
    head[u] = h;
    if (heavy[u]) hld(heavy[u], h);
    for (auto v: adj[u]) if (v != cha[u]) {
        if (v != heavy[u]) hld(v, v);
    }
}

int lca(int a, int b) {
    while (head[a] != head[b]) {
        if (h[head[a]] < h[head[b]]) swap(a, b);
        a = cha[head[a]];
    }
    if (h[a] < h[b]) swap(a, b);
return a;
}

struct IT {
    int n;
    vii st;
    IT(const int &_n = 0): n(_n + 1), st(4 *_n + 1, pii(0, 0)) {}
    void down(int id) {
        int x = st[id].se;
        if (x == 0) return ;
        st[id << 1].fi = x;
        st[id << 1].se = x;

        st[id << 1 | 1].fi = x;
        st[id << 1 | 1].se = x;
        x = 0;
    }
    int merge(int x, int y) {
        if (x == 0) return y;
    return x;
    }
    void adj(int id, int l, int r, int u, int v, int val) {
        if (r < u || v < l) return ;
        if (u <= l && r <= v) {
            st[id].fi = val;
            st[id].se = val;
        return ;
        }
        int mid = l + r >> 1;
        down(id);
        adj(id << 1, l, mid, u, v, val);
        adj(id << 1 | 1, mid + 1, r, u, v, val);
        st[id].fi = merge(st[id << 1].fi, st[id << 1 | 1].fi);
    }
    int get(int id, int l, int r, int i) {
        if (r < i || i < l) return 0;
        if (l == r) return st[id].fi;
        int mid = l + r >> 1;
        down(id);
    return merge(get(id << 1, l, mid, i), get(id << 1 | 1, mid + 1, r, i));
    }
} it;
void lef(int a, int b) {
    while (head[a] != head[b]) {
        int x = head[a];
        it.adj(1, 1, n, pos[x], pos[a], 1);
        a = cha[x];
    }
    it.adj(1, 1, n, pos[b], pos[a], 1);
}
void rig(int a, int b) {
    while (head[a] != head[b]) {
        int x = head[a];
        it.adj(1, 1, n, pos[x], pos[a], -1);
        a = cha[x];
    }
    it.adj(1, 1, n, pos[b], pos[a], -1);
}


int LCA(int u, int v) {
    if (d[u] < d[v]) swap(u, v);
    int k = __builtin_log2(d[u]);
    for (; k >= 0; k--) {
        if (d[par[u][k]] >= d[v]) u = par[u][k];
    }
    for (; k >= 0; k--) {
        if (par[u][k] != -1 && par[u][k] != par[v][k]) {
            u = par[u][k];
            v = par[v][k];
        }
    }
return par[u][0];
}

int dp[N];
void dfs2(int u, int chu) {
    for (auto v: adj[u]) if (v != chu) {
        dfs2(v, u);
        dp[u] += dp[v];
    }
}
void solve(int test = -1) {
    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        int x, y;
        cin >> x >> y;
        a[x].pb(y);
        a[y].pb(x);
        e[i] = pii(x, y);
    }

    dfs(1, -1);
    num[0] = 0;
    for (int i = 1; i <= n; i++) {
        num[i] = 0;
        for (auto v: a[i]) {
            if (low[i] != low[v]) {
                adj[low[i] - mod].pb(low[v] - mod);
            }
        }
    }



    memset(par, -1, sizeof par);
    dfs1(1, -1);
    for (int j = 1; MASK(j) <= n; j++) {
        for (int i = 1; i <= n; i++) {
            par[i][j] = par[par[i][j - 1]][j - 1];
        }
    }

    // hld(1, 1);
    // it = IT(low[0]);
    cin >> p;
    for (int i = 1; i <= p; i++) {
        int x, y;
        cin >> x >> y;
        x = low[x] - mod;
        y = low[y] - mod;
        // int CHA = lca(x, y);
        // lef(x, CHA);
        // rig(y, CHA);

        int PAR = LCA(x, y);
        dp[x]++;
        dp[y]--;
    }
    dfs2(1, -1);
    for (int i = 2; i <= low[0]; i++) {
        int u = cha[i];
        mp[pii(i, u)] = dp[i];
        mp[pii(u, i)] = -dp[i];
    }
    for (int i = 1; i <= n; i++) {
        for (auto v: a[i]) {
            mp1[pii(i, v)] = mp[pii(low[i] - mod, low[v] - mod)];
        }
    }
    for (int i = 1; i <= m; i++) {
        int u = e[i].fi;
        int v = e[i].se;
        cout << (mp1[pii(u, v)] == 0 ? "B" : mp1[pii(u, v)] == 1 ? "R" : "L");
    }
}

int32_t main()  {
    file();
    int T = 1;
    // cin >> T;
    for (int i = 1; i <= T; i++) {
        solve(i);
    }
return 0;
}

Compilation message

oneway.cpp: In member function 'void IT::adj(int, int, int, int, int, int)':
oneway.cpp:145:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  145 |         int mid = l + r >> 1;
      |                   ~~^~~
oneway.cpp: In member function 'int IT::get(int, int, int, int)':
oneway.cpp:154:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  154 |         int mid = l + r >> 1;
      |                   ~~^~~
oneway.cpp: In function 'void solve(int)':
oneway.cpp:242:13: warning: unused variable 'PAR' [-Wunused-variable]
  242 |         int PAR = LCA(x, y);
      |             ^~~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 25 ms 33928 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 25 ms 33928 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 25 ms 33928 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -