답안 #528209

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
528209 2022-02-19T17:10:07 Z cig32 One-Way Streets (CEOI17_oneway) C++17
100 / 100
1362 ms 99972 KB
#include "bits/stdc++.h"
using namespace std;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
const int MAXN = 1e5 + 10;
const int MOD = 1e9 + 7;
#define int long long
 
int rnd(int x, int y) {
  int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
vector<int> adj[MAXN];
int tin[MAXN], tout[MAXN], low[MAXN], vis[MAXN], tim = 0;
map<pair<int, int>, bool> is_bridge;
map<pair<int, int>, int> o; // appear count
void dfs(int node, int prv) {
  tin[node] = ++tim;
  int mi = 1e9;
  vis[node] = 1;
  for(int x: adj[node]) {
    if(!vis[x]) {
      dfs(x, node);
      mi = min(mi, low[x]);
      //cout << "tree edge: " << node << " -> " << x << "\n";
      if(low[x] > tin[node] && o[{x, node}] == 1) {
        //cout << "bridge found: " << x << " " << node << "\n";
        is_bridge[{x, node}] = is_bridge[{node, x}] = 1;
      }
    }
  }
  tout[node] = ++tim;
  //Calculating low[node]
  low[node] = tin[node];
  for(int x: adj[node]) {
    if(tin[x] < tin[node] && tout[x] == 0 && tin[x] > 0 && x != prv) {
      //cout << x << " is an ancestor of " << node << "\n";
      low[node] = min(low[node], tin[x]);
    }
  }
  low[node] = min(low[node], mi);
  //cout << node << ": " << tin[node] << " " << tout[node] << " " << low[node] <<" \n";
}
int dsu[MAXN];
int set_of(int u) {
  if(dsu[u] == u) return u;
  return dsu[u] = set_of(dsu[u]);
}
void union_(int u, int v) {
  dsu[set_of(u)] = set_of(v);
}
vector<int> adj2[MAXN];
map<pair<int, int>, int> ord;
map<pair<int, int>, int> rel;
map<pair<int, int>, int> delta;
int par[MAXN];
void dfs2(int node, int prv) {
  vis[node] = 1;
  par[node] = prv;
  for(int x: adj2[node]) {
    if(x != prv && !vis[x]) {
      dfs2(x, node);
    }
  }
}
void dfs3(int node, int prv) {
  vis[node] = 1;
  int tot = delta[{node, prv}];
  for(int x: adj2[node]) {
    if(x != prv && !vis[x]) {
      dfs3(x, node);
      tot += delta[{x, node}];
    }
  }
  delta[{node, prv}] = delta[{prv, node}] = tot;
  if(tot > 0)rel[{node, prv}] = rel[{prv, node}] = node;
  else if(tot < 0) rel[{node, prv}] = rel[{prv, node}] = prv;
  else rel[{node, prv}] = rel[{prv, node}] = 0;
}
void solve(int tc) {
  int n, m;
  cin >> n >> m;
  for(int i=1; i<=n; i++) {
    dsu[i] = i;
  }
  pair<int, int> edges[m+1];
  for(int i=1; i<=m; i++) {
    int a, b;
    cin >> a >> b;
    edges[i] = {a, b};
    o[{a, b}]++; o[{b, a}]++;
    if(a == b) continue;
    if(o[{a, b}] > 1) continue;
    ord[edges[i]] = a;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  for(int i=1; i<=n; i++) {
    if(!vis[i]) dfs(i, -1);
  }
  for(int i=1; i<=n; i++) {
    for(int x: adj[i]) {
      if(!is_bridge[{i, x}] || o[{i, x}] > 1) {
        if(set_of(i) != set_of(x)) {
          union_(i, x);
        }
      }
    }
  }
  map<pair<int, int>, bool> alr;
  for(int i=1; i<=n; i++) {
    for(int x: adj[i]) {
      if(set_of(i) != set_of(x) && !alr[{set_of(i), set_of(x)}]) {
        alr[{set_of(i), set_of(x)}] = alr[{set_of(x), set_of(i)}] = 1;
        adj2[set_of(i)].push_back(set_of(x));
        adj2[set_of(x)].push_back(set_of(i));
      }
    }
  }
  //adj2 is the bridge-block tree of the graph
  for(int i=1; i<=n; i++) vis[i] = 0;
  for(int i=1; i<=n; i++) {
    if(!vis[set_of(i)]) dfs2(set_of(i), -1);
  }
  int p; cin >> p;
  for(int i=0; i<p; i++) {
    int x, y;
    cin >> x >> y;
    x = set_of(x), y = set_of(y);
    if(x == y) continue;
    delta[{x, par[x]}]++;
    delta[{par[x], x}]++;
    delta[{y, par[y]}]--;
    delta[{par[y], y}]--;
  }
  for(int i=1; i<=n; i++) vis[i] = 0;
  for(int i=1; i<=n; i++) {
    if(!vis[set_of(i)]) dfs3(set_of(i), -1);
  }
  for(int i=1; i<=m; i++) {
    if(edges[i].first == edges[i].second) {
      cout << "B"; continue;
    }
    else if(o[edges[i]] > 1) {
      cout << "B"; continue;
    }
    else if(!is_bridge[edges[i]]) {
      cout << "B"; continue;
    }
    int r = rel[{set_of(edges[i].first), set_of(edges[i].second)}];
    int q = set_of(ord[edges[i]]);
    if(r > 0) cout << (r == q ? "R" : "L");
    else cout << "B";
  }
  cout << "\n";
}

int32_t main(){
  ios::sync_with_stdio(0); cin.tie(0);
  int t = 1;// cin >> t;
  for(int i=1; i<=t; i++) solve(i);
} 
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 5 ms 5452 KB Output is correct
4 Correct 7 ms 5836 KB Output is correct
5 Correct 7 ms 5836 KB Output is correct
6 Correct 5 ms 5324 KB Output is correct
7 Correct 7 ms 5836 KB Output is correct
8 Correct 7 ms 5836 KB Output is correct
9 Correct 4 ms 5324 KB Output is correct
10 Correct 4 ms 5324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 5 ms 5452 KB Output is correct
4 Correct 7 ms 5836 KB Output is correct
5 Correct 7 ms 5836 KB Output is correct
6 Correct 5 ms 5324 KB Output is correct
7 Correct 7 ms 5836 KB Output is correct
8 Correct 7 ms 5836 KB Output is correct
9 Correct 4 ms 5324 KB Output is correct
10 Correct 4 ms 5324 KB Output is correct
11 Correct 424 ms 44228 KB Output is correct
12 Correct 433 ms 46620 KB Output is correct
13 Correct 466 ms 50400 KB Output is correct
14 Correct 611 ms 60472 KB Output is correct
15 Correct 721 ms 64832 KB Output is correct
16 Correct 1171 ms 87792 KB Output is correct
17 Correct 1055 ms 91208 KB Output is correct
18 Correct 1201 ms 87784 KB Output is correct
19 Correct 1041 ms 93660 KB Output is correct
20 Correct 396 ms 45660 KB Output is correct
21 Correct 325 ms 44092 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 5 ms 5452 KB Output is correct
4 Correct 7 ms 5836 KB Output is correct
5 Correct 7 ms 5836 KB Output is correct
6 Correct 5 ms 5324 KB Output is correct
7 Correct 7 ms 5836 KB Output is correct
8 Correct 7 ms 5836 KB Output is correct
9 Correct 4 ms 5324 KB Output is correct
10 Correct 4 ms 5324 KB Output is correct
11 Correct 424 ms 44228 KB Output is correct
12 Correct 433 ms 46620 KB Output is correct
13 Correct 466 ms 50400 KB Output is correct
14 Correct 611 ms 60472 KB Output is correct
15 Correct 721 ms 64832 KB Output is correct
16 Correct 1171 ms 87792 KB Output is correct
17 Correct 1055 ms 91208 KB Output is correct
18 Correct 1201 ms 87784 KB Output is correct
19 Correct 1041 ms 93660 KB Output is correct
20 Correct 396 ms 45660 KB Output is correct
21 Correct 325 ms 44092 KB Output is correct
22 Correct 1289 ms 92012 KB Output is correct
23 Correct 1254 ms 90076 KB Output is correct
24 Correct 1362 ms 90200 KB Output is correct
25 Correct 1211 ms 99972 KB Output is correct
26 Correct 1234 ms 92616 KB Output is correct
27 Correct 1216 ms 90084 KB Output is correct
28 Correct 79 ms 9412 KB Output is correct
29 Correct 419 ms 46860 KB Output is correct
30 Correct 382 ms 47252 KB Output is correct
31 Correct 418 ms 47876 KB Output is correct
32 Correct 549 ms 56644 KB Output is correct