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>
#define all(vec) vec.begin(), vec.end()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
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], par[50000], siz[50000];
struct UnionFind {
void build(int n) {
for (int i = 0; i < n; i++) {
par[i] = i, siz[i] = 1;
}
}
inline int find(int x) {
if (par[x] == x) {
return x;
} else {
return par[x] = find(par[x]);
}
}
inline bool unite(int u, int v) {
u = find(u), v = find(v);
if (u == v)
return false;
if (siz[u] < siz[v])
swap(u, v);
par[v] = u;
siz[u] += siz[v];
return true;
}
bool same(int u, int v) { return find(u) == find(v); }
};
vector<int> G[50000];
int ok[50000];
vector<int> vv;
void dfs(int i) {
ok[i] = 1;
vv.emplace_back(i);
for (auto &e : G[i]) {
if (ok[e]) {
continue;
}
dfs(e);
}
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
const int b = 500;
int n, m;
cin >> n >> m;
set<pair<P, P>, greater<pair<P, P>>> es;
for (int i = 0; i < m; i++) {
cin >> u[i] >> v[i] >> d[i];
--u[i];
--v[i];
es.insert(make_pair(P(d[i], i), P(u[i], v[i])));
}
int q;
cin >> q;
vector<int> t(q), s(q), w(q), vis(m), res(q, -1), tmp(m, -1);
vector<pair<P, int>> qs;
UnionFind uf;
for (int i = 0; i < q; i++) {
cin >> t[i] >> s[i] >> w[i];
--s[i];
vector<int> vs;
if (t[i] == 1) {
vis[s[i]] = 1;
} else {
qs.emplace_back(make_pair(P(w[i], s[i]), i));
}
if (i % b < b - 1 && i != q - 1) {
continue;
}
uf.build(n);
sort(all(qs));
reverse(all(qs));
int fs = (i / b) * b;
auto it = es.begin();
for (int j = 0; j < qs.size(); j++) {
int ww = qs[j].first.first, st = qs[j].first.second,
idx = qs[j].second;
while (it != es.end() && (*it).first.first >= ww) {
if (!vis[(*it).first.second]) {
uf.unite((*it).second.first, (*it).second.second);
}
it++;
}
for (int k = fs; k < idx; k++) {
if (t[k] == 1) {
tmp[s[k]] = w[k];
}
}
for (int k = fs; k <= i; k++) {
if (t[k] == 1) {
if ((k < idx && tmp[s[k]] >= ww) ||
(k > idx &&
(tmp[s[k]] == -1 ? d[s[k]] : tmp[s[k]]) >= ww)) {
int uu = uf.find(u[s[k]]), vv = uf.find(v[s[k]]);
G[uu].emplace_back(uf.find(vv));
G[vv].emplace_back(uf.find(uu));
}
}
}
for (int k = fs; k < idx; k++) {
if (t[k] == 1) {
tmp[s[k]] = -1;
}
}
dfs(uf.find(st));
res[idx] = 0;
for (auto &e : vv) {
res[idx] += siz[uf.find(e)];
ok[uf.find(e)] = 0;
}
for (int k = fs; k <= i; k++) {
if (t[k] == 1) {
int uu = uf.find(u[s[k]]), vv = uf.find(v[s[k]]);
if (!G[uu].empty()) {
G[uu].clear();
}
if (!G[vv].empty()) {
G[vv].clear();
}
}
}
vv.clear();
}
for (int j = i; j >= fs; j--) {
if (t[j] == 1) {
if (vis[s[j]]) {
es.erase(make_pair(P(d[s[j]], s[j]), P(u[s[j]], v[s[j]])));
d[s[j]] = w[j];
es.insert(make_pair(P(d[s[j]], s[j]), P(u[s[j]], v[s[j]])));
vis[s[j]] = 0;
}
}
}
qs.clear();
}
for (int i = 0; i < q; i++) {
if (res[i] != -1) {
cout << res[i] << '\n';
}
}
}
Compilation message (stderr)
bridges.cpp: In function 'int main()':
bridges.cpp:85:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < qs.size(); j++) {
~~^~~~~~~~~~~
# | 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... |