# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
723187 | Son | Bridges (APIO19_bridges) | C++14 | 2665 ms | 46768 KiB |
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;
#define LL long long
#define pb push_back
int n,m,q;
int U[100005], V[100005], W[100005];
int T[100005], X[100005], Y[100005];
int ans[100005];
int dsu[100005], sz[100005];
const int D = 1000;
bool changed[100005];
vector < int > to_join[100005],stck;
int rt( int a ){
if ( dsu[a] == a ) return a;
return rt(dsu[a]);
}
bool cmp_ask( int a, int b ){
return Y[a] > Y[b];
}
bool cmp_unchanged( int a, int b ){
return W[a] > W[b];
}
void merge( int a, int b ){
a = rt(a);
b = rt(b);
if ( a == b ) return;
if ( sz[a] > sz[b] ) swap(a,b);
stck.pb(a);
sz[b] += sz[a];
dsu[a] = dsu[b];
}
void rollback( int prev_stack_size ){
while ( stck.size() > prev_stack_size ) {
int a = stck.back();
stck.pop_back();
sz[dsu[a]] -= sz[a];
dsu[a] = a;
}
}
int main(){
scanf("%d%d",&n,&m);
for ( int i = 1; i <= m; i++ ){
scanf("%d%d%d",&U[i],&V[i],&W[i]);
}
scanf("%d",&q);
for ( int i = 1; i <= q; i++ ){
scanf("%d%d%d",&T[i],&X[i],&Y[i]);
}
for ( int l = 1; l <= q; l += D){
int r = min(q + 1, l + D );
memset(changed,false,sizeof(changed));
vector < int > upd,ask,unchanged;
for ( int i = l; i < r; i++ ){
if ( T[i] == 1 ) {
changed[X[i]] = true;
upd.pb(i);
} else {
ask.pb(i);
}
}
for ( int i = l; i < r; i++ ){
to_join[i-l].clear();
if ( T[i] == 1 ){
W[X[i]] = Y[i];
continue;
}
if ( T[i] == 2 ){
for ( int j : upd ){
if ( W[X[j]] >= Y[i] ) to_join[i-l].pb(j);
}
}
}
for ( int i = 1; i <= m; i++ ){
if ( !changed[i] ){
unchanged.pb(i);
}
}
sort(unchanged.begin(),unchanged.end(),cmp_unchanged);
sort(ask.begin(),ask.end(),cmp_ask);
for ( int i = 1; i <= n; i++ ){
dsu[i] = i;
sz[i] = 1;
}
int ptr = 0;
for ( int i : ask ){
while ( ptr < unchanged.size() && W[unchanged[ptr]] >= Y[i] ){
merge(U[unchanged[ptr]], V[unchanged[ptr]]);
ptr++;
}
int stack_size = (int)stck.size();
for ( int j : to_join[i-l] ) merge(U[X[j]], V[X[j]]);
ans[i] = sz[rt(X[i])];
rollback(stack_size);
}
}
for ( int i = 1; i <= q; i++ ) if ( T[i] == 2 ) printf("%d\n",ans[i]);
return 0;
}
Compilation message (stderr)
# | 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... |