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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include <bits/stdc++.h>
#define ll long long
#define debug if (0)

struct Query {
  int type; // 1,2,3,4
  int a;
  int b;
  ll c;

  Query(int _type, int _a, int _b, ll _c) : type(_type), a(_a), b(_b), c(_c) {}
};

const int MAX_N = 2e5;
int n, m, q;

std::vector<Query> queries;
bool are_updates;
std::vector<std::pair<int, ll>> list[MAX_N + 3];

/* BRUTE STATE. */
int color[MAX_N + 3];

void brute_add(int u, int v, ll d) {
  list[u].push_back({v, d});
  list[v].push_back({u, d});
}

void brute_remove(int u, int v) {
  int it = 0;
  for (int i = 0; i < list[u].size(); i++)
    if (list[u][i].first == v)
      it = i;
  list[u].erase(list[u].begin() + it);
  for (int i = 0; i < list[v].size(); i++)
    if (list[v][i].first == u)
      it = i;
  list[v].erase(list[v].begin() + it);
}

void dfs_brute_color(int v, int f, int k, ll z, ll d) {
  if (d > z)
    return;
  color[v] = k;
  for (auto &[u, w] : list[v])
    if (u != f)
      dfs_brute_color(u, v, k, z, d + w);
}

void brute_color(int u, int k, ll z) { dfs_brute_color(u, -1, k, z, 0); }

void go_brute() {
  for (int v = 1; v <= n; v++)
    color[v] = 0;

  for (auto &[t, a, b, c] : queries) {
    switch (t) {
    case 1:
      brute_add(a, b, c);
      break;
    case 2:
      brute_remove(a, b);
      break;
    case 3:
      brute_color(a, b, c);
      break;
    case 4:
      std::cout << color[a] << "\n";
    }
  }
}

void input() {
  std::cin >> n >> m >> q;

  for (int i = 1; i <= m; i++) {
    int x, y;
    ll d;
    std::cin >> x >> y >> d;
    list[x].push_back({y, d});
    list[y].push_back({x, d});
  }

  are_updates = false;
  int t, a, b;
  ll c;

  for (int i = 1; i <= q; i++) {
    std::cin >> t;
    switch (t) {
    case 1:
      std::cin >> a >> b >> c;
      break;
    case 2:
      std::cin >> a >> b;
      break;
    case 3:
      std::cin >> a >> c >> b;
      break;
    case 4:
      std::cin >> a;
    }
    if (t == 1 || t == 2)
      are_updates = true;
    queries.emplace_back(t, a, b, c);
  }
}

/* LCA AND DIST STATE. */
int f[20][MAX_N + 3];
ll depth[MAX_N + 3];
int depth2[MAX_N + 3];

void init_dfs1(int v, int father, ll d, int d2) {
  f[0][v] = father;
  depth[v] = d;
  depth2[v] = d2;
  for (auto &[u, w] : list[v])
    if (u != father)
      init_dfs1(u, v, d + w, d2 + 1);
}

void precalc_lca() {
  for (int k = 1; k <= 19; k++)
    for (int v = 1; v <= n; v++)
      f[k][v] = f[k - 1][f[k - 1][v]];
}

int lca(int u, int v) {
  if (depth2[u] < depth2[v])
    std::swap(u, v);
  for (int k = 19; k >= 0 && depth2[u] != depth2[v]; k--)
    if (depth2[f[k][u]] >= depth2[v])
      u = f[k][u];
  if (u == v)
    return u;
  for (int k = 19; k >= 0 && f[0][u] != f[0][v]; k--)
    if (f[k][u] != f[k][v]) {
      u = f[k][u];
      v = f[k][v];
    }
  return f[0][u];
}

ll dist(int u, int v) {
  int w = lca(u, v);
  return depth[u] + depth[v] - 2 * depth[w];
}

/* CENTROIDS STATE. */
std::vector<int> children[MAX_N + 3];
int parent[MAX_N + 3];

bool vis[MAX_N + 3];
int subsize[MAX_N + 3];

int calc_subsize_dfs(int v, int father) {
  subsize[v] = 1;
  for (auto &[u, _] : list[v])
    if (u != father && !vis[u])
      subsize[v] += calc_subsize_dfs(u, v);
  return subsize[v];
}

int find_centroid_dfs(int v, int father, int tsize) {
  for (auto &[u, _] : list[v])
    if (u != father && !vis[u] && subsize[u] > tsize / 2)
      return find_centroid_dfs(u, v, tsize);
  return v;
}

int find_centroid(int v) {
  int tsize = calc_subsize_dfs(v, 0);
  return find_centroid_dfs(v, 0, tsize);
}

int init_centroid(int v) {
  int c = find_centroid(v);
  vis[c] = true;
  for (auto &[u, _] : list[c])
    if (!vis[u]) {
      int cc = init_centroid(u);
      children[c].push_back(cc);
      parent[cc] = c;
    }
  return c;
}

// timestamp, colour, distance
std::vector<std::pair<int, std::pair<int, ll>>> updates[MAX_N + 3];

void update(int v, int time, int k, ll z) {
  int u = v;
  ll d;
  while (u != 0) {
    d = z - dist(u, v);
    if (d >= 0) {
      while (!updates[u].empty() && updates[u].back().second.second <= d)
        updates[u].pop_back();
      updates[u].push_back({time, {k, d}});
    }
    u = parent[u];
  }
}

int query(int v) {
  int u = v;
  int latest_time = -1;
  int k = 0;
  ll d;
  int b, e, mid;

  auto affects = [](int w, int i, ll z) {
    if (i < 0 || i >= updates[w].size())
      return false;
    return updates[w][i].second.second >= z;
  };

  while (u != 0) {
    d = dist(u, v);
    b = 0;
    e = updates[u].size() - 1;

    while (b < e) {
      mid = (b + e + 1) / 2;
      if (affects(u, mid, d))
        b = mid;
      else
        e = mid - 1;
    }

    if (affects(u, b, d) && updates[u][b].first > latest_time) {
      k = updates[u][b].second.first;
      latest_time = updates[u][b].first;
    }

    u = parent[u];
  }

  return k;
}

void go_centroids() {
  init_dfs1(1, 0, 1, 1);

  precalc_lca();

  for (int i = 1; i <= n; i++) {
    vis[i] = false;
    parent[i] = 0;
  }

  int r = init_centroid(1);

  int time = 0;
  for (auto &[t, a, b, c] : queries) {
    switch (t) {
    case 3:
      update(a, time, b, c);
      time++;
      break;
    case 4:
      std::cout << query(a) << "\n";
    }
  }
}

int main() {
  std::ios_base::sync_with_stdio(0);
  std::cin.tie(NULL);

  input();

  if (m == n - 1 && !are_updates)
    go_centroids();
  else
    go_brute();
}