이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
int n,m,q;
struct canh {
int u,v,w;
} e[500005];
int dd[200005],ans[200005];
struct disjoint_set_union_rollback {
vector <int> lab, sz;
stack <int> save;
int n;
disjoint_set_union_rollback(int _n = 0) : n(_n) {
lab.resize(n + 1, 0);
sz.resize(n + 1, 1);
iota(lab.begin(),lab.end(), 0);
}
void clear() {
while((int) save.size()) save.pop();
fill(sz.begin(),sz.end(), 1);
iota(lab.begin(),lab.end(), 0);
}
int find(int u) {
assert(u <= n);
// cout << u << '\n';
return lab[u] == u ? u : find(lab[u]);
}
bool merge(int u, int v) {
u = find(u), v = find(v);
if(u == v) return false;
if(sz[u] < sz[v]) swap(u, v);
sz[u] += sz[v];
lab[v] = u;
save.push(v);
return true;
}
void rollback(int ptr) {
while((int) save.size() > ptr) {
int v = save.top(); save.pop();
sz[lab[v]] -= sz[v];
lab[v] = v;
}
}
};
bool cmp(int a,int b) {
if(e[a].w!=e[b].w)return e[a].w>e[b].w;
return a<b;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for(int i=1; i<=m; i++) {
cin>>e[i].u>>e[i].v>>e[i].w;
}
cin>>q;
for(int i=1; i<=q; i++)
cin>>e[i+m].u>>e[i+m].v>>e[i+m].w;
const int blsize=1000;
disjoint_set_union_rollback dsu(n+5);
for(int l=m+1; l<=q+m; l+=blsize) {
int r=min(l+blsize-1,m+q);
dsu.clear();
vector<int>change;
vector<int>unchange;
for(int i=l; i<=r; i++) {
if(e[i].u==1) {
change.push_back(i);
dd[e[i].v]=1;
} else unchange.push_back(i);
}
for(int i=1; i<=m; i++)if(dd[i]==0)unchange.push_back(i);
else dd[i]=0;
sort(unchange.begin(),unchange.end(),cmp);
reverse(change.begin(),change.end());
for(auto v:unchange) {
if(v<=m) {
dsu.merge(e[v].u,e[v].v);
} else {
int now=(int)dsu.save.size();
for(auto vv:change) {
if(vv<=v&&dd[e[vv].v]==0) {
dd[e[vv].v]=1;
int id=e[vv].v;
if(e[vv].w>=e[v].w)dsu.merge(e[id].u,e[id].v);
}
}
for(auto vv:change) {
if(vv>v&&dd[e[vv].v]==0) {
int id=e[vv].v;
if(e[id].w>=e[v].w)dsu.merge(e[id].u,e[id].v);
}
}
ans[v]=dsu.sz[dsu.find(e[v].v)];
dsu.rollback(now);
for(auto vv:change)dd[e[vv].v]=0;
}
}
for(int i=l;i<=r;i++)if(e[i].u==1){int id=e[i].v;e[id].w=e[i].w;}
}
for(int i=m+1; i<=m+q; i++)if(ans[i])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... |