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
#include <vector>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <iostream>

struct Neighbour {
	int id;
	int d;
};

struct Node {
	int k = 0;
	int queryId = 0;
	std::vector<Neighbour> neighbours;
};

struct EdgeInput {
	int a;
	int b;
	int d;
};

struct Query {
	int type;
	int arg1;
	long long arg2;
	int arg3;	
};

struct Groups {
	void addNodes(int a, int b) {
		auto itA = nodeIdToGroupId.find(a);
		auto itB = nodeIdToGroupId.find(b);
		if (itA == nodeIdToGroupId.end() && itB == nodeIdToGroupId.end()) {
			maxGroupId++;
			nodeIdToGroupId[a] = maxGroupId;
			nodeIdToGroupId[b] = maxGroupId;
			groupIdToNodes[maxGroupId].insert(a);
			groupIdToNodes[maxGroupId].insert(b);
		} else if (itA != nodeIdToGroupId.end() && itB == nodeIdToGroupId.end()) {
			nodeIdToGroupId[b] = itA->second;
			groupIdToNodes[itA->second].insert(b);
		} else if (itA == nodeIdToGroupId.end() && itB != nodeIdToGroupId.end()) {
			nodeIdToGroupId[a] = itB->second;
			groupIdToNodes[itB->second].insert(a);
		} else {
			int groupA = itA->second;
			int groupB = itB->second;
			if (groupIdToNodes[groupA].size() >= groupIdToNodes[groupB].size()) {
				for (auto nodeId : groupIdToNodes[groupB]) {
					nodeIdToGroupId[nodeId] = groupA;
				}
				groupIdToNodes[groupA].insert(groupIdToNodes[groupB].begin(), groupIdToNodes[groupB].end());
				groupIdToNodes.erase(groupB);
			} else {
				for (auto nodeId : groupIdToNodes[groupA]) {
					nodeIdToGroupId[nodeId] = groupB;
				}
				groupIdToNodes[groupB].insert(groupIdToNodes[groupA].begin(), groupIdToNodes[groupA].end());
				groupIdToNodes.erase(groupA);
			}
		}
	}
	
	bool setColor(int v, long long z, int k, int queryId) {
		if (singleBigGroup) {
			if (z >= 200000000000000LL) {
				queryIdForColorForSingleBigGroup = queryId;
				colorForSingleBigGroup = k;
				return true;
			}			
		}
		return false;
	}
	
	int getColor(int u, int queryIdLastNodeUpdate) {
		int result{0};
		if (singleBigGroup && colorForSingleBigGroup != 0 && queryIdForColorForSingleBigGroup > queryIdLastNodeUpdate) {
			result = colorForSingleBigGroup;
		}
		return result;
	}
	bool singleBigGroup = false;
	int colorForSingleBigGroup = 0;
	int queryIdForColorForSingleBigGroup = 0;
	int maxGroupId = 0;
	std::unordered_map<int, int> nodeIdToGroupId;
	std::unordered_map<int, std::unordered_set<int>> groupIdToNodes;
	std::unordered_map<int, int> groupIdToColor;
};

class Solution {
public:
	Solution(int aN, int aM, int aQ)
		: n(aN)
		, m(aM)
		, q(aQ)
		, queriesType1Type2Exist(false)
		, allQueriesType3TheSame(true)
		, nodes(n + 1)
	{}
	
	void run() {
		getInput();
		configureGroups();
		processQueries();
	}
	
	void getInput() {
		edgesInput.reserve(m);
		for (int i = 0; i < m; i++) {
			EdgeInput edge;
			std::cin >> edge.a >> edge.b >> edge.d;
			edgesInput.push_back(edge);
			nodes[edge.a].neighbours.push_back({edge.b, edge.d});
			nodes[edge.b].neighbours.push_back({edge.a, edge.d});
			//groups.addNodes(a, b);
		}
		
		queries.reserve(q);
		for (int i = 0; i < q; i++) {
			Query query;
			std::cin >> query.type;
			if (query.type == 1) {
				queriesType1Type2Exist = true;
				std::cin >> query.arg1 >> query.arg2 >> query.arg3;
			} else if (query.type == 2) {
				queriesType1Type2Exist = true;
				std::cin >> query.arg1 >> query.arg2;
			} else if (query.type == 3) {
				std::cin >> query.arg1 >> query.arg2 >> query.arg3;
				if (query.arg2 != 1000000000000000LL) {
					allQueriesType3TheSame = false;
				}
			} else if (query.type == 4) {
				std::cin >> query.arg1;
			}
			queries.push_back(query);
		}
	}
	
	void configureGroups() {
		if (!queriesType1Type2Exist) {
			groups.singleBigGroup = true;
		}/* else {
			for (auto & edge : edgesInput) {
				groups.addNodes(edge.a, edge.b);
			}
		}*/
	}
	
	void processQueries() {
		for (int i = 0; i < q; i++) {
			Query & query = queries[i];
			int queryId = i + 1;
			int & a = query.arg1;
			int b = static_cast<int>(query.arg2);
			int & d = query.arg3;
			int & v = query.arg1;
			long long & z = query.arg2;
			int & k = query.arg3;
			int & u = query.arg1;
			if (query.type == 1) {
				nodes[a].neighbours.push_back({b, d});
				nodes[b].neighbours.push_back({a, d});
				/*groups.addNodes(a, b);*/
			} else if (query.type == 2) {
				deleteEdge(a, b);
			} else if (query.type == 3) {
				addColor(v, z, k, queryId);
			} else if (query.type == 4) {
				int color = getColor(u);
				std::cout << color << "\n";
			}
		}
	}
	
	void deleteEdge(int a, int b) {
		auto itA = std::find_if(nodes[a].neighbours.begin(), nodes[a].neighbours.end(), [&](auto & neighbour) {
			return neighbour.id == b;
		});
		nodes[a].neighbours.erase(itA);
		auto itB = std::find_if(nodes[b].neighbours.begin(), nodes[b].neighbours.end(), [&](auto & neighbour) {
			return neighbour.id == a;
		});
		nodes[b].neighbours.erase(itB);
	}
	
	void addColor(int v, long long z, int k, int queryId) {
		if (!groups.setColor(v, z, k, queryId)) {
			std::deque<std::pair<int, long long>> nodesToProcess;
			nodesToProcess.push_back(std::pair<int, long long>(v, z));
			while (!nodesToProcess.empty()) {
				int currentNodeId = nodesToProcess.front().first;
				long long distance = nodesToProcess.front().second;
				nodesToProcess.pop_front();
				nodes[currentNodeId].k = k;
				nodes[currentNodeId].queryId = queryId;
				for (auto & adj : nodes[currentNodeId].neighbours) {
					if (adj.d <= distance) {
						nodesToProcess.push_back(std::pair<int, long long>(adj.id, distance - adj.d));
					}
				}
			}
		}
	}
	
	int getColor(int u) {
		int color = groups.getColor(u, nodes[u].queryId);
		return color != 0 ? color : nodes[u].k;
	}
	
private:
	int n;
	int m;
	int q;
	bool queriesType1Type2Exist;
	bool allQueriesType3TheSame;
	std::vector<Node> nodes;
	std::vector<EdgeInput> edgesInput;
	std::vector<Query> queries;
	Groups groups;
};

int main() {
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(NULL);
	
	int n, m, q;
	std::cin >> n >> m >> q;
	Solution solution(n, m, q);
	solution.run();
	
	return 0;
}