# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
332829 | Dymo | Bridges (APIO19_bridges) | C++14 | 3010 ms | 75484 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 pb push_back
#define eb emplace_back
#define ll int
#define pll pair<ll,ll>
#define ff first
#define ss second
#define endl "\n"
const ll maxn=1e5+100;
const ll mod =1e9+7 ;
const ll base=1e18;
const ll block=500;
/// con 1 ngay nua
/// zesitahn
struct dsu_rollback
{
ll par[maxn];
vector<pll> vt;
dsu_rollback()
{
memset(par,-1,sizeof(par));
vt.clear();
}
void rollback(ll t)
{
while (vt.size()>t)
{
auto p=vt.back();
vt.pop_back();
par[p.ff]=p.ss;
}
}
ll find_par(ll u)
{
if (par[u]<0)
return u;
return find_par(par[u]);
}
void merge(ll x,ll y)
{
x=find_par(x);
y=find_par(y);
if (x==y) return ;
if (par[x]>par[y])
{
swap(x,y);
}
vt.pb(make_pair(x,par[x]));
vt.pb(make_pair(y,par[y]));
par[x]+=par[y];
par[y]=x;
}
bool chk(ll x,ll y)
{
return find_par(x)==find_par(y);
}
};
ll w[maxn];
ll u[maxn];
ll v[maxn];
ll t[maxn];
ll a[maxn];
ll b[maxn];
ll wnw[maxn];
ll ans[maxn];
vector<ll> adj[maxn];
bool kt[maxn];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
if (fopen("queue.inp", "r"))
{
freopen("queue.inp", "r", stdin);
freopen("queue.out", "w", stdout);
}
ll n, m;
cin>> n>> m;
for (int i=1;i<=m;i++)
{
cin>>u[i]>>v[i]>>w[i];
}
ll q;
cin>> q;
for (int i=1;i<=q;i++)
{
cin>>t[i]>>a[i]>>b[i];
}
for (int i=1;i<=q;i+=block)
{
ll l=i;
ll r=min(i+block-1,q);
dsu_rollback dsu;
vector<pll> que;
vector<ll> change;
vector<pll> non;
for (int j=l;j<=r;j++)
{
if (t[j]==1)
{
kt[a[j]]=true;
change.pb(a[j]);
wnw[a[j]]=w[a[j]];
}
}
for (int j=l;j<=r;j++)
{
if (t[j]==1)
{
wnw[a[j]]=b[j];
}
else
{
for (auto p:change)
{
if (b[j]<=wnw[p])
{
adj[j].pb(p);
}
}
que.pb(make_pair(b[j],j));
}
}
for (int i=1;i<=m;i++)
{
if (!kt[i])
{
non.pb(make_pair(w[i],i));
}
}
sort(que.rbegin(),que.rend());
sort(non.rbegin(),non.rend());
ll idnw=0;
for (auto p:que)
{
ll id=p.ss;
while (idnw<non.size()&&non[idnw].ff>=p.ff)
{
dsu.merge(u[non[idnw].ss],v[non[idnw].ss]);
idnw++;
}
ll siznw=dsu.vt.size();
ll posnw=a[id];
//cout <<p.ff<<" "<<p.ss<<" "<<posnw<<" "<<siznw<<" "<<non.size()<<endl;
for (auto to:adj[id])
{
dsu.merge(u[to],v[to]);
}
ans[id]=-dsu.par[dsu.find_par(posnw)];
dsu.rollback(siznw);
}
for (int i=1;i<=m;i++)
{
if (kt[i])
{
w[i]=wnw[i];
}
kt[i]=false;
}
for (int j=l;j<=r;j++)
{
adj[j].clear();
}
}
for (int i=1;i<=q;i++)
{
if (t[i]==2)
{
cout <<ans[i]<<endl;
}
}
}
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... |