#include <bits/stdc++.h>
#include "collapse.h"
using namespace std;
#define FOR(i, a, b) for(int i = a; i <= (int)b; i++)
#define FORD(i, a, b) for(int i = a; i >= (int)b; i--)
#define all(a) a.begin(), a.end()
#define pb push_back
#define fi first
#define se second
const int maxN = 3e5 + 5;
int n, m, q;
array <int, 3> edges[maxN];
array <int, 2> Q[maxN];
// void init() {
// cin >> n >> m >> q;
// FOR(i, 1, m) {
// cin >> edges[i][0] >> edges[i][1] >> edges[i][2];
// if (edges[i][1] > edges[i][2]) swap(edges[i][1], edges[i][2]);
// }
// FOR(i, 1, q) {
// cin >> Q[i][0] >> Q[i][1];
// }
// }
namespace sub1 {
bool checksub() {
return max(n, q) <= 5000;
}
int lab[maxN];
int ssc;
int find (int x) {
return lab[x] < 0 ? x : lab[x] = find(lab[x]);
}
bool joint (int u, int v) {
int x = find(u), y = find(v);
if (x == y) return 0;
if (lab[x] > lab[y]) swap(x, y);
lab[x]+= lab[y];
lab[y] = x;
ssc--;
return 1;
}
vector <int> solve() {
vector <int> res;
FOR(_q, 1, q) {
FOR(i, 1, n) lab[i] = - 1;
ssc = n;
set <pair <int, int>> edge_query;
FOR(i, 1, Q[_q][0] + 1) {
if (edges[i][0] == 0) {
if (edge_query.find(make_pair(edges[i][0], edges[i][1])) != edge_query.end()) {
edge_query.erase(edge_query.find(make_pair(edges[i][0], edges[i][1])));
}
} else {
if (edges[i][1] <= Q[_q][1] && edges[i][2] >= Q[_q][1] + 1) {
continue;
}
edge_query.insert(make_pair(edges[i][1], edges[i][2]));
}
}
for (auto &it : edge_query) joint(it.first, it.second);
res.push_back(ssc);
}
return res;
}
}
vector <int> simulateCollapse(int N, vector<int> T, vector<int> X, vector<int> Y, vector<int> W, vector<int> P) {
n = N;
m = X.size();
q = W.size();
FOR(i, 0, m - 1) {
edges[i + 1][0] = T[i];
edges[i + 1][1] = X[i];
edges[i + 1][2] = Y[i];
if (edges[i + 1][1] > edges[i + 1][2]) swap(edges[i + 1][1], edges[i + 1][2]);
}
for (int i = 0; i < q; i++) {
Q[i + 1][0] = W[i];
Q[i + 1][1] = P[i];
}
vector <int> res = sub1::solve();
return res;
}
void process() {
if(sub1::checksub()) sub1::solve();
}
// signed main() {
// ios_base::sync_with_stdio(0);
// cin.tie(0); cout.tie(0);
// #define ko "kieuoanh"
// if (fopen(ko".inp", "r")) {
// freopen(ko".inp", "r", stdin);
// freopen(ko".out", "w", stdout);
// }
// vector <int> res = simulateCollapse(4, {1, 1, 1, 1}, {1, 2, 3, 2}, {2, 3, 4, 4}, {2, 0}, {3, 3});
// return 0;
// }
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |