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 pb push_back
#define fi first
#define se second
#define sz(a) (int)(a.size())
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define MOD (ll)(1e9+7)
#define INF (ll)(1e18)
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll gcd(ll a,ll b){if(!b)return a;else return gcd(b,a%b);}
int blk = 1000;
const int MAXN = 555555;
vector<int>par(MAXN),szz(MAXN,1);
int find(int x){
if(x == par[x])return x;
return find(par[x]);
}
stack<int>stk;
void unite(int x,int y){
x = find(x);
y = find(y);
if(x == y)return;
if(szz[x] > szz[y])swap(x,y);
stk.push(x);
szz[y]+=szz[x];
par[x] = par[y];
}
void rollback(int n){
while(sz(stk) > n){
int x = stk.top();
szz[par[x]]-=szz[x];
par[x] = x;
stk.pop();
}
}
int main()
{
int n,m;
cin>>n>>m;
vector<int>w(m+1),v(m+1),u(m+1);
for(int i=1;i<=n;i++)par[i] = i;
for(int i=1;i<=m;i++)cin>>v[i]>>u[i]>>w[i];
int q;cin>>q;
vector<int>t(q),x(q),y(q),ans(q);
for(int i=0;i<q;i++)cin>>t[i]>>x[i]>>y[i];
vector<int>ask,upd,unchange;
vector<bool>change(m+1,0);
vii join(q);
for(int i=0;i<q;i++){
if(t[i] == 2){
ask.pb(i);
}else{
change[x[i]] = 1;
upd.pb(i);
}
}
for(int i=0;i<q;i++){
if(t[i] == 2){
for(int j:upd){
if(w[x[j]] >= y[i])join[i].pb(x[j]);
}
}else w[x[i]] = y[i];
}
for(int i=1;i<=m;i++){
if(!change[i])unchange.pb(i);
}
sort(all(ask),[&](const int &a,const int &b){return y[a] > y[b];});
sort(all(unchange),[&](const int &a,const int &b){return w[a] > w[b];});
int ptr = 0;
for(int i : ask){
while(ptr < sz(unchange) && w[unchange[ptr]] >= y[i]){
int id = unchange[ptr];
unite(v[id],u[id]);
ptr++;
}
int prev = sz(stk);
for(int j:join[i]){
unite(u[j],v[j]);
}
ans[i] = szz[find(x[i])];
rollback(prev);
}
for(int i=0;i<q;i++)if(t[i] == 2)cout<<ans[i]<<'\n';
}
# | 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... |