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
#include<bits/stdc++.h>
using namespace std;

#define int int64_t
#define pb push_back
#define pii pair<int, int>
#define st first
#define nd second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()

const int MAXN = 200'009;

pair<pii, int> E[MAXN];
pair<pii, pii> Q[MAXN];
int n, m, q;

namespace TC1 {
	vector<pii> G[MAXN];
	bool is_removed[MAXN];
	int subtree_size[MAXN];
	int par[MAXN];
	vector<pair<int, pii>> V[MAXN];
	int timer, tin[MAXN], tout[MAXN];
	int jp2[MAXN][20];
	int dep[MAXN];
	void dfs(int v, int p) {
		tin[v] = ++timer;
		jp2[v][0] = p;
		for (int i = 1; i < 20; ++i)
			jp2[v][i] = jp2[jp2[v][i-1]][i-1];

		for (auto [u, c] : G[v]) {
			if (u != p) {
				dep[u] = dep[v]+c;
				dfs(u, v);
			}
		}
		tout[v] = ++timer;
	}
	bool is_ancestor(int u, int v) {
		return tin[u] <= tin[v] && tout[u] >= tout[v];
	}
	int lca(int u, int v) {
		if (is_ancestor(u, v))
			return u;
		if (is_ancestor(v, u))
			return v;
		for (int i = 19; i >= 0; --i) {
			if (!is_ancestor(jp2[u][i], v))
				u = jp2[u][i];
		}
		return jp2[u][0];
	}
	int dis(int u, int v) {
		return dep[u]+dep[v]-2*dep[lca(u, v)];
	}
	int get_subtree_size(int node, int parent = -1) {
		subtree_size[node] = 1;
		for (auto [child, _] : G[node]) {
			if (child == parent || is_removed[child]) { continue; }
			subtree_size[node] += get_subtree_size(child, node);
		}
		return subtree_size[node];
	}
	int get_centroid(int node, int tree_size, int parent = -1) {
		for (auto [child, _] : G[node]) {
			if (child == parent || is_removed[child]) { continue; }
			if (subtree_size[child] * 2 > tree_size) {
				return get_centroid(child, tree_size, node);
			}
		}
		return node;
	}
	void build_centroid_decomp(int node = 1, int p=0) {
		int tree_size = get_subtree_size(node);
		int centroid = get_centroid(node, tree_size);
		par[centroid] = p;
		is_removed[centroid] = true;
		for (auto [child, _] : G[centroid]) {
			if (is_removed[child]) { continue; }
			build_centroid_decomp(child, centroid);
		}
	}
	void insert(int v, pair<int, pii> x) {
		if(x.st<0) return;
		while(sz(V[v])&&V[v].back().st<=x.st) {
			V[v].pop_back();
		}
		V[v].pb(x);
	}
	pii query(int v, int x) {
		int s = -1;
		int e = sz(V[v]);
		while(e-s>1) {
			int m = (e+s)/2;
			if(V[v][m].st>=x) s = m;
			else e = m;
		}
		if(s==-1) return {-1, 0};
		return V[v][s].nd;
	}
	void solve() {
		for(int i=0;i<m;i++) {
			auto [x, c] = E[i];
			G[x.st].pb({x.nd, c});
			G[x.nd].pb({x.st, c});
		}
		build_centroid_decomp();
		dfs(1, 0);
		tout[0] = ++timer;
		for(int i=0;i<q;i++) {
			int type = Q[i].st.st;
			if(type==3) {
				int v = Q[i].st.nd;
				int ran = Q[i].nd.st;
				int col = Q[i].nd.nd; 
				int cur = v;
				while(cur!=0) {
					insert(cur, {ran-dis(v, cur), {i, col}});
					cur = par[cur];
				}
			}
			if(type==4) {
				int v = Q[i].st.nd;
				int cur = v;
				pii ans = {-1, 0};
				while(cur!=0) {
					ans = max(ans, query(cur, dis(v, cur)));
					cur = par[cur];
				}
				cout << ans.nd << "\n";
			}
		}
	}
}

namespace BRUT {
	set<pii> G[MAXN];
	map<pii, int> M;
	int col[MAXN];
	void dfs(int x, int ran, int c, int p=0) {
		col[x] = c;
		for(pii y:G[x]) {
			if(y.st!=p&&ran>=y.nd) {
				dfs(y.st, ran-y.nd, c, x);
			}
		}
	}
	void solve() {
		for(int i=0;i<m;i++) {
			auto [x, c] = E[i];
			G[x.st].insert({x.nd, c});
			G[x.nd].insert({x.st, c});
			M[minmax(x.st, x.nd)] = c;
		}
		for(int i=0;i<q;i++) {
			int type = Q[i].st.st;
			if(type==1) {
				int a = Q[i].st.nd, b = Q[i].nd.st, c = Q[i].nd.nd;
				G[a].insert({b, c});
				G[b].insert({a, c});
				M[minmax(a, b)] = c;
			}
			if(type==2) {
				int a = Q[i].st.nd, b = Q[i].nd.st;
				int c = M[minmax(a, b)];
				G[a].erase({b, c});
				G[b].erase({a, c});
			}
			if(type==3) {
				int v = Q[i].st.nd, ran = Q[i].nd.st, c = Q[i].nd.nd;
				dfs(v, ran, c);
			}
			if(type==4) {
				int v = Q[i].st.nd;
				cout << col[v] << "\n";
			}
		}
	}
}

int32_t main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	
	cin >> n >> m >> q;
	bool tc1 = true;
	if(m!=n-1) tc1 = false;
	for(int i=0;i<m;i++) {
		int a, b, c;
		cin >> a >> b >> c;
		E[i]={{a, b}, c};
	}
	for(int i=0;i<q;i++) {
		int type;
		cin >> type;
		if(type<3) tc1 = false;
		if(type==1) {
			int a, b, c;
			cin >> a >> b >> c;
			Q[i] = {{type, a}, {b, c}};
		}
		if(type==2) {
			int a, b;
			cin >> a >> b;
			Q[i] = {{type, a}, {b, 0}};
		}
		if(type==3) {
			int a, b, c;
			cin >> a >> b >> c;
			Q[i] = {{type, a}, {b, c}};
		}
		if(type==4) {
			int a;
			cin >> a;
			Q[i] = {{type, a}, {0, 0}};
		}
	}
	if(tc1) {
		TC1::solve();
		return 0;
	}
	BRUT::solve();
}