#pragma GCC optimize ("O3") #include <bits/stdc++.h> using namespace std; #define FOR(i, b, e) for(int i = (b); i < (e); i++) #define sz(x) int(x.size()) #define all(x) x.begin(), x.end() #define pb push_back #define mp make_pair #define st first #define nd second using ll = long long; using vi = vector<int>; using pii = pair<int, int>; auto &operator<<(auto &o, pair<auto, auto> p) { return o << "(" << p.st << ", " << p.nd << ")"; } auto operator<<(auto &o, auto x)->decltype(end(x), o) { o << "{"; int i=0; for(auto e: x) o << ", " + 2*!i++ << e; return o << "}"; } #ifdef LOCAL #define deb(x...) cerr << "[" #x "]: ", [](auto...$) { \ ((cerr << $ << "; "),...) << endl; }(x) #else #define deb(...) #endif void usun(vector<pii> &g, int v) { int pos = -1; FOR(i, 0, sz(g)) if(g[i].st == v) pos = i; g.erase(g.begin() + pos); } void dfsB(int v, int par, ll rem, int col, vector<vector<pii>> &G, vi &color) { if(rem < 0) return; color[v] = col; for(auto &[u, d]: G[v]) if(u != par) dfsB(u, v, rem - d, col, G, color); } void brut(vector<vector<pii>> &G, vector<tuple<int, int, ll, int>> &que) { int n = sz(G); vi color(n); for(auto &[t, a, b, c]: que) { if(t == 1) G[a].pb({b, c}), G[b].pb({a, c}); if(t == 2) usun(G[a], b), usun(G[b], a); if(t == 3) dfsB(a, -1, b, c, G, color); if(t == 4) cout << color[a] << '\n'; } } vector<vector<pii>> G; vector<ll> dist[20]; vi rozm, block, par, dpth; vector<vector<tuple<ll, int, int>>> paint; void sz_dfs(int v, int p) { rozm[v] = 1; for(auto &[u, _]: G[v]) if(u != p && !block[u]) { sz_dfs(u, v); rozm[v] += rozm[u]; } } int centroid(int v, int p, int sum) { for(auto &[u, _]: G[v]) if(u != p && !block[u] && rozm[u] > sum / 2) { return centroid(u, v, sum); } return v; } void dist_dfs(int v, int p, int gleb, ll akt) { dist[gleb][v] = akt; for(auto &[u, d]: G[v]) if(u != p && !block[u]) { dist_dfs(u, v, gleb, akt + d); } } void decomp(int v, int p) { sz_dfs(v, -1); v = centroid(v, -1, rozm[v]); par[v] = p; if(p != -1) dpth[v] = dpth[p] + 1; dist_dfs(v, -1, dpth[v], 0); block[v] = 1; for(auto &[u, _]: G[v]) if(!block[u]) decomp(u, v); } void go(int v, ll d, int col, int czas) { for(int p = v; p != -1; p = par[p]) { ll range = d - dist[dpth[p]][v]; if(range < 0) continue; while(sz(paint[p]) > 0 && get<0>(paint[p].back()) <= range) paint[p].pop_back(); paint[p].pb({range, col, czas}); } } int ans(int v) { pii akt = {-1, 0}; for(int p = v; p != -1; p = par[p]) { ll range = dist[dpth[p]][v]; auto it = lower_bound(paint[p].rbegin(), paint[p].rend(), make_tuple(range, -1, -1)); if(it != paint[p].rend()) akt = max(akt, {get<2>(*it), get<1>(*it)}); } return akt.nd; } void cent(vector<tuple<int, int, ll, int>> &que) { int n = sz(G); rozm.resize(n), block.resize(n), par.resize(n), dpth.resize(n); paint.resize(n); FOR(i, 0, 20) dist[i].resize(n); decomp(0, -1); int czas = 0; for(auto &[t, a, b, c]: que) { if(t == 3) go(a, b, c, ++czas); if(t == 4) cout << ans(a) << '\n'; } } void solve() { int n, m, q; cin >> n >> m >> q; G.resize(n); FOR(i, 0, m) { int a, b, c; cin >> a >> b >> c; a--, b--; G[a].pb({b, c}), G[b].pb({a, c}); } vector<tuple<int, int, ll, int>> queries; bool lol = 1; FOR(i, 0, q) { int t; ll a, b, c; cin >> t; if(t == 1) { cin >> a >> b >> c; b--; lol = 0; } if(t == 2) { cin >> a >> b; b--; lol = 0; } if(t == 3) cin >> a >> b >> c; if(t == 4) cin >> a; queries.pb({t, a - 1, b, c}); } if(lol && m == n - 1) cent(queries); else brut(G, queries); } int main() { cin.tie(0)->sync_with_stdio(0); int tt = 1; // cin >> tt; FOR(te, 0, tt) solve(); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | #pragma GCC optimize ("O3") #include <bits/stdc++.h> using namespace std; #define FOR(i, b, e) for(int i = (b); i < (e); i++) #define sz(x) int(x.size()) #define all(x) x.begin(), x.end() #define pb push_back #define mp make_pair #define st first #define nd second using ll = long long; using vi = vector<int>; using pii = pair<int, int>; auto &operator<<(auto &o, pair<auto, auto> p) { return o << "(" << p.st << ", " << p.nd << ")"; } auto operator<<(auto &o, auto x)->decltype(end(x), o) { o << "{"; int i=0; for(auto e: x) o << ", " + 2*!i++ << e; return o << "}"; } #ifdef LOCAL #define deb(x...) cerr << "[" #x "]: ", [](auto...$) { \ ((cerr << $ << "; "),...) << endl; }(x) #else #define deb(...) #endif void usun(vector<pii> &g, int v) { int pos = -1; FOR(i, 0, sz(g)) if(g[i].st == v) pos = i; g.erase(g.begin() + pos); } void dfsB(int v, int par, ll rem, int col, vector<vector<pii>> &G, vi &color) { if(rem < 0) return; color[v] = col; for(auto &[u, d]: G[v]) if(u != par) dfsB(u, v, rem - d, col, G, color); } void brut(vector<vector<pii>> &G, vector<tuple<int, int, ll, int>> &que) { int n = sz(G); vi color(n); for(auto &[t, a, b, c]: que) { if(t == 1) G[a].pb({b, c}), G[b].pb({a, c}); if(t == 2) usun(G[a], b), usun(G[b], a); if(t == 3) dfsB(a, -1, b, c, G, color); if(t == 4) cout << color[a] << '\n'; } } vector<vector<pii>> G; vector<ll> dist[20]; vi rozm, block, par, dpth; vector<vector<tuple<ll, int, int>>> paint; void sz_dfs(int v, int p) { rozm[v] = 1; for(auto &[u, _]: G[v]) if(u != p && !block[u]) { sz_dfs(u, v); rozm[v] += rozm[u]; } } int centroid(int v, int p, int sum) { for(auto &[u, _]: G[v]) if(u != p && !block[u] && rozm[u] > sum / 2) { return centroid(u, v, sum); } return v; } void dist_dfs(int v, int p, int gleb, ll akt) { dist[gleb][v] = akt; for(auto &[u, d]: G[v]) if(u != p && !block[u]) { dist_dfs(u, v, gleb, akt + d); } } void decomp(int v, int p) { sz_dfs(v, -1); v = centroid(v, -1, rozm[v]); par[v] = p; if(p != -1) dpth[v] = dpth[p] + 1; dist_dfs(v, -1, dpth[v], 0); block[v] = 1; for(auto &[u, _]: G[v]) if(!block[u]) decomp(u, v); } void go(int v, ll d, int col, int czas) { for(int p = v; p != -1; p = par[p]) { ll range = d - dist[dpth[p]][v]; if(range < 0) continue; while(sz(paint[p]) > 0 && get<0>(paint[p].back()) <= range) paint[p].pop_back(); paint[p].pb({range, col, czas}); } } int ans(int v) { pii akt = {-1, 0}; for(int p = v; p != -1; p = par[p]) { ll range = dist[dpth[p]][v]; auto it = lower_bound(paint[p].rbegin(), paint[p].rend(), make_tuple(range, -1, -1)); if(it != paint[p].rend()) akt = max(akt, {get<2>(*it), get<1>(*it)}); } return akt.nd; } void cent(vector<tuple<int, int, ll, int>> &que) { int n = sz(G); rozm.resize(n), block.resize(n), par.resize(n), dpth.resize(n); paint.resize(n); FOR(i, 0, 20) dist[i].resize(n); decomp(0, -1); int czas = 0; for(auto &[t, a, b, c]: que) { if(t == 3) go(a, b, c, ++czas); if(t == 4) cout << ans(a) << '\n'; } } void solve() { int n, m, q; cin >> n >> m >> q; G.resize(n); FOR(i, 0, m) { int a, b, c; cin >> a >> b >> c; a--, b--; G[a].pb({b, c}), G[b].pb({a, c}); } vector<tuple<int, int, ll, int>> queries; bool lol = 1; FOR(i, 0, q) { int t; ll a, b, c; cin >> t; if(t == 1) { cin >> a >> b >> c; b--; lol = 0; } if(t == 2) { cin >> a >> b; b--; lol = 0; } if(t == 3) cin >> a >> b >> c; if(t == 4) cin >> a; queries.pb({t, a - 1, b, c}); } if(lol && m == n - 1) cent(queries); else brut(G, queries); } int main() { cin.tie(0)->sync_with_stdio(0); int tt = 1; // cin >> tt; FOR(te, 0, tt) solve(); return 0; } |