This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#pragma GCC target("avx2")
#define all(vec) vec.begin(), vec.end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
constexpr ll INF = (1LL << 30) - 1LL;
constexpr ll LINF = (1LL << 60) - 1LL;
constexpr ll MOD = 1e9 + 7;
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chmax(T &a, T b) { a = max(a, b); }
int u[100000], v[100000], d[100000], dat[50000];
int t[100000], s[100000], w[100000], vis[100000], res[100000], tmp[100000];
P es[100000];
struct UnionFind {
stack<P> his;
inline void build(const int &n) { memset(dat, -1, sizeof(dat)); }
inline int find(const int &x) {
if (dat[x] < 0) {
return x;
} else {
return find(dat[x]);
}
}
inline bool unite(int u, int v, const int &f) {
u = find(u), v = find(v);
if (u == v)
return false;
if (dat[u] > dat[v])
swap(u, v);
if (f) {
his.emplace(u, dat[u]);
his.emplace(v, dat[v]);
}
dat[u] += dat[v];
dat[v] = u;
return true;
}
// bool same(int u, int v) { return find(u) == find(v); }
inline void rollback() {
while (!his.empty()) {
dat[his.top().first] = his.top().second;
his.pop();
dat[his.top().first] = his.top().second;
his.pop();
}
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
const int b = 800;
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> u[i] >> v[i] >> d[i];
--u[i];
--v[i];
}
int q;
cin >> q;
memset(tmp, -1, sizeof(tmp));
priority_queue<pair<P, int>> qs;
UnionFind uf;
for (int i = 0; i < q; i++) {
cin >> t[i] >> s[i] >> w[i];
--s[i];
}
for (int i = 0; i < q; i++) {
if (t[i] == 1) {
vis[s[i]] = 1;
} else {
qs.push(make_pair(P(w[i], s[i]), i));
}
if (i % b < b - 1 && i != q - 1) {
continue;
}
for (int j = 0; j < m; j++) {
es[j] = P(d[j], j);
}
sort(es, es + m, greater<P>());
int id = 0;
uf.build(n);
int fs = (i / b) * b;
int ww, st, idx;
while (!qs.empty()) {
ww = qs.top().first.first, st = qs.top().first.second,
idx = qs.top().second;
qs.pop();
while (id < m && es[id].first >= ww) {
if (!vis[es[id].second]) {
uf.unite(u[es[id].second], v[es[id].second], 0);
}
id++;
}
for (int k = idx - 1; k >= fs; k--) {
if (t[k] == 1) {
if (tmp[s[k]] == -1) {
tmp[s[k]] = w[k];
if (w[k] >= ww) {
uf.unite(u[s[k]], v[s[k]], 1);
}
}
}
}
for (int k = idx + 1; k <= i; k++) {
if (t[k] == 1 &&
(tmp[s[k]] == -1 ? d[s[k]] : tmp[s[k]]) >= ww) {
uf.unite(u[s[k]], v[s[k]], 1);
}
}
for (int k = fs; k < idx; k++) {
if (t[k] == 1) {
tmp[s[k]] = -1;
}
}
res[idx] = -dat[uf.find(st)];
uf.rollback();
}
for (int j = i; j >= fs; j--) {
if (t[j] == 1 && vis[s[j]]) {
d[s[j]] = w[j];
vis[s[j]] = 0;
}
}
}
for (int i = 0; i < q; i++) {
if (t[i] == 2) {
cout << res[i] << '\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... |