Submission #679840

# Submission time Handle Problem Language Result Execution time Memory
679840 2023-01-09T12:09:25 Z peijar One-Way Streets (CEOI17_oneway) C++17
100 / 100
171 ms 26516 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;

namespace std {
template <typename T> ostream &operator<<(ostream &out, const vector<T> &vec) {
  out << "[";
  for (int i = 0; i < (int)vec.size(); ++i) {
    out << vec[i];
    if (i + 1 < (int)vec.size())
      out << ", ";
  }
  return out << "]";
}
} // namespace std

void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
  cout << ' ' << H;
  dbg_out(T...);
}

#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

const int MAXN = 1e5;

struct UnionFind {
  vector<int> id;

  UnionFind() {}
  UnionFind(int N) { id.assign(N, -1); }

  int find(int u) {
    if (id[u] < 0)
      return u;
    return id[u] = find(id[u]);
  }

  bool merge(int u, int v) {
    u = find(u), v = find(v);
    if (u == v)
      return false;
    if (id[u] > id[v])
      swap(u, v);
    id[u] += id[v];
    id[v] = u;
    return true;
  }
};

vector<pair<int, int>> adj[MAXN];
vector<pair<int, int>> edges;
vector<int> num, st;
int Time;
vector<int> bridges;
bool isBridge[MAXN];

int dfs(int at, int par) {
  int me = num[at] = ++Time, e, y, top = me;

  for (auto pa : adj[at])
    if (pa.second != par) {
      tie(y, e) = pa;
      if (num[y])
        top = min(top, num[y]);
      else {
        int up = dfs(y, e);
        top = min(top, up);
        if (up == me) {
        } else if (up < me) {
        } else {
          bridges.push_back(e);
          isBridge[e] = true;
        }
      }
    }
  return top;
}

void bicomps(int nbSommet) {
  num.assign(nbSommet, 0);
  for (int i = 0; i < nbSommet; ++i)
    if (!num[i])
      dfs(i, -1);
}

signed main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int nbSommet, nbAretes, nbRequetes;
  cin >> nbSommet >> nbAretes;
  edges.resize(nbAretes);
  for (int i = 0; i < nbAretes; ++i) {
    auto &[u, v] = edges[i];
    cin >> u >> v;
    --u, --v;
    assert(u != v);
    adj[u].emplace_back(v, i);
    adj[v].emplace_back(u, i);
  }

  cin >> nbRequetes;
  vector<pair<int, int>> queries(nbRequetes);
  for (auto &[u, v] : queries) {
    cin >> u >> v;
    --u, --v;
  }

  bicomps(nbSommet);

  UnionFind dsu(nbSommet);
  for (int i = 0; i < nbAretes; ++i)
    if (!isBridge[i])
      dsu.merge(edges[i].first, edges[i].second);
  vector<int> roots;
  for (int u = 0; u < nbSommet; ++u)
    if (dsu.find(u) == u)
      roots.push_back(u);

  auto getId = [&](int u) {
    return lower_bound(roots.begin(), roots.end(), dsu.find(u)) - roots.begin();
  };
  for (int u = 0; u < nbSommet; ++u)
    adj[u].clear();
  for (int iArete = 0; iArete < nbAretes; ++iArete) {
    if (isBridge[iArete]) {
      auto [u, v] = edges[iArete];
      u = getId(u);
      v = getId(v);
      if (u == v)
        continue;
      adj[u].emplace_back(v, iArete);
      adj[v].emplace_back(u, iArete);
    }
  }
  int nbRoots = roots.size();
  vector<int> cntDeb(nbRoots), cntFin(nbRoots);
  for (auto [u, v] : queries) {
    u = getId(u);
    v = getId(v);
    if (u == v)
      continue;
    cntDeb[u]++;
    cntFin[v]++;
  }

  vector<int> side(nbAretes);
  vector<bool> seen(nbRoots);

  auto dfsBridge = [&](auto rec, int u, int p) -> void {
    seen[u] = true;
    for (auto [v, id] : adj[u])
      if (v != p) {
        rec(rec, v, u);
        cntDeb[u] += cntDeb[v];
        cntFin[u] += cntFin[v];
        if (cntDeb[v] > cntFin[v]) {
          side[id] = getId(edges[id].first) == u ? -1 : 1;
        }
        if (cntFin[v] > cntDeb[v])
          side[id] = getId(edges[id].first) == u ? 1 : -1;
      }
  };

  for (int u = 0; u < nbRoots; ++u)
    if (!seen[u])
      dfsBridge(dfsBridge, u, u);
  for (int x : side) {
    if (x == 0)
      cout << 'B';
    else if (x == -1)
      cout << 'L';
    else
      cout << 'R';
  }
  cout << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2772 KB Output is correct
4 Correct 2 ms 2772 KB Output is correct
5 Correct 2 ms 2772 KB Output is correct
6 Correct 2 ms 2772 KB Output is correct
7 Correct 2 ms 2772 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 2 ms 2772 KB Output is correct
10 Correct 2 ms 2772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2772 KB Output is correct
4 Correct 2 ms 2772 KB Output is correct
5 Correct 2 ms 2772 KB Output is correct
6 Correct 2 ms 2772 KB Output is correct
7 Correct 2 ms 2772 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 2 ms 2772 KB Output is correct
10 Correct 2 ms 2772 KB Output is correct
11 Correct 39 ms 12624 KB Output is correct
12 Correct 39 ms 13708 KB Output is correct
13 Correct 47 ms 14972 KB Output is correct
14 Correct 58 ms 16328 KB Output is correct
15 Correct 65 ms 16824 KB Output is correct
16 Correct 89 ms 16336 KB Output is correct
17 Correct 82 ms 18980 KB Output is correct
18 Correct 85 ms 16324 KB Output is correct
19 Correct 80 ms 20696 KB Output is correct
20 Correct 39 ms 12964 KB Output is correct
21 Correct 36 ms 12760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2772 KB Output is correct
4 Correct 2 ms 2772 KB Output is correct
5 Correct 2 ms 2772 KB Output is correct
6 Correct 2 ms 2772 KB Output is correct
7 Correct 2 ms 2772 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 2 ms 2772 KB Output is correct
10 Correct 2 ms 2772 KB Output is correct
11 Correct 39 ms 12624 KB Output is correct
12 Correct 39 ms 13708 KB Output is correct
13 Correct 47 ms 14972 KB Output is correct
14 Correct 58 ms 16328 KB Output is correct
15 Correct 65 ms 16824 KB Output is correct
16 Correct 89 ms 16336 KB Output is correct
17 Correct 82 ms 18980 KB Output is correct
18 Correct 85 ms 16324 KB Output is correct
19 Correct 80 ms 20696 KB Output is correct
20 Correct 39 ms 12964 KB Output is correct
21 Correct 36 ms 12760 KB Output is correct
22 Correct 140 ms 21712 KB Output is correct
23 Correct 141 ms 19044 KB Output is correct
24 Correct 171 ms 18968 KB Output is correct
25 Correct 130 ms 26516 KB Output is correct
26 Correct 130 ms 20944 KB Output is correct
27 Correct 132 ms 19308 KB Output is correct
28 Correct 30 ms 11984 KB Output is correct
29 Correct 56 ms 15352 KB Output is correct
30 Correct 60 ms 15580 KB Output is correct
31 Correct 57 ms 15820 KB Output is correct
32 Correct 92 ms 18668 KB Output is correct