Submission #556053

# Submission time Handle Problem Language Result Execution time Memory
556053 2022-05-02T09:18:11 Z Tien_Noob One-Way Streets (CEOI17_oneway) C++17
100 / 100
132 ms 29120 KB
//Make CSP great again
//Vengeance
#include <bits/stdc++.h>
#define TASK "TESTCODE"
#define Log2(x) 31 - __builtin_clz(x)
using namespace std;
const int N = 1e5, cbit = 16;
vector<int> adj[N + 1], edge[N + 1];
int n, m, q, l[N + 1], r[N + 1], dp[N + 1][2], f[N + 1][cbit + 1], p[N + 1], res[N + 1], depth[N + 1];
int num[N + 1], low[N + 1], Count, Comp[N + 1], CompCount;
stack<int> st;
bool visited[N + 1];
void read()
{
    cin >> n >> m;
    for (int i = 1; i <= m; ++ i)
    {
        cin >> l[i] >> r[i];
        adj[l[i]].push_back(i);
        adj[r[i]].push_back(i);
    }
}
void visit(int u, int pre)
{
    st.push(u);
    num[u] = low[u] = ++Count;
    for (int i : adj[u])
    {
        if (i == pre)
        {
            continue;
        }
        int v = l[i] ^ u ^ r[i];
        if (num[v])
        {
            low[u] = min(low[u], num[v]);
        }
        else
        {
            visit(v, i);
            low[u] = min(low[u], low[v]);
        }
    }
    if (low[u] == num[u])
    {
        int v;
        ++CompCount;
        do
        {
            v = st.top();
            st.pop();
            Comp[v] = CompCount;
            low[v] = num[v] = 1e9;
        }
        while(v != u);
    }
}
void Compress(int u)
{
    visited[u] = true;
    for (int i : adj[u])
    {
        int v = l[i] ^ r[i] ^ u;
        if (Comp[u] == Comp[v])
        {
            res[i] = 2;
        }
        if (visited[v])
        {
            continue;
        }
        if (Comp[v] != Comp[u])
        {
            p[Comp[v]] = i;
            edge[Comp[u]].push_back(Comp[v]);
            depth[Comp[v]] = depth[Comp[u]] + 1;
            f[Comp[v]][0] = Comp[u];
            for (int k = 1; k <= cbit; ++ k)
            {
                f[Comp[v]][k] = f[f[Comp[v]][k - 1]][k - 1];
            }
            //cerr << Comp[u] << ' ' << Comp[v] << '\n';
        }
        Compress(v);
    }
}
int LCA(int u, int v)
{
    if (depth[u] > depth[v])
    {
        swap(u, v);
    }
    int k = depth[v] - depth[u];
    for (int i = cbit; i >= 0; -- i)
    {
        if ((k >> i) & 1)
        {
            v = f[v][i];
        }
    }
    if (u == v)
    {
        return u;
    }
    for (int i = cbit; i >= 0; -- i)
    {
        if (f[u][i] != f[v][i])
        {
            u = f[u][i];
            v = f[v][i];
        }
    }
    return f[u][0];
}
void DFS(int u)
{
    //0 : R, 1 : L, 2 : B
    for (int v : edge[u])
    {
        DFS(v);
        dp[u][0] += dp[v][0];
        dp[u][1] += dp[v][1];
        int i = p[v];
        if (dp[v][0] == 0 && dp[v][1] == 0)
        {
            res[i] = 2;
            continue;
        }
        if (dp[v][0] > 0 && depth[Comp[l[i]]] < depth[Comp[r[i]]])
        {
            res[i] = 1;
        }
        if (dp[v][1] > 0 && depth[Comp[l[i]]] > depth[Comp[r[i]]])
        {
            res[i] = 1;
        }
    }
}
void solve()
{
    for (int i = 1; i <= n; ++ i)
    {
        if (num[i] == 0)
        {
            visit(i, 0);
        }
    }
    //cerr << Comp[1] << ' ' << Comp[2] << '\n';
    for (int i = 1; i <= n; ++ i)
    {
        if (!visited[i])
        {
            Compress(i);
        }
    }
    cin >> q;
    while(q--)
    {
        int u, v;
        cin >> u >> v;
        u = Comp[u];
        v = Comp[v];
        if (u == v)
        {
            continue;
        }
        int l = LCA(u, v);
        ++dp[u][0];
        --dp[l][0];
        ++dp[v][1];
        --dp[l][1];
        //cerr << u << ' ' << l << ' ' << v << '\n';
    }
    for (int i = 1; i <= CompCount; ++ i)
    {
        if (depth[i] == 0)
        {
            DFS(i);
        }
    }
    for (int i = 1; i <= m; ++ i)
    {
        if (res[i] == 2)
        {
            cout << 'B';
        }
        if (res[i] == 1)
        {
            cout << 'L';
        }
        if (res[i] == 0)
        {
            cout << 'R';
        }
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    if (fopen(TASK".INP", "r"))
    {
        freopen(TASK".INP", "r", stdin);
        //freopen(TASK".OUT", "w", stdout);
    }
    int t = 1;
    bool typetest = false;
    if (typetest)
    {
        cin >> t;
    }
    for (int __ = 1; __ <= t; ++ __)
    {
        //cout << "Case " << __ << ": ";
        read();
        solve();
    }
}

Compilation message

oneway.cpp: In function 'int main()':
oneway.cpp:203:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  203 |         freopen(TASK".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 3 ms 5144 KB Output is correct
4 Correct 4 ms 5204 KB Output is correct
5 Correct 3 ms 5204 KB Output is correct
6 Correct 4 ms 5076 KB Output is correct
7 Correct 3 ms 5168 KB Output is correct
8 Correct 3 ms 5168 KB Output is correct
9 Correct 3 ms 5036 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 3 ms 5144 KB Output is correct
4 Correct 4 ms 5204 KB Output is correct
5 Correct 3 ms 5204 KB Output is correct
6 Correct 4 ms 5076 KB Output is correct
7 Correct 3 ms 5168 KB Output is correct
8 Correct 3 ms 5168 KB Output is correct
9 Correct 3 ms 5036 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 38 ms 10592 KB Output is correct
12 Correct 40 ms 11808 KB Output is correct
13 Correct 48 ms 13492 KB Output is correct
14 Correct 59 ms 16272 KB Output is correct
15 Correct 65 ms 17432 KB Output is correct
16 Correct 77 ms 21736 KB Output is correct
17 Correct 70 ms 24200 KB Output is correct
18 Correct 94 ms 21708 KB Output is correct
19 Correct 80 ms 25592 KB Output is correct
20 Correct 41 ms 11656 KB Output is correct
21 Correct 40 ms 11348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 3 ms 5144 KB Output is correct
4 Correct 4 ms 5204 KB Output is correct
5 Correct 3 ms 5204 KB Output is correct
6 Correct 4 ms 5076 KB Output is correct
7 Correct 3 ms 5168 KB Output is correct
8 Correct 3 ms 5168 KB Output is correct
9 Correct 3 ms 5036 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 38 ms 10592 KB Output is correct
12 Correct 40 ms 11808 KB Output is correct
13 Correct 48 ms 13492 KB Output is correct
14 Correct 59 ms 16272 KB Output is correct
15 Correct 65 ms 17432 KB Output is correct
16 Correct 77 ms 21736 KB Output is correct
17 Correct 70 ms 24200 KB Output is correct
18 Correct 94 ms 21708 KB Output is correct
19 Correct 80 ms 25592 KB Output is correct
20 Correct 41 ms 11656 KB Output is correct
21 Correct 40 ms 11348 KB Output is correct
22 Correct 132 ms 25280 KB Output is correct
23 Correct 125 ms 23332 KB Output is correct
24 Correct 118 ms 22840 KB Output is correct
25 Correct 120 ms 29120 KB Output is correct
26 Correct 111 ms 24748 KB Output is correct
27 Correct 125 ms 23444 KB Output is correct
28 Correct 29 ms 8492 KB Output is correct
29 Correct 59 ms 12300 KB Output is correct
30 Correct 64 ms 12492 KB Output is correct
31 Correct 61 ms 12872 KB Output is correct
32 Correct 78 ms 18460 KB Output is correct