답안 #866766

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
866766 2023-10-27T03:17:13 Z _anhxinloi_ One-Way Streets (CEOI17_oneway) C++14
100 / 100
69 ms 20692 KB
#include<bits/stdc++.h>
#define int long long
#define fi first
#define se second

using namespace std;

const int maxn = 1e5 + 5;
const int inf = 1e18;
typedef pair<int, int> pii;

int low[maxn], num[maxn], tds, scc;
int lab[maxn], n, m, d[maxn], q;
char s[maxn];
stack<int> st;
vector<int> adj[maxn], g[maxn];
pii canh[maxn];
bool check[maxn];

void dfs(int u, int par)
{
    low[u] = num[u] = ++tds;
    st.push(u);
    int tds = 0;
    for(int v : adj[u]){
        if(v != par || tds == 1){
            if(num[v] != 0)
                low[u] = min(low[u], num[v]);
            else{
                dfs(v, u);
                low[u] = min(low[u], low[v]);
            }
        }
        else{
            tds++;
        }
    }

    if(low[u] >= num[u]){
        scc++;
        int tmp;
        do
        {
            tmp = st.top();
            st.pop();
            lab[tmp] = scc;
            for (int w: adj[tmp]) {
                if (lab[w] && scc != lab[w]){
                    g[scc].push_back(lab[w]);
//                    cout << lab[w] << ' ' << scc << endl;
                }
            }
            low[tmp] = num[tmp] = inf;
        }while(tmp != u);
    }
}

void dfs_work(int u)
{
    check[u] = true;
    for(int v : g[u]){
        if(!check[v])
            dfs_work(v);
        d[u] += d[v];
    }
}

signed main()
{
//    freopen("oneway.inp" , "r" , stdin);
//    freopen("oneway.out" , "w" , stdout);

    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    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);
        canh[i] = {u, v};
    }

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

//    for(int i=1 ; i<=m ; ++i){
//        auto [u, v] = canh[i];
//        int x = lab[u];
//        int y = lab[v];
//        if(x > y){
//            g[max(x, y)].push_back(min(x, y));
//        }
//    }

    cin >> q;
    for(int i=1 ; i<=q ; ++i){
        int u, v;   cin >> u >> v;
        u = lab[u]; v = lab[v];
        d[v]--; d[u]++;
    }

    for(int i=1 ; i<=scc ; ++i){
        if(!check[i]){
            dfs_work(i);
        }
    }

    for(int i=1 ; i<=m ; ++i){
        auto [u, v] = canh[i];
        int x = lab[u]; int y = lab[v];

        if(x == y){
            s[i] = 'B';
        }

        if(x > y)
        {
            if(d[y] > 0)    s[i] = 'L';
            if(d[y] < 0)    s[i] = 'R';
            if(d[y] == 0)   s[i] = 'B';
        }

        if(x < y)
        {
            if(d[x] > 0)    s[i] = 'R';
            if(d[x] < 0)    s[i] = 'L';
            if(d[x] == 0)   s[i] = 'B';
        }
    }

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

/// code by yourlove ///

Compilation message

oneway.cpp: In function 'int main()':
oneway.cpp:112:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  112 |         auto [u, v] = canh[i];
      |              ^
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 2 ms 8760 KB Output is correct
5 Correct 2 ms 8632 KB Output is correct
6 Correct 2 ms 8536 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 2 ms 8540 KB Output is correct
9 Correct 2 ms 8540 KB Output is correct
10 Correct 2 ms 8540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 2 ms 8760 KB Output is correct
5 Correct 2 ms 8632 KB Output is correct
6 Correct 2 ms 8536 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 2 ms 8540 KB Output is correct
9 Correct 2 ms 8540 KB Output is correct
10 Correct 2 ms 8540 KB Output is correct
11 Correct 29 ms 12892 KB Output is correct
12 Correct 28 ms 13932 KB Output is correct
13 Correct 35 ms 14988 KB Output is correct
14 Correct 37 ms 15964 KB Output is correct
15 Correct 37 ms 16164 KB Output is correct
16 Correct 40 ms 15528 KB Output is correct
17 Correct 38 ms 17424 KB Output is correct
18 Correct 36 ms 15708 KB Output is correct
19 Correct 40 ms 18516 KB Output is correct
20 Correct 33 ms 13576 KB Output is correct
21 Correct 41 ms 13136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 2 ms 8760 KB Output is correct
5 Correct 2 ms 8632 KB Output is correct
6 Correct 2 ms 8536 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 2 ms 8540 KB Output is correct
9 Correct 2 ms 8540 KB Output is correct
10 Correct 2 ms 8540 KB Output is correct
11 Correct 29 ms 12892 KB Output is correct
12 Correct 28 ms 13932 KB Output is correct
13 Correct 35 ms 14988 KB Output is correct
14 Correct 37 ms 15964 KB Output is correct
15 Correct 37 ms 16164 KB Output is correct
16 Correct 40 ms 15528 KB Output is correct
17 Correct 38 ms 17424 KB Output is correct
18 Correct 36 ms 15708 KB Output is correct
19 Correct 40 ms 18516 KB Output is correct
20 Correct 33 ms 13576 KB Output is correct
21 Correct 41 ms 13136 KB Output is correct
22 Correct 55 ms 17488 KB Output is correct
23 Correct 69 ms 15952 KB Output is correct
24 Correct 55 ms 15700 KB Output is correct
25 Correct 54 ms 20692 KB Output is correct
26 Correct 51 ms 16976 KB Output is correct
27 Correct 60 ms 15952 KB Output is correct
28 Correct 26 ms 10900 KB Output is correct
29 Correct 53 ms 13092 KB Output is correct
30 Correct 51 ms 13336 KB Output is correct
31 Correct 53 ms 13700 KB Output is correct
32 Correct 50 ms 16168 KB Output is correct