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 ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define pf push_front
#define vi vector<ll>
#define vii vector<vi>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define all(a) a.begin(), a.end()
#define fi first
#define se second
using namespace std;
const ll mod = 1e9+7;
const ll inf = 2e18;
const ll B = 1e5 + 8;
const ll N = 1e5 + 8;
struct DSU_roll_back{
vector<ll> par,sz;
vector<pair<ll &,ll>> history;
void init(ll n){
par = vector<ll> (n+5);
sz = vector<ll> (n+5,1);
iota(all(par),0);
}
ll fs(ll v){
return par[v] == v?v:fs(par[v]);
}
void us(ll u, ll v){
u = fs(u), v = fs(v);
if(u == v) return;
if(sz[u] < sz[v]) swap(u,v);
history.pb({sz[u],sz[u]});
history.pb({par[v],par[v]});
par[v] = u;
sz[u] += sz[v];
}
ll now_time(){
return history.size();
}
void roll_back(ll time){
while(now_time() > time){
history.back().fi = history.back().se;
history.pop_back();
}
}
ll size(ll u){
return sz[fs(u)];
}
};
struct query{
ll id,t,v,w;
};
struct edge{
ll u,v,w;
};
vector<edge> ed = {{0,0,0}};
ll n,m,q;
vector<query> qr[N/B+2];
ll ans[N];
bool mark[N];
ll t[N];
void solve(){
cin >> n >> m;
DSU_roll_back dsu;
for(ll i = 1; i <= m; i++){
ll u,v,w;cin >> u >> v >> w;
ed.pb({u,v,w});
}
cin >> q;
for(ll i = 0; i < q; i++){
ll v,w;cin >> t[i] >> v >> w;
qr[i/B].pb({i,t[i],v,w});
}
for(ll block = 0; block <= (q-1)/B; block++){
dsu.init(n);
vector<edge> unchange;
for(auto [id,t,v,w]:qr[block]){
if(t == 1) mark[v] = 1;
}
sort(all(qr[block]),[&](query a, query b){
return a.w > b.w;
});
for(ll i = 1; i <= m; i++){
if(!mark[i]) unchange.pb(ed[i]);
}
sort(all(unchange),[&](edge a, edge b){
return a.w < b.w;
});
for(ll i = 0; i < qr[block].size(); i++){
auto [id,t,v,w] = qr[block][i];
while(unchange.size() && unchange.back().w >= w){
dsu.us(unchange.back().u,unchange.back().v);
unchange.pop_back();
}
if(t == 2){
ll time = dsu.now_time();
for(ll j = 0; j < qr[block].size(); j++){
auto [id1,t1,v1,w1] = qr[block][j];
if(t1 == 1){
if(id1 < id && j < i) dsu.us(ed[v1].u,ed[v1].v);
else if(id1 > id && ed[v1].w >= w) dsu.us(ed[v1].u,ed[v1].v);
}
}
ans[id] = dsu.size(v);
dsu.roll_back(time);
}
}
for(auto [id,t,v,w]:qr[block]) if(t == 1) ed[v].w = w, mark[v] = 0;
}
for(ll i = 0; i < q; i++){
if(t[i] == 2) cout << ans[i] << '\n';
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if (fopen("test.inp", "r")){
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
}
ll T = 1;
// cin >> T;
for (ll i = 1; i <= T; i++){
solve();
cout << '\n';
}
}
Compilation message (stderr)
bridges.cpp: In function 'void solve()':
bridges.cpp:92:25: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<query>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | for(ll i = 0; i < qr[block].size(); i++){
| ~~^~~~~~~~~~~~~~~~~~
bridges.cpp:100:33: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<query>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
100 | for(ll j = 0; j < qr[block].size(); j++){
| ~~^~~~~~~~~~~~~~~~~~
bridges.cpp: In function 'int main()':
bridges.cpp:122:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
122 | freopen("test.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:123:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
123 | freopen("test.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |