이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<iostream>
#include<set>
#include<vector>
#define DIM 100005
using namespace std;
int n, q, i, j, x, y, r1, r2, ry;
long long sol;
int r[DIM], num[DIM], nv[DIM];
vector<int> v[DIM], vr[DIM];
set<int> s[DIM], sr[DIM];
int rad(int x){
while(r[x] > 0){
x = r[x];
}
return x;
}
int main(){
cin>> n >> q;
for(i = 1; i <= n; i++){
r[i] = -1;
num[i] = 1;
vr[i].push_back(i);
}
for(; q; q--){
cin>> x >> y;
v[y].push_back(x);
r1 = rad(x);
r2 = rad(y);
if(r1 == r2){
cout<< sol <<"\n";
continue;
}
if(s[x].find(r2) == s[x].end() ){
sol += num[r2];
nv[r2]++;
s[x].insert(r2);
}
sr[r1].insert(r2);
if(sr[r2].find(r1) == sr[r2].end() ){
cout<< sol <<"\n";
continue;
}
if(r[r1] > r[r2]){
swap(r1, r2);
}
sol -= num[r1] * 1LL * (num[r1] - 1) + num[r1] * 1LL * nv[r1];
sol -= num[r2] * 1LL * (num[r2] - 1) / 2 + num[r2] * 1LL * nv[r2];
for(i = 0; i < vr[r2].size(); i++){
x = vr[r2][i];
vr[r1].push_back(x);
if(s[x].find(r1) != s[x].end() ){
nv[r1]--;
s[x].erase(r1);
}
for(j = 0; j < v[x].size(); j++){
y = v[x][j];
ry = rad(y);
if(ry == r1){
s[y].erase(r2);
continue;
}
if(s[y].find(r1) == s[y].end() ){
s[y].insert(r1);
nv[r1]++;
}
s[y].erase(r2);
sr[ry].insert(r1);
sr[ry].erase(r2);
}
}
set<int> :: iterator it;
for(it = sr[r2].begin(); it != sr[r2].end(); it++){
sr[r1].insert(*it);
}
r[r1] += r[r2];
r[r2] = r1;
num[r1] += num[r2];
sr[r1].erase(r2);
sol += num[r1] * 1LL * (num[r1] - 1) + num[r1] * 1LL * nv[r1];
cout<< sol <<"\n";
}
}
컴파일 시 표준 에러 (stderr) 메시지
joitter2.cpp: In function 'int main()':
joitter2.cpp:48:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i = 0; i < vr[r2].size(); i++){
~~^~~~~~~~~~~~~~~
joitter2.cpp:55:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(j = 0; j < v[x].size(); j++){
~~^~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |