56562023-09-04 22:46:13TomaSajtTom és Jerry2 (60)cpp17Elfogadva 60/6043ms14796 KiB
// refactored from mraron's code

#include <bits/stdc++.h>
using namespace std;
const int BIG = 1e9;

vector<vector<array<int, 3>>> g;
vector<int> l, d, comp_id, tom_dist, hole_dist;
int hole, ts = 0, curr_comp_id = 1;
stack<int> st;

// components are separated by cutting vertices
void make_comps(int u, int par_ei) {
  d[u] = l[u] = ++ts;
  int ch = 0;
  for (auto& [v, w, ei] : g[u]) {
    if (par_ei == ei) continue;  // case: tree-edge, from the other direction
    if (d[v] > d[u]) continue;   // case: back-edge, from the other direction
    st.push(ei);
    if (d[v] != 0) {
      // case: (u,v) is back-edge
      l[u] = min(l[u], d[v]);
      continue;
    }
    // case: (u,v) is tree-edge
    ch++;
    make_comps(v, ei);
    l[u] = min(l[u], l[v]);

    if ((ch > 1 && par_ei == -1) || (l[v] >= d[u])) {
      // case: u is a cutting vertex
      while (st.top() != ei) {
        comp_id[st.top()] = curr_comp_id;
        st.pop();
      }
      comp_id[st.top()] = curr_comp_id++;
      st.pop();
    }
  }
}

vector<bool> vis;
vector<int> ans;

// last_safe: for how many timesteps can tom move until we have to leave the current space
void dfs(int u, int par_ei, int idx, int last_safe, int first_comp) {
  vis[u] = 1;

  if (last_safe <= 0) ans[u] = idx;

  for (auto& [v, w, ei] : g[u]) {
    if (hole_dist[v] != hole_dist[u] + 1) continue;  // case: going backwards

    if (u == hole) {
      if (comp_id[ei] != first_comp) {
        continue;
      }
      dfs(v, ei, idx, last_safe, first_comp);
    }
    else {
      if (vis[v]) continue;
      if (comp_id[ei] == comp_id[par_ei]) {
        dfs(v, ei, idx, last_safe - 1, first_comp);  // case: going around component?
      }
      else {
        if (tom_dist[u] <= last_safe)
          dfs(v, ei, u, tom_dist[u] - 1, first_comp);
        else
          dfs(v, ei, idx, last_safe - 1, first_comp);
      }
    }
  }
}

int main() {
  cin.tie(0), ios::sync_with_stdio(0);

  int n, m, tom, j_cnt;
  cin >> n >> m >> tom >> j_cnt >> hole;

  g.resize(n + 1);
  for (int ei = 0; ei < m; ei++) {
    int u, v, w;
    cin >> u >> v >> w;
    g[u].push_back({v, w, ei});
    g[v].push_back({u, w, ei});
  }

  l.resize(n + 1), d.resize(n + 1), comp_id.resize(m + 1);
  make_comps(hole, -1);
  while (!st.empty()) {
    comp_id[st.top()] = curr_comp_id;
    st.pop();
  }
  curr_comp_id++;

  queue<int> q;
  q.push(tom);
  tom_dist.assign(n + 1, BIG);
  tom_dist[tom] = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (auto& [v, w, ei] : g[u]) {
      if (w != 2 || tom_dist[v] != BIG || v == hole) continue;
      tom_dist[v] = tom_dist[u] + 1;
      q.push(v);
    }
  }

  q.push(hole);
  hole_dist.assign(n + 1, BIG);
  hole_dist[hole] = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (auto& [v, w, ei] : g[u]) {
      if (hole_dist[v] != BIG) continue;
      hole_dist[v] = hole_dist[u] + 1;
      q.push(v);
    }
  }

  ans.assign(n + 1, 0);
  vis.resize(n + 1, 0);
  vector<bool> comp_vis(n + 1);
  for (auto& [v, w, ei] : g[hole]) {
    if (comp_vis[comp_id[ei]]) continue;
    dfs(hole, -1, BIG, BIG, comp_id[ei]);
    comp_vis[comp_id[ei]] = 1;
  }

  while (j_cnt--) {
    int x;
    cin >> x;
    cout << ans[x] << '\n';
  }
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base60/60
1Elfogadva0/03ms1832 KiB
2Elfogadva0/09ms4020 KiB
3Elfogadva2/23ms2248 KiB
4Elfogadva2/23ms2464 KiB
5Elfogadva2/23ms2688 KiB
6Elfogadva3/33ms2904 KiB
7Elfogadva2/23ms3252 KiB
8Elfogadva2/23ms3244 KiB
9Elfogadva2/23ms3504 KiB
10Elfogadva3/33ms3824 KiB
11Elfogadva3/33ms3844 KiB
12Elfogadva3/34ms4104 KiB
13Elfogadva3/36ms4792 KiB
14Elfogadva3/38ms5296 KiB
15Elfogadva3/38ms5728 KiB
16Elfogadva3/39ms5768 KiB
17Elfogadva3/39ms7332 KiB
18Elfogadva3/317ms9900 KiB
19Elfogadva4/428ms10864 KiB
20Elfogadva4/443ms14356 KiB
21Elfogadva5/525ms13224 KiB
22Elfogadva5/525ms14796 KiB