#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; int l[maxn], r[maxn], type[maxn], query_left[maxn], query_right[maxn], query_color[maxn], color[maxn]; long long w[maxn], query_weight[maxn]; int n, m, q; set<pair<int, long long> > g[maxn]; map<pair<int, int>, long long> weights; void dfs1(int u, int p, long long w, int c) { if (w < 0) return; color[u] = c; for (const auto& [ne, W] : g[u]) { if (ne == p) continue; dfs1(ne, u, w - W, c); } } int main() { // freopen("input.txt", "r", stdin); cin >> n >> m >> q; for (int i = 1; i <= m; ++i) { cin >> l[i] >> r[i] >> w[i]; } for (int i = 1; i <= q; ++i) { cin >> type[i]; if (type[i] == 1) { cin >> query_left[i] >> query_right[i] >> query_weight[i]; } else if (type[i] == 2) { cin >> query_left[i] >> query_right[i]; } else if (type[i] == 3) { cin >> query_left[i] >> query_weight[i] >> query_color[i]; } else cin >> query_left[i]; } for (int i = 1; i <= n; ++i) color[i] = 0; for (int i = 1; i <= m; ++i) { g[l[i]].insert({r[i], w[i]}); g[r[i]].insert({l[i], w[i]}); weights[{l[i], r[i]}] = w[i]; weights[{r[i], l[i]}] = w[i]; } for (int i = 1; i <= q; ++i) { if (type[i] == 1) { g[query_left[i]].insert({query_right[i], query_weight[i]}); g[query_right[i]].insert({query_left[i], query_weight[i]}); weights[{query_left[i], query_right[i]}] = query_weight[i]; weights[{query_right[i], query_left[i]}] = query_weight[i]; } else if (type[i] == 2) { long long w = weights[{query_left[i], query_right[i]}]; g[query_left[i]].erase({query_right[i], w}); g[query_right[i]].erase({query_left[i], w}); } else if (type[i] == 3) { dfs1(query_left[i], query_left[i], query_weight[i], query_color[i]); } else cout << color[query_left[i]] << endl; } 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 | #include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; int l[maxn], r[maxn], type[maxn], query_left[maxn], query_right[maxn], query_color[maxn], color[maxn]; long long w[maxn], query_weight[maxn]; int n, m, q; set<pair<int, long long> > g[maxn]; map<pair<int, int>, long long> weights; void dfs1(int u, int p, long long w, int c) { if (w < 0) return; color[u] = c; for (const auto& [ne, W] : g[u]) { if (ne == p) continue; dfs1(ne, u, w - W, c); } } int main() { // freopen("input.txt", "r", stdin); cin >> n >> m >> q; for (int i = 1; i <= m; ++i) { cin >> l[i] >> r[i] >> w[i]; } for (int i = 1; i <= q; ++i) { cin >> type[i]; if (type[i] == 1) { cin >> query_left[i] >> query_right[i] >> query_weight[i]; } else if (type[i] == 2) { cin >> query_left[i] >> query_right[i]; } else if (type[i] == 3) { cin >> query_left[i] >> query_weight[i] >> query_color[i]; } else cin >> query_left[i]; } for (int i = 1; i <= n; ++i) color[i] = 0; for (int i = 1; i <= m; ++i) { g[l[i]].insert({r[i], w[i]}); g[r[i]].insert({l[i], w[i]}); weights[{l[i], r[i]}] = w[i]; weights[{r[i], l[i]}] = w[i]; } for (int i = 1; i <= q; ++i) { if (type[i] == 1) { g[query_left[i]].insert({query_right[i], query_weight[i]}); g[query_right[i]].insert({query_left[i], query_weight[i]}); weights[{query_left[i], query_right[i]}] = query_weight[i]; weights[{query_right[i], query_left[i]}] = query_weight[i]; } else if (type[i] == 2) { long long w = weights[{query_left[i], query_right[i]}]; g[query_left[i]].erase({query_right[i], w}); g[query_right[i]].erase({query_left[i], w}); } else if (type[i] == 3) { dfs1(query_left[i], query_left[i], query_weight[i], query_color[i]); } else cout << color[query_left[i]] << endl; } return 0; } |