Submission #559840

#TimeUsernameProblemLanguageResultExecution timeMemory
559840GioChkhaidzeOne-Way Streets (CEOI17_oneway)C++14
100 / 100
178 ms33664 KiB
#include <bits/stdc++.h> #define pb push_back #define f first #define s second using namespace std; const int N = 1e5 + 5; char ans[N]; bool f[N], brd[N]; int n, m, q, timer, depth, ts; int a[N], b[N], disc[N], low[N], p[N], sz[N], dep[N], in[N], out[N], W[N][4], Pr[N][20]; vector < int > bridges; vector < pair < int , int > > v[N], g[N]; void dfs(int x, int edg) { f[x] = true; disc[x] = low[x] = ++timer; for (int i = 0; i < v[x].size(); ++i) { int to = v[x][i].f, ed = v[x][i].s; if (ed == edg) continue; if (!f[to]) { dfs(to, ed); low[x] = min(low[x], low[to]); if (low[to] > disc[x]) { brd[ed] = true; bridges.pb(ed); } } else { low[x] = min(low[x], disc[to]); } } } int P(int x) { if (p[x] == x) return x; return p[x] = P(p[x]); } bool is_anc(int a, int b) { return in[a] <= in[b] && out[b] <= out[a]; } int lca(int a, int b) { if (is_anc(a, b)) return a; for (int i = 17; i >= 0; --i) if (!is_anc(Pr[a][i], b)) a = Pr[a][i]; return Pr[a][0]; } void wfs(int x, int p) { f[x] = true; in[x] = ++ts; Pr[x][0] = p; for (int j = 1; j < 18; ++j) { Pr[x][j] = Pr[Pr[x][j - 1]][j - 1]; } for (int i = 0; i < g[x].size(); ++i) { int to = g[x][i].f; if (to != p) wfs(to, x); } out[x] = ts; } void ufs(int x, int edg) { f[x] = true; for (int i = 0; i < g[x].size(); ++i) { int to = g[x][i].f, ed = g[x][i].s; if (ed == edg) continue; ufs(to, ed); W[x][0] += W[to][0]; W[x][1] += W[to][1]; } if (edg != -1){ int A = P(a[edg]), B = P(b[edg]); if (W[x][0] > 0) { if (B == x) ans[edg] = 'L'; else if (A == x) ans[edg] = 'R'; } else if (W[x][1] > 0) { if (B == x) ans[edg] = 'R'; else if (A == x) ans[edg] = 'L'; } } } main () { ios_base::sync_with_stdio(false); cin.tie(NULL), cout.tie(NULL); cin >> n >> m; for (int i = 1; i <= m; ++i) { ans[i] = 'B'; cin >> a[i] >> b[i]; v[a[i]].pb({b[i], i}); v[b[i]].pb({a[i], i}); } for (int i = 1; i <= n; ++i) if (!f[i]) dfs(i, -1); for (int i = 1; i <= n; ++i) p[i] = i, sz[i] = 1, f[i] = 0; for (int i = 1; i <= m; ++i) { if (brd[i]) continue; int A = P(a[i]), B = P(b[i]); if (A == B) continue; if (sz[A] < sz[B]) swap(A, B); sz[A] += sz[B]; p[B] = A; } vector < int > nd; for (int i = 0; i < bridges.size(); ++i) { int id = bridges[i]; int A = P(a[id]); int B = P(b[id]); g[A].pb({B, id}); g[B].pb({A, id}); if (!f[A]) nd.pb(A), f[A] = true; if (!f[B]) nd.pb(B), f[B] = true; } for (int i = 0; i < nd.size(); ++i) f[nd[i]] = false; for (int i = 0; i < nd.size(); ++i) if (!f[nd[i]]) wfs(nd[i], nd[i]); cin >> q; for (int i = 1; i <= q; ++i) { int x, y, z; cin >> x >> y; x = P(x), y = P(y); if (x == y) continue; z = lca(x, y); --W[z][0], ++W[x][0]; --W[z][1], ++W[y][1]; } for (int i = 0; i < nd.size(); ++i) f[nd[i]] = false; for (int i = 0; i < nd.size(); ++i) if (!f[nd[i]]) ufs(nd[i], -1); for (int i = 1; i <= m; ++i) cout << ans[i]; }

Compilation message (stderr)

oneway.cpp: In function 'void dfs(int, int)':
oneway.cpp:21:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   21 |  for (int i = 0; i < v[x].size(); ++i) {
      |                  ~~^~~~~~~~~~~~~
oneway.cpp: In function 'void wfs(int, int)':
oneway.cpp:61:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |  for (int i = 0; i < g[x].size(); ++i) {
      |                  ~~^~~~~~~~~~~~~
oneway.cpp: In function 'void ufs(int, int)':
oneway.cpp:70:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |  for (int i = 0; i < g[x].size(); ++i) {
      |                  ~~^~~~~~~~~~~~~
oneway.cpp: At global scope:
oneway.cpp:93:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   93 | main () {
      | ^~~~
oneway.cpp: In function 'int main()':
oneway.cpp:120:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  120 |  for (int i = 0; i < bridges.size(); ++i) {
      |                  ~~^~~~~~~~~~~~~~~~
oneway.cpp:130:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  130 |  for (int i = 0; i < nd.size(); ++i)
      |                  ~~^~~~~~~~~~~
oneway.cpp:133:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  133 |  for (int i = 0; i < nd.size(); ++i)
      |                  ~~^~~~~~~~~~~
oneway.cpp:147:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  147 |  for (int i = 0; i < nd.size(); ++i)
      |                  ~~^~~~~~~~~~~
oneway.cpp:150:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  150 |  for (int i = 0; i < nd.size(); ++i)
      |                  ~~^~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...