Submission #841814

# Submission time Handle Problem Language Result Execution time Memory
841814 2023-09-02T06:54:30 Z WLZ One-Way Streets (CEOI17_oneway) C++17
100 / 100
146 ms 35052 KB
#include <bits/stdc++.h>
using namespace std;
 
/** 1-indexed LCA class */
class least_common_ancestor {
  private:
    int n, max_log, dfs_cnt = 0;
    vector< vector< pair<int, int> > > g;
    vector< vector<int> > up;
    vector<int> d, dfs_in, dfs_out;
 
    void dfs(int u, int p) {
      dfs_in[u] = dfs_cnt++;
      up[u][0] = p;
      for (int i = 1; i <= max_log; i++) up[u][i] = up[up[u][i - 1]][i - 1];
      for (auto &v : g[u]) {
        if (v.first == u) continue;
        d[v.first] = d[u] + v.second;
        dfs(v.first, u);
      }
      dfs_out[u] = dfs_cnt;
    }
 
    void init() {
      n = (int) g.size() - 1;
      max_log = ceil(log2(n + 1));
      up.assign(n + 1, vector<int>(max_log + 1));
      d.assign(n + 1, 0);
      dfs_in.assign(n + 1, -1);
      dfs_out.resize(n + 1);
      for (int i = 1; i <= n; i++) if (dfs_in[i] == -1) dfs(i, i);
    }
  public:
    /** Assumes g is an undirected tree. Can be directed if edges point away from vertex the root */
    least_common_ancestor(const vector< vector< pair<int, int> > > &_g) : g(_g) {
      init();
    }
 
    /** Assigns weight 1 to all edges in an unweighted tree */
    least_common_ancestor(const vector< vector<int> > &_g) {
      g.assign((int) _g.size(), vector< pair<int, int> >());
      for (int i = 0; i < (int) _g.size(); i++) {
        for (auto &x : _g[i]) g[i].emplace_back(x, 1);
      }
      init();
    }
 
    bool is_anc(int u, int v) {
      return dfs_in[u] <= dfs_in[v] && dfs_out[v] <= dfs_out[u];
    }
 
    int query(int u, int v) {
      if (is_anc(u, v)) return u;
      if (is_anc(v, u)) return v;
      for (int i = max_log; i >= 0; i--) {
        if (!is_anc(up[u][i], v)) u = up[u][i];
      }
      return up[u][0];
    }
 
    int depth(int u) {
      return d[u];
    }
 
    int dist(int u, int v) {
      return d[u] + d[v] - 2 * d[query(u, v)];
    }
 
    int preorder(int u) {
      return dfs_in[u];
    }
 
    int postorder(int u) {
      return dfs_out[u];
    }
};
 
vector< vector< pair<int, int> > > g;
vector< vector<int> > dfs_tree;
vector<bool> was, used;
vector< pair<int, int> > edges, p;
vector<int> up, down, over, back;
string ans;
 
void dfs(int u) {
  was[u] = true;
  for (auto &v : g[u]) {
    if (!was[v.first]) {
      p[v.first] = {u, v.second};
      dfs(v.first);
    } else if (v.second != p[u].second && !used[v.second]) {
      back[u]++; back[v.first]--;
    }
    used[v.second] = true;
  }  
}
 
void dfs_2(int u) {
  over[u] = back[u];
  for (auto &v : dfs_tree[u]) {
    dfs_2(v);
    down[u] += down[v];
    up[u] += up[v];
    over[u] += over[v];
  }
  if (over[u] == 0) {
    assert(down[u] == 0 || up[u] == 0);
    if (down[u] > 0) {
      if (edges[p[u].second].second == u) ans[p[u].second] = 'R';
      else ans[p[u].second] = 'L';
    } else if (up[u] > 0) {
      if (edges[p[u].second].first == u) ans[p[u].second] = 'R';
      else ans[p[u].second] = 'L';
    }
  }
}
 
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  g.resize(n + 1);
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    edges.push_back({u, v});
    g[u].push_back({v, i});
    g[v].push_back({u, i});
  }
  was.assign(n + 1, false);
  p.assign(n + 1, {-1, -1});
  back.assign(n + 1, 0);
  used.assign(m, false);
  for (int i = 1; i <= n; i++) if (!was[i]) dfs(i);
  dfs_tree.resize(n + 1);
  for (int i = 2; i <= n; i++) if (p[i].first != -1) dfs_tree[p[i].first].push_back({i});
  least_common_ancestor lca(dfs_tree);
  up.assign(n + 1, 0); down.assign(n + 1, 0);
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int x, y;
    cin >> x >> y;
    up[x]++; down[y]++;
    int tmp = lca.query(x, y);
    up[tmp]--; down[tmp]--;
  }
  ans = string(m, 'B');
  over.assign(n + 1, 0);
  for (int i = 1; i <= n; i++) if (p[i].first == -1) dfs_2(i);
  for (int i = 0; i < m; i++) cout << ans[i];
  cout << '\n';
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 600 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 600 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Correct 1 ms 856 KB Output is correct
9 Correct 1 ms 600 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 600 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 600 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Correct 1 ms 856 KB Output is correct
9 Correct 1 ms 600 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 32 ms 9844 KB Output is correct
12 Correct 48 ms 13508 KB Output is correct
13 Correct 59 ms 19400 KB Output is correct
14 Correct 81 ms 26820 KB Output is correct
15 Correct 86 ms 28868 KB Output is correct
16 Correct 101 ms 29380 KB Output is correct
17 Correct 99 ms 31728 KB Output is correct
18 Correct 108 ms 29344 KB Output is correct
19 Correct 101 ms 33072 KB Output is correct
20 Correct 64 ms 18116 KB Output is correct
21 Correct 72 ms 17844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 600 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 600 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Correct 1 ms 856 KB Output is correct
9 Correct 1 ms 600 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 32 ms 9844 KB Output is correct
12 Correct 48 ms 13508 KB Output is correct
13 Correct 59 ms 19400 KB Output is correct
14 Correct 81 ms 26820 KB Output is correct
15 Correct 86 ms 28868 KB Output is correct
16 Correct 101 ms 29380 KB Output is correct
17 Correct 99 ms 31728 KB Output is correct
18 Correct 108 ms 29344 KB Output is correct
19 Correct 101 ms 33072 KB Output is correct
20 Correct 64 ms 18116 KB Output is correct
21 Correct 72 ms 17844 KB Output is correct
22 Correct 144 ms 31684 KB Output is correct
23 Correct 121 ms 29912 KB Output is correct
24 Correct 132 ms 29332 KB Output is correct
25 Correct 142 ms 35052 KB Output is correct
26 Correct 146 ms 31160 KB Output is correct
27 Correct 129 ms 29992 KB Output is correct
28 Correct 22 ms 3276 KB Output is correct
29 Correct 80 ms 17740 KB Output is correct
30 Correct 75 ms 17860 KB Output is correct
31 Correct 84 ms 18200 KB Output is correct
32 Correct 88 ms 23492 KB Output is correct