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>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
#pragma GCC optimize("O3")
#define watch(x) cout<<(#x)<<"="<<(x)<<'\n'
#define mset(d,val) memset(d,val,sizeof(d))
#define setp(x) cout<<fixed<<setprecision(x)
#define sz(x) (int)(x).size()
#define all(x) begin(x), end(x)
#define forn(i,a,b) for(int i=(a);i<(b);i++)
#define fore(i,a,b) for(int i=(a);i<=(b);i++)
#define pb push_back
#define F first
#define S second
#define fbo find_by_order
#define ook order_of_key
typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
//template<typename T>
//using pbds = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
void SD(int t=0){ cout<<"PASSED "<<t<<endl; }
const ll INF = ll(1e18);
const int MOD = 998244353;
const bool DEBUG = 0;
const int MAXN = 50005;
const int MAXM = 100005;
const int MAXQ = 100005;
struct Edge
{
int u,v; int w;
bool operator<(const Edge &e){ return w<e.w; }
};
struct DSU
{
struct Node{ int p; int sz; };
vector<Node> dsu; int cc;
vector<pair<int,Node>> upd; // (v,(p,sz))
DSU(int n){ dsu.resize(n); cc=n; upd.clear();
forn(i,0,n) dsu[i]={i,1};
}
inline int rt(int u){ return u==dsu[u].p ? u : rt(dsu[u].p); }
inline bool sameset(int u, int v){ return rt(u)==rt(v); }
void merge(int u, int v){
u=rt(u); v=rt(v);
if(u==v) return;
if(dsu[u].sz<dsu[v].sz) swap(u,v);
upd.pb({v,dsu[v]});
dsu[v].p=u;
dsu[u].sz+=dsu[v].sz;
cc--;
}
void undo()
{
assert(!upd.empty());
auto u=upd.back();
int p=dsu[u.F].p;
dsu[p].sz-=u.S.sz;
dsu[u.F]=u.S;
upd.pop_back();
}
inline Node& operator[](int u){ return dsu[u]; }
};
int n,m,Q;
vector<Edge> edges;
vector<pair<ii,int>> queries; //(node,weight), time
vector<pair<ii,int>> updates; //(id,weight), time
int ans[MAXQ];
bool chg[MAXM];
int neww[MAXM];
bool cmpw(pair<ii,int> &a, pair<ii,int> &b){ return a.F.S>b.F.S; }
void process()
{
vector<Edge> fixed;
reverse(all(updates));
forn(i,0,m)
{
if(!chg[i]) fixed.pb(edges[i]);
else updates.pb({{i,edges[i].w},-1});
}
sort(all(fixed));
reverse(all(updates));
sort(all(queries),cmpw);
DSU dsu(n);
// large w to small w
for(auto q: queries)
{
int u=q.F.F; int w=q.F.S; int qtime=q.S;
while(!fixed.empty() && fixed.back().w>=w)
{
dsu.merge(fixed.back().u, fixed.back().v);
fixed.pop_back();
}
for(auto upd: updates)
{
if(upd.S>qtime) break;
int id=upd.F.F;
neww[id]=upd.F.S;
}
int updcnt=0;
for(auto upd: updates)
{
if(upd.S>qtime) break;
int id=upd.F.F;
if(neww[id]>=w)
{
if(dsu.sameset(edges[id].u, edges[id].v)) continue;
dsu.merge(edges[id].u, edges[id].v);
updcnt++;
}
}
ans[qtime]=dsu[dsu.rt(u)].sz;
while(updcnt--) dsu.undo();
}
for(auto upd: updates)
{
chg[upd.F.F]=0;
edges[upd.F.F].w=upd.F.S;
}
queries.clear();
updates.clear();
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n>>m;
forn(i,0,m)
{
int u,v; int w; cin>>u>>v>>w; u--; v--;
edges.pb({u,v,w});
}
cin>>Q;
forn(i,0,Q) ans[i]=-1;
int qcnt=0;
forn(i,0,Q)
{
int t; cin>>t;
if(t==1)
{
int id; int w; cin>>id>>w; id--;
updates.pb({{id,w},i});
chg[id]=1;
}
else
{
int u; int w; cin>>u>>w; u--;
queries.pb({{u,w},i});
}
qcnt++;
if(qcnt>=700 || i==Q-1)
{
process();
qcnt=0;
}
}
forn(i,0,Q)
{
if(ans[i]!=-1) cout<<ans[i]<<'\n';
}
return 0;
}
# | 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... |