56552023-09-04 22:04:20TomaSajtTom és Jerry2 (60)cpp17Hibás válasz 20/6046ms15276 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;
    if (d[v] == 0) {
      // case: (u,v) is tree-edge
      ch++;
      st.push(ei);
      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();
      }
    }
    else {
      // case: (u,v) is back-edge
      l[u] = min(l[u], d[v]);
      st.push(ei);
    }
  }
}

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

void dfs(int u, int par_ei, int idx, int tav, int ac) {
  vis[u] = 1;
  if (tav <= 0) {
    ans[u] = idx;
  }
  for (auto& [v, w, ei] : g[u]) {
    if (hole_dist[v] != hole_dist[u] + 1) continue;
    if (u == hole) {
      if (comp_id[ei] != ac) {
        continue;
      }
      dfs(v, ei, idx, tav, ac);
    }
    else {
      if (!vis[v]) {
        if (comp_id[ei] == comp_id[par_ei]) {
          dfs(v, ei, idx, tav - 1, ac);
        }
        else {
          if (tom_dist[u] <= tav)
            dfs(v, ei, u, tom_dist[u] - 1, ac);
          else
            dfs(v, ei, idx, tav - 1, ac);
        }
      }
    }
  }
}

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

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

  g.assign(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.assign(n + 1, 0);
  d.assign(n + 1, 0);
  comp_id.assign(m + 1, 0);
  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.assign(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
base20/60
1Elfogadva0/03ms1824 KiB
2Hibás válasz0/09ms4076 KiB
3Elfogadva2/23ms2424 KiB
4Elfogadva2/22ms2420 KiB
5Hibás válasz0/23ms2432 KiB
6Hibás válasz0/33ms2640 KiB
7Hibás válasz0/23ms3000 KiB
8Hibás válasz0/23ms3268 KiB
9Hibás válasz0/23ms3200 KiB
10Hibás válasz0/33ms3360 KiB
11Hibás válasz0/33ms3580 KiB
12Hibás válasz0/34ms4036 KiB
13Hibás válasz0/36ms5104 KiB
14Hibás válasz0/38ms5484 KiB
15Hibás válasz0/38ms5852 KiB
16Hibás válasz0/39ms6292 KiB
17Elfogadva3/39ms7800 KiB
18Elfogadva3/317ms10336 KiB
19Hibás válasz0/430ms11624 KiB
20Hibás válasz0/446ms15128 KiB
21Elfogadva5/526ms13808 KiB
22Elfogadva5/525ms15276 KiB