Submission #860172

# Submission time Handle Problem Language Result Execution time Memory
860172 2023-10-12T02:26:58 Z Nhoksocqt1 One-Way Streets (CEOI17_oneway) C++17
100 / 100
113 ms 29532 KB
#include<bits/stdc++.h>
using namespace std;

#define inf 0x3f3f3f3f
#define sz(x) int((x).size())
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> ii;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int Random(int l, int r) {
    return uniform_int_distribution<int>(l, r)(rng);
}

const int MAXN = 100005;

struct Edge {
    int u, v;
} edge[MAXN];

vector<ii> adj[MAXN];
vector<int> adjp[MAXN];
ii sortedDepth[MAXN];
int cnt[MAXN][2], depth[MAXN], P[MAXN][17];
int low[MAXN], num[MAXN], compID[MAXN], numNode, numComp, numEdge, numPath;
bool dx[MAXN];

stack<int> st;
void dfs(int u) {
    low[u] = num[u] = ++num[0];
    st.push(u);

    for (int it = 0; it < sz(adj[u]); ++it) {
        int v(adj[u][it].fi), id(adj[u][it].se);
        if(!dx[id]) {
            dx[id] = 1;
            if(!num[v]) {
                dfs(v);
                low[u] = min(low[u], low[v]);
            } else {
                low[u] = min(low[u], num[v]);
            }
        }
    }

    if(low[u] == num[u]) {
        ++numComp;
        int v;
        do {
            v = st.top(); st.pop();
            low[v] = num[v] = 1e9+7;
            compID[v] = numComp;
        } while(v != u);
    }
}

void dfsTree(int u) {
    num[u] = num[0];
    for (int it = 0; it < sz(adjp[u]); ++it) {
        int v(adjp[u][it]);
        if(!num[v]) {
            depth[v] = depth[u] + 1;
            P[v][0] = u;
            dfsTree(v);
        }
    }
}

int lca(int u, int v) {
    if(depth[u] < depth[v])
        swap(u, v);

    for (int i1 = depth[u] - depth[v]; i1 > 0; i1 ^= i1 & -i1) {
        int i = __builtin_ctz(i1);
        u = P[u][i];
    }

    if(u == v)
        return u;

    for (int i = 31 - __builtin_clz(depth[u]); i >= 0; --i) {
        if(P[u][i] != P[v][i])
            u = P[u][i], v = P[v][i];
    }

    return P[u][0];
}

void process(void) {
    cin >> numNode >> numEdge;
    for (int i = 0; i < numEdge; ++i) {
        int u, v;
        cin >> u >> v;
        edge[i] = {u, v};
        adj[u].push_back({v, i});
        adj[v].push_back({u, i});
    }

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

    for (int i = 1; i <= numNode; ++i) {
        for (int it = 0; it < sz(adj[i]); ++it) {
            int j(adj[i][it].fi), id(adj[i][it].se);
            if(compID[i] != compID[j]) {
                adjp[compID[i]].push_back(compID[j]);
            }
        }
    }

    for (int i = 1; i <= numComp; ++i)
        num[i] = 0;

    for (int i = 1; i <= numComp; ++i) {
        if(!num[i]) {
            ++num[0];
            depth[i] = 1, P[i][0] = -1;
            dfsTree(i);
        }
    }

    for (int j = 1; (1 << j) <= numComp; ++j) {
        for (int i = 1; i <= numComp; ++i) {
            if(P[i][j - 1] != -1) {
                P[i][j] = P[P[i][j - 1]][j - 1];
            } else {
                P[i][j] = -1;
            }
        }
    }

    cin >> numPath;
    for (int i = 0; i < numPath; ++i) {
        int u, v;
        cin >> u >> v;
        u = compID[u], v = compID[v];

        assert(num[u] == num[v]);
        int par = lca(u, v);

        ++cnt[u][0], ++cnt[v][1];
        --cnt[par][0], --cnt[par][1];
    }

    for (int i = 1; i <= numComp; ++i)
        sortedDepth[i] = {depth[i], i};

    sort(sortedDepth + 1, sortedDepth + numComp + 1);
    for (int i = numComp; i > 0; --i) {
        int u(sortedDepth[i].se);
        if(P[u][0] != -1) {
            cnt[P[u][0]][0] += cnt[u][0];
            cnt[P[u][0]][1] += cnt[u][1];
        }
    }

    for (int i = 0; i < numEdge; ++i) {
        int u(edge[i].u), v(edge[i].v);
        u = compID[u], v = compID[v];
        if(u == v) {
            cout << 'B';
        } else {
            bool isSwapped(0);

            if(P[u][0] == v) {
                swap(u, v);
                isSwapped = 1;
            }

            //cout << '.' << edge[i].u << ' ' << edge[i].v << ' ' << u << ' ' << v << ' ' << cnt[v][0] << ' ' << cnt[v][1] << '\n';
            if(cnt[v][0] && cnt[v][1] || !cnt[v][0] && !cnt[v][1]) {
                cout << 'B';
            } else {
                cout << (((cnt[v][1] > 0) ^ isSwapped) ? 'R' : 'L');
            }
        }
    }

    cout << '\n';
}

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

    process();
    return 0;
}

Compilation message

oneway.cpp: In function 'void process()':
oneway.cpp:107:35: warning: unused variable 'id' [-Wunused-variable]
  107 |             int j(adj[i][it].fi), id(adj[i][it].se);
      |                                   ^~
oneway.cpp:174:26: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
  174 |             if(cnt[v][0] && cnt[v][1] || !cnt[v][0] && !cnt[v][1]) {
      |                ~~~~~~~~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 2 ms 8692 KB Output is correct
3 Correct 2 ms 8796 KB Output is correct
4 Correct 2 ms 8796 KB Output is correct
5 Correct 2 ms 8796 KB Output is correct
6 Correct 2 ms 8796 KB Output is correct
7 Correct 2 ms 8796 KB Output is correct
8 Correct 3 ms 8796 KB Output is correct
9 Correct 2 ms 8796 KB Output is correct
10 Correct 2 ms 8796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 2 ms 8692 KB Output is correct
3 Correct 2 ms 8796 KB Output is correct
4 Correct 2 ms 8796 KB Output is correct
5 Correct 2 ms 8796 KB Output is correct
6 Correct 2 ms 8796 KB Output is correct
7 Correct 2 ms 8796 KB Output is correct
8 Correct 3 ms 8796 KB Output is correct
9 Correct 2 ms 8796 KB Output is correct
10 Correct 2 ms 8796 KB Output is correct
11 Correct 26 ms 14936 KB Output is correct
12 Correct 29 ms 15952 KB Output is correct
13 Correct 35 ms 17200 KB Output is correct
14 Correct 40 ms 20820 KB Output is correct
15 Correct 42 ms 21388 KB Output is correct
16 Correct 56 ms 23888 KB Output is correct
17 Correct 56 ms 25436 KB Output is correct
18 Correct 58 ms 23888 KB Output is correct
19 Correct 57 ms 26520 KB Output is correct
20 Correct 28 ms 15452 KB Output is correct
21 Correct 28 ms 15192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 2 ms 8692 KB Output is correct
3 Correct 2 ms 8796 KB Output is correct
4 Correct 2 ms 8796 KB Output is correct
5 Correct 2 ms 8796 KB Output is correct
6 Correct 2 ms 8796 KB Output is correct
7 Correct 2 ms 8796 KB Output is correct
8 Correct 3 ms 8796 KB Output is correct
9 Correct 2 ms 8796 KB Output is correct
10 Correct 2 ms 8796 KB Output is correct
11 Correct 26 ms 14936 KB Output is correct
12 Correct 29 ms 15952 KB Output is correct
13 Correct 35 ms 17200 KB Output is correct
14 Correct 40 ms 20820 KB Output is correct
15 Correct 42 ms 21388 KB Output is correct
16 Correct 56 ms 23888 KB Output is correct
17 Correct 56 ms 25436 KB Output is correct
18 Correct 58 ms 23888 KB Output is correct
19 Correct 57 ms 26520 KB Output is correct
20 Correct 28 ms 15452 KB Output is correct
21 Correct 28 ms 15192 KB Output is correct
22 Correct 113 ms 26512 KB Output is correct
23 Correct 89 ms 24820 KB Output is correct
24 Correct 77 ms 25164 KB Output is correct
25 Correct 86 ms 29532 KB Output is correct
26 Correct 90 ms 26128 KB Output is correct
27 Correct 82 ms 24944 KB Output is correct
28 Correct 24 ms 12892 KB Output is correct
29 Correct 40 ms 16220 KB Output is correct
30 Correct 44 ms 16420 KB Output is correct
31 Correct 50 ms 16756 KB Output is correct
32 Correct 59 ms 22356 KB Output is correct