This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
#define ff first
#define ss second
const int N = 1e6+100, M = 1e5+10, K = 52, MX = 30;
struct Dsu {
vector<int> s, p;
vector<pair<int,int>> poop;
int sz;
Dsu(int n){
sz = n;
s.resize(n+1, 1);
p.resize(n+1);
for(int i = 0; i <= n; ++i) p[i] = i;
}
int find(int v){
if(p[v] == v) return v;
return find(p[v]);
}
void merge(int a, int b){
a = find(a);
b = find(b);
if(a != b){
if(s[a] > s[b]){
swap(a, b);
}
s[b] += s[a];
p[a] = b;
poop.pb({a, b});
}else{
poop.pb({-1, -1
});
}
}
void rollback(){
if(poop.size() == 0) return;
if(poop.back().ff == -1){
poop.pop_back();
return;
}
int a = poop.back().ff;
int b = poop.back().ss;
poop.pop_back();
p[a] = a;
s[b] -= s[a];
}
};
int n, m, q, ans[N];
vector<array<int, 4>> edges;
array<int, 3> upd[N];
array<int, 4> qr[N];
set<array<int , 4>> SE;
int S = 1100;
void solve(){
cin >> n >> m;
for(int i = 0; i < m; ++i){
int u, v, w; cin >> u >> v >> w;
edges.pb({-w, v, u, i});
SE.insert({-w, v, u, i});
}
// sort(all(edges), greater<array<int, 4>>());
vector<int> pos(m);
vector<int> used(m);
cin >> q;
int pu = 0;
// vector<array<int, 3>> upd;
// vector<array<int, 4>> qr;
for(int y = 0; y < q; y += S){
int k = min(q, y+S) - y;
int qq = 0, u = 0;
for(int j = 0; j < k; ++j){
int t, uu, v; cin >> t >> uu >> v;
if(t == 1){
--uu;
upd[u] = {v, uu, j};
++u;
}else{
qr[qq] = {v, uu, j, qq};
++qq;
}
}
Dsu d(n);
sort(qr, qr+qq, greater<array<int, 4>>());
auto it = SE.begin();
for(int l = 0; l < u; ++l) used[upd[l][1]] = 1;
for(int j = 0; j < qq; ++j){
int w = qr[j][0];
// cout << w << " : q\n" ;
while(it != SE.end() && -(*it)[0] >= w){
if(used[(*it)[3]]) ++it;
else{
d.merge((*it)[1], (*it)[2]);
// cout << edges[pu][3] << " : upd" << '\n';
++it;
}
}
int roll = 0;
for(int l = u - 1; l >= 0; --l){
int idx = upd[l][1];
if(used[idx] > 1) continue;
if(upd[l][2] < qr[j][2]){
used[idx] = l + 2;
}
}
for(int l = 0; l < u; ++l){
int idx = upd[l][1];
if(!(used[idx] == 1 || used[idx] == l + 2)) continue;
// used[idx] = 1;
if(upd[l][2] < qr[j][2]){
if(upd[l][0] >= w){
++roll;
d.merge(edges[idx][1], edges[idx][2]);
}
}else{
if(-edges[idx][0] >= w){
++roll;
d.merge(edges[idx][1], edges[idx][2]);
}
}
}
for(int l = 0; l < u; ++l) used[upd[l][1]] = 1;
// cout << qr[j][1] << ' ';
ans[qr[j][3]] = d.s[d.find(qr[j][1])];
for(int l = 0; l < roll; ++l){
d.rollback();
}
}
for(int l = 0; l < u; ++l){
int idx = upd[l][1];
used[idx] = 0;
SE.erase(SE.find(edges[idx]));
edges[idx][0] = -upd[l][0];
SE.insert(edges[idx]);
}
for(int l = 0; l < qq; ++l) cout << ans[l] << '\n';
}
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(tt--){
solve();
en;
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
return 0;
}
Compilation message (stderr)
bridges.cpp: In function 'void solve()':
bridges.cpp:78:7: warning: unused variable 'pu' [-Wunused-variable]
78 | int pu = 0;
| ^~
bridges.cpp: In function 'int main()':
bridges.cpp:165:15: warning: unused variable 'aa' [-Wunused-variable]
165 | int tt = 1, aa;
| ^~
# | 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... |