답안 #1071572

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1071572 2024-08-23T08:55:37 Z kiethm07 One-Way Streets (CEOI17_oneway) C++11
100 / 100
72 ms 24148 KB
#include <bits/stdc++.h>

#define pii pair<int,int>
#define fi first
#define se second

using namespace std;

const int N = 1e5 + 5;

vector<pii> a[N];
vector<pii> t[N];
int n,m,q;
int low[N], num[N];
int component[N];
bool bridge[N];
bool inv[N];
int pa[N], sz[N], head[N], pos[N], d_hld[N], id;
int val[N];
int ans[N];
int cnt = 0;

struct edge{
    int u,v;
    edge(){}
};

edge e[N];

void dfs1(int u,int pre){
    low[u] = num[u] = ++cnt;
    for (int i = 0; i < a[u].size(); i++){
        int v = a[u][i].fi;
        int id = a[u][i].se;
        if (id == pre) continue;
        if (num[v] != 0){
            low[u] = min(low[u], num[v]);
        }
        else{
            dfs1(v,id);
            low[u] = min(low[u], low[v]);
        }
        bridge[id] = low[v] == num[v];
    }
}

void build(int u,int c){
    component[u] = c;
    for (int i = 0; i < a[u].size(); i++){
        int v = a[u][i].fi;
        int id = a[u][i].se;
        if (bridge[id]) continue;
        if (component[v] != 0) continue;
        build(v,c);
    }
}

void dfs2(int u,int p){
    sz[u] = 1;
    for (int i = 0; i < t[u].size(); i++){
        int v = t[u][i].fi;
        int id = t[u][i].se;
        if (v == p) continue;
        inv[id] = u != component[e[id].u];
        pa[v] = u;
        dfs2(v,u);
        sz[u] += sz[v];
    }
}

void hld(int u,int p){
    pos[u] = ++id;
    int cur = 0;
    int nxt = -1;
    for (int i = 0; i < t[u].size(); i++){
        int v = t[u][i].fi;
        if (v == p) continue;
        if (sz[v] > cur){
            cur = sz[v];
            nxt = v;
        }
    }
    if (nxt != -1){
        head[nxt] = head[u];
        d_hld[nxt] = d_hld[u];
        hld(nxt,u);
        for (int i = 0; i < t[u].size(); i++){
            int v = t[u][i].fi;
            if (v == p || v == nxt) continue;
            head[v] = v;
            d_hld[v] = d_hld[u] + 1;
            hld(v,u);
        }
    }
}

int lca(int u,int v){
    if (d_hld[u] < d_hld[v]) swap(u,v);
    while(d_hld[u] > d_hld[v]) u = pa[head[u]];
    while(head[u] != head[v]){
        u = pa[head[u]];
        v = pa[head[v]];
    }
    if (pos[u] > pos[v]) swap(u,v);
    return u;
}

void dfs3(int u,int p){
    for (int i = 0; i < t[u].size(); i++){
        int v = t[u][i].fi;
        int id = t[u][i].se;
        if (v == p) continue;
        dfs3(v,u);
        if (val[v] < 0) ans[id] = -1;
        else if (val[v] > 0) ans[id] = 1;
        val[u] += val[v];
    }
}

int main(){
    cin.tie(0) -> sync_with_stdio(0);
    #define repdimahuhu "Phanluong"
    if (fopen(repdimahuhu".inp","r")){
        freopen(repdimahuhu".inp","r",stdin);
        freopen(repdimahuhu".out","w",stdout);
    }
    cin >> n >> m;
    for (int i = 1; i <= m; i++){
        int u,v;
        cin >> u >> v;
        a[u].push_back(pii(v,i));
        a[v].push_back(pii(u,i));
        e[i].u = u;
        e[i].v = v;
    }
    for (int i = 1; i <= n; i++){
        if (num[i] == 0) dfs1(i,-1);
    }
    cnt = 0;
    for (int i = 1; i <= n; i++){
        if (component[i] != 0) continue;
        build(i,++cnt);
    }
    for (int i = 1; i <= m; i++){
        int u = component[e[i].u];
        int v = component[e[i].v];
        if (u == v) continue;
        t[u].push_back(pii(v,i));
        t[v].push_back(pii(u,i));
    }
    for (int i = 1; i <= cnt; i++){
        if (pa[i] != 0) continue;
        pa[i] = -1;
        dfs2(i,-1);
        head[i] = i;
        hld(i,-1);
    }
    cin >> q;
    while(q--){
        int u,v;
        cin >> u >> v;
        u = component[u];
        v = component[v];
        val[u] += 1;
        val[v] -= 1;
    }
    for (int i = 1; i <= cnt; i++){
        if (pa[i] != -1) continue;
        dfs3(i,-1);
    }
//    for (int i = 1; i <= m; i++){
//        int u = e[i].u;
//        int v = e[i].v;
//        cout << u << " " << v << " " << inv[i] << "\n";
//    }
    for (int i = 1; i <= m; i++){
        if (inv[i]) ans[i] *= -1;
        if (ans[i] == 0) cout << "B";
        if (ans[i] == -1) cout << "R";
        if (ans[i] == 1) cout << "L";
    }
    return 0;
}

Compilation message

oneway.cpp: In function 'void dfs1(int, int)':
oneway.cpp:32:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   32 |     for (int i = 0; i < a[u].size(); i++){
      |                     ~~^~~~~~~~~~~~~
oneway.cpp: In function 'void build(int, int)':
oneway.cpp:49:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |     for (int i = 0; i < a[u].size(); i++){
      |                     ~~^~~~~~~~~~~~~
oneway.cpp: In function 'void dfs2(int, int)':
oneway.cpp:60:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |     for (int i = 0; i < t[u].size(); i++){
      |                     ~~^~~~~~~~~~~~~
oneway.cpp: In function 'void hld(int, int)':
oneway.cpp:75:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   75 |     for (int i = 0; i < t[u].size(); i++){
      |                     ~~^~~~~~~~~~~~~
oneway.cpp:87:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   87 |         for (int i = 0; i < t[u].size(); i++){
      |                         ~~^~~~~~~~~~~~~
oneway.cpp: In function 'void dfs3(int, int)':
oneway.cpp:109:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  109 |     for (int i = 0; i < t[u].size(); i++){
      |                     ~~^~~~~~~~~~~~~
oneway.cpp: In function 'int main()':
oneway.cpp:124:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  124 |         freopen(repdimahuhu".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
oneway.cpp:125:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  125 |         freopen(repdimahuhu".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5724 KB Output is correct
2 Correct 2 ms 5724 KB Output is correct
3 Correct 2 ms 5724 KB Output is correct
4 Correct 2 ms 5724 KB Output is correct
5 Correct 2 ms 5724 KB Output is correct
6 Correct 1 ms 5724 KB Output is correct
7 Correct 2 ms 5724 KB Output is correct
8 Correct 2 ms 5724 KB Output is correct
9 Correct 2 ms 5724 KB Output is correct
10 Correct 2 ms 5724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5724 KB Output is correct
2 Correct 2 ms 5724 KB Output is correct
3 Correct 2 ms 5724 KB Output is correct
4 Correct 2 ms 5724 KB Output is correct
5 Correct 2 ms 5724 KB Output is correct
6 Correct 1 ms 5724 KB Output is correct
7 Correct 2 ms 5724 KB Output is correct
8 Correct 2 ms 5724 KB Output is correct
9 Correct 2 ms 5724 KB Output is correct
10 Correct 2 ms 5724 KB Output is correct
11 Correct 24 ms 13144 KB Output is correct
12 Correct 32 ms 14164 KB Output is correct
13 Correct 35 ms 15196 KB Output is correct
14 Correct 37 ms 16372 KB Output is correct
15 Correct 39 ms 16820 KB Output is correct
16 Correct 48 ms 18516 KB Output is correct
17 Correct 51 ms 20052 KB Output is correct
18 Correct 48 ms 18768 KB Output is correct
19 Correct 48 ms 21132 KB Output is correct
20 Correct 36 ms 13648 KB Output is correct
21 Correct 32 ms 13396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5724 KB Output is correct
2 Correct 2 ms 5724 KB Output is correct
3 Correct 2 ms 5724 KB Output is correct
4 Correct 2 ms 5724 KB Output is correct
5 Correct 2 ms 5724 KB Output is correct
6 Correct 1 ms 5724 KB Output is correct
7 Correct 2 ms 5724 KB Output is correct
8 Correct 2 ms 5724 KB Output is correct
9 Correct 2 ms 5724 KB Output is correct
10 Correct 2 ms 5724 KB Output is correct
11 Correct 24 ms 13144 KB Output is correct
12 Correct 32 ms 14164 KB Output is correct
13 Correct 35 ms 15196 KB Output is correct
14 Correct 37 ms 16372 KB Output is correct
15 Correct 39 ms 16820 KB Output is correct
16 Correct 48 ms 18516 KB Output is correct
17 Correct 51 ms 20052 KB Output is correct
18 Correct 48 ms 18768 KB Output is correct
19 Correct 48 ms 21132 KB Output is correct
20 Correct 36 ms 13648 KB Output is correct
21 Correct 32 ms 13396 KB Output is correct
22 Correct 63 ms 21040 KB Output is correct
23 Correct 63 ms 19476 KB Output is correct
24 Correct 72 ms 19792 KB Output is correct
25 Correct 63 ms 24148 KB Output is correct
26 Correct 62 ms 20640 KB Output is correct
27 Correct 66 ms 19840 KB Output is correct
28 Correct 22 ms 11352 KB Output is correct
29 Correct 50 ms 14420 KB Output is correct
30 Correct 35 ms 14684 KB Output is correct
31 Correct 40 ms 14936 KB Output is correct
32 Correct 48 ms 18080 KB Output is correct