이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,fma,bmi2,popcnt,lzcnt")
#include <bits/stdc++.h>
// #define int long long
#define all(v) v.begin(),v.end()
using namespace std;
mt19937_64 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
const int N=1e5;
int n,m,q,u[N+5],v[N+5],d[N+5],ty[N+5],x[N+5],y[N+5],ans[N+5],tr[N+5];
bool use[N+5];
void Update(int i, int j){
for (int k=i;k<=j;++k)
if (ty[k]==1) d[x[k]]=y[k];
}
struct DSU_rollback{
int n,ds[N+5];
vector <pair<int,int>> history;
void Reset(){
for (int i=1;i<=n;++i) ds[i]=-1;
history.clear();
}
int Get(int idx){
return ds[idx]<0 ? idx : Get(ds[idx]);
}
void Join(int u, int v){
u=Get(u);
v=Get(v);
if (u==v) return;
history.push_back({v,ds[v]});
ds[u]+=ds[v];
ds[v]=u;
}
void RollBack(int sz){
while (history.size()>sz){
pair <int,int> top=history.back();
history.pop_back();
ds[ds[top.first]]-=top.second;
ds[top.first]=top.second;
}
}
} dsu;
void Sol(int l, int r){
vector <int> before_edge,query;
if (l>1) for (int i=1;i<=m;++i) use[i]=false;
for (int i=l;i<=r;++i)
if (ty[i]==1) use[x[i]]=true;
else query.push_back(i);
for (int i=1;i<=m;++i)
if (!use[i]) before_edge.push_back(i);
sort(all(before_edge),[&](int x, int y){
return d[x]>d[y];
});
sort(all(query),[&](int a, int b){
return y[a]>y[b];
});
dsu.Reset();
int pointer=0;
for (int pos : query){
while (pointer<before_edge.size() && d[before_edge[pointer]]>=y[pos]){
dsu.Join(u[before_edge[pointer]],v[before_edge[pointer]]);
++pointer;
}
int size_before=dsu.history.size();
for (int i=1;i<=m;++i) tr[i]=d[i];
for (int i=l;i<pos;++i)
if (ty[i]==1)
tr[x[i]]=y[i];
for (int i=1;i<=m;++i)
if (tr[i]>=y[pos])
dsu.Join(u[i],v[i]);
ans[pos]=-dsu.ds[dsu.Get(x[pos])];
dsu.RollBack(size_before);
}
}
void Solve(){
dsu.n=1e5;
cin>>n>>m;
for (int i=1;i<=m;++i) cin>>u[i]>>v[i]>>d[i];
cin>>q;
for (int i=1;i<=q;++i) cin>>ty[i]>>x[i]>>y[i];
int bl=sqrt(q);
for (int i=1;i<=q;i+=bl){
int j=min(q,i+bl-1);
Sol(i,j);
Update(i,j);
}
for (int i=1;i<=q;++i)
if (ty[i]==2) cout<<ans[i]<<"\n";
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen ("FILE.INP","r",stdin);
// freopen ("FILE.OUT","w",stdout);
int t=1;
while (t--) Solve();
cerr<<"\nTIME: "<<1000*clock()/CLOCKS_PER_SEC<<"ms\n";
}
컴파일 시 표준 에러 (stderr) 메시지
bridges.cpp: In member function 'void DSU_rollback::RollBack(int)':
bridges.cpp:34:30: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
34 | while (history.size()>sz){
| ~~~~~~~~~~~~~~^~~
bridges.cpp: In function 'void Sol(int, int)':
bridges.cpp:59:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | while (pointer<before_edge.size() && d[before_edge[pointer]]>=y[pos]){
| ~~~~~~~^~~~~~~~~~~~~~~~~~~
# | 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... |