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>
using namespace std;
using ll = long long;
int n, m, q, p[50050], sz[50050], ans[100100];
bool chk[100100], chk2[100100];
stack<array<int, 2>> st;
int find(int a) {
if(a == p[a]) return a;
return find(p[a]);
}
bool merge(int a, int b) {
// cout << a << "+" << b << "\n";
a = find(a), b = find(b);
if(a == b) return false;
if(sz[a] < sz[b]) swap(a, b);
p[b] = a, sz[a] += sz[b];
st.push({a, b});
return true;
}
int main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m;
vector<array<int, 3>> E(m);
for(auto &[z, x, y] : E) cin >> x >> y >> z;
cin >> q;
int s = 1000;
for(int b=0;b<q;b+=s) {
int k = min(b+s, q) - b;
for(int i=1;i<=n;i++) p[i] = i, sz[i] = 1;
while(!st.empty()) st.pop();
vector<array<int, 3>> v, v2;
for(int i=0;i<k;i++) {
int q, x, y; cin >> q >> x >> y;
if(q == 2) v.push_back({y, x, i});
else x--, v2.push_back({i, x, y}), chk[x] = 1;
}
sort(v.rbegin(), v.rend());
vector<array<int, 3>> e;
for(int i=0;i<m;i++) if(!chk[i]) e.push_back(E[i]);
sort(e.rbegin(), e.rend());
int j = 0;
for(auto [y, x, i] : v) {
while(j < e.size() && e[j][0] >= y) merge(e[j][1], e[j][2]), j++;
int r = 0;
while(r < v2.size() && v2[r][0] < i) r++;
// cout << r << "\n";
int cnt = 0;
for(int u=r-1;u>=0;u--) if(!chk2[v2[u][1]]) {
int id = v2[u][1]; chk2[id] = 1;
if(v2[u][2] >= y) cnt += merge(E[id][1], E[id][2]);
}
for(auto [i, id, w] : v2) if(!chk2[id] && E[id][0] >= y) chk2[id] = 1, cnt += merge(E[id][1], E[id][2]);
ans[b+i] = sz[find(x)];
while(cnt--) {
auto [x, y] = st.top(); st.pop();
p[y] = y, sz[x] -= sz[y];
}
for(auto [i, id, w] : v2) chk2[id] = 0;
}
// cout << "reset\n";
for(auto [i, x, y] : v2) E[x][0] = y, chk[x] = 0;
}
// cout << "----------\n";
for(int i=0;i<q;i++) if(ans[i]) cout << ans[i] << "\n";
}
Compilation message (stderr)
bridges.cpp: In function 'int main()':
bridges.cpp:56:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | while(j < e.size() && e[j][0] >= y) merge(e[j][1], e[j][2]), j++;
| ~~^~~~~~~~~~
bridges.cpp:59:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | while(r < v2.size() && v2[r][0] < i) r++;
| ~~^~~~~~~~~~~
# | 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... |