이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5+10;
int n,m,q;
stack<int> s1;
int sz[MAXN];
int par[MAXN];
void reset(){
for(int i=1;i<MAXN;i++){
par[i] = i;
}
for(int i=1;i<MAXN;i++){
sz[i] = 1;
}
}
int find(int a){
while(par[a] !=a){
a=par[a];
}
return a;
}
void merge(int a,int b){
a =find(a);
b = find(b);
if(a==b){
return;
}
if(sz[a]>sz[b]){
swap(a,b);
}
s1.push(a);
sz[b]+=sz[a];
par[a] =par[b];
}
void rollback(int x){
while(s1.size()>x){
int k = s1.top();
s1.pop();
sz[par[k]]-=sz[k];
par[k] = k;
}
}
int u[MAXN],v[MAXN],w[MAXN],t[MAXN],x[MAXN],y[MAXN];
bool changed[MAXN];
vector<int> to_join[1010];
int ans[MAXN];
bool cmp(int a,int b){
return y[a]>y[b];
}
bool cmp2(int a,int b){
return w[a]>w[b];
}
int main(){
cin>>n>>m;
for(int i=1;i<=m;i++){
cin>>u[i]>>v[i]>>w[i];
}
cin>>q;
for(int i=1;i<=q;i++){
cin>>t[i]>>x[i]>>y[i];
}
for(int l=1;l<=q;l+=1000){
int r = min(q,l+1000-1);
reset();
vector<int> ask,update,unchanged;
for(int i=0;i<=m;i++){
changed[i] = false;
}
for(int i=l;i<=r;i++){
if(t[i] == 1){
changed[x[i]]=true;
update.push_back(i);
}else{
ask.push_back(i);
}
}
for(int i=1;i<=m;i++){
if(!changed[i]){
unchanged.push_back(i);
}
}
for(int i=l;i<=r;i++){
if(t[i] == 1){
w[x[i]] = y[i];
}else{
to_join[i-l].clear();
for(int j:update){
if(w[x[j]]>=y[i]){
to_join[i-l].push_back(x[j]);
}
}
}
}
sort(ask.begin(),ask.end(),cmp);
sort(unchanged.begin(),unchanged.end(),cmp2);
int ptr = 0;
for(int i:ask){
while(ptr<unchanged.size() && w[unchanged[ptr]]>=y[i]){
merge(u[unchanged[ptr]],v[unchanged[ptr]]);
ptr++;
}
int prev_size = s1.size();
for(int j:to_join[i-l]){
merge(u[j],v[j]);
}
ans[i] = sz[find(x[i])];
rollback(prev_size);
}
}
for(int i=1;i<=q;i++){
if(t[i] == 2){
cout<<ans[i]<<endl;
}
}
}
컴파일 시 표준 에러 (stderr) 메시지
bridges.cpp: In function 'void rollback(int)':
bridges.cpp:38:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(s1.size()>x){
~~~~~~~~~^~
bridges.cpp: In function 'int main()':
bridges.cpp:101:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(ptr<unchanged.size() && w[unchanged[ptr]]>=y[i]){
~~~^~~~~~~~~~~~~~~~~
# | 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... |