unordered_map<int, vector<int> > graph; vector<int> res; int i = 0, j = 0, n, connection_size = connections.size(); int p_stat1, p_stat2;
// 建图 for (i = 0; i < c; i++) { // 编号从0到c-1 graph[i] = vector<int>(); }
for (i = 0; i < connection_size; i++) { p_stat1 = connections[i][0] - 1; // 从0开始编号 p_stat2 = connections[i][1] - 1; graph[p_stat1].push_back(p_stat2); graph[p_stat2].push_back(p_stat1); }
// 填充neighbors_heap int top; vector<bool> visited = vector<bool>(c, false); vector<vector<int> > graph_components; for (i = 0; i < c; i++) { // 深度优先遍历 if (visited[i]) { continue; } stack<int> st; st.push(i); vector<int> curr_graph; while (!st.empty()) { top = st.top(); st.pop(); if (!visited[top]) { visited[top] = true; curr_graph.push_back(top); }
for (int node : graph[top]) { if (!visited[node]) { st.push(node); } } } graph_components.push_back(curr_graph); }
for (vector<int> v : graph_components) { //图的每个连通分量 n = v.size(); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (j == i) { continue; } neighbors_heap[v[i]].push(v[j]); } } }
int query_size = queries.size(); int ops, p_stat; bool flag; for (i = 0; i < query_size; i++) { ops = queries[i][0]; p_stat = queries[i][1] - 1; if (ops == 1) { // maintence if (work_states[p_stat]) { // p_stat在线,自行解决 res.push_back(p_stat + 1); } else { //它的neighbor_heap中最小的 flag = true; while (!neighbors_heap[p_stat].empty()) { top = neighbors_heap[p_stat].top(); if (!to_be_deleted[p_stat].count(top)) { // 不需要删除 res.push_back(top + 1); flag = false; break; } else { neighbors_heap[p_stat].pop(); to_be_deleted[p_stat].erase(top); } } if (flag) { res.push_back(-1); } }
for (auto& q : queries) { int op = q[0], x = q[1]; if (op == 2) { online[x] = false; offlineCounts[x]++; } }
for (int i = 1; i <= c; i++) { int root = dsu.find(i); if (!minimumOnlineStations.count(root)) { minimumOnlineStations[root] = -1; }
int station = minimumOnlineStations[root]; if (online[i]) { if (station == -1 || station > i) { minimumOnlineStations[root] = i; } } }
vector<int> ans;
for (int i = (int)queries.size() - 1; i >= 0; i--) { int op = queries[i][0], x = queries[i][1]; int root = dsu.find(x); int station = minimumOnlineStations[root];
if (op == 1) { if (online[x]) { ans.push_back(x); } else { ans.push_back(station); } }
if (op == 2) { if (offlineCounts[x] > 1) { offlineCounts[x]--; } else { online[x] = true; if (station == -1 || station > x) { minimumOnlineStations[root] = x; } } } }