이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = array<int, 2>;
#define all(x) begin(x), end(x)
#define sz(x) (int) (x).size()
struct DisjointSet {
vector<int> par, sub;
stack<int> stk;
DisjointSet(int n) : par(n), sub(n) {
iota(all(par), 0);
fill(all(sub), 1);
}
int find(int a) {
return par[a] == a ? a : find(par[a]);
}
void unite(int a, int b) {
a = find(a), b = find(b);
if (a == b) { return; }
if (sub[a] < sub[b]) { swap(a, b); }
sub[a] += sub[b];
par[b] = a;
stk.push(b);
}
void rollback(int x) {
while (sz(stk) > x) {
int cur = stk.top(); stk.pop();
sub[par[cur]] -= sub[cur];
par[cur] = cur;
}
}
inline int edges() { return sz(stk); }
inline int size(int a) { return sub[find(a)]; }
};
constexpr int B = 512;
int main() {
cin.tie(0) -> sync_with_stdio(0);
int n, m;
cin >> n >> m;
vector<int> u(m), v(m), d(m);
for (int i = 0; i < m; i++) {
cin >> u[i] >> v[i] >> d[i];
--u[i], --v[i];
}
int q; cin >> q;
vector<int> t(q), a(q), b(q);
for (int i = 0; i < q; i++) {
cin >> t[i] >> a[i] >> b[i], --a[i];
}
vector<int> res(q, -1);
for (int i = 0; i < q; i += B) {
int j = min(i + B, q);
vector<bool> upd(m);
vector<int> calc, diff;
for (int x = i; x < j; x++) {
if (t[x] == 1) {
upd[a[x]] = true;
diff.push_back(x);
} else {
calc.push_back(x);
}
}
vector<int> fixed;
for (int x = 0; x < m; x++) {
if (!upd[x]) { fixed.push_back(x); }
}
sort(all(fixed), [&](int x, int y) { return d[x] > d[y]; });
sort(all(calc), [&](int x, int y) { return b[x] > b[y]; });
DisjointSet dsu(n);
int ptr = 0;
for (int x : calc) {
while (ptr < sz(fixed) && d[fixed[ptr]] >= b[x]) {
dsu.unite(u[fixed[ptr]], v[fixed[ptr]]);
++ptr;
}
int prv = dsu.edges();
map<pii, int> bef;
for (int y : diff) {
pii cur = {u[a[y]], v[a[y]]};
if (!bef.count(cur) && x < y) {
bef[cur] = d[a[y]];
}
if (y < x) {
bef[cur] = b[y];
}
}
for (const auto [con, wt] : bef) {
if (wt >= b[x]) { dsu.unite(con[0], con[1]); }
}
res[x] = dsu.size(a[x]);
dsu.rollback(prv);
}
for (int x : diff) {
d[a[x]] = b[x];
}
}
for (int x : res) {
if (x == -1) { continue; }
cout << x << "\n";
}
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |