| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
|---|---|---|---|---|---|---|---|
| 538163 | urd05 | 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) | C++17 | 4 ms | 7252 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
set<int> rev[100001];
int in[100001];
int p[100001];
int sz[100001];
vector<int> vec[100001];
int main() {
int n,m;
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++) {
p[i]=i;
vec[i].push_back(i);
sz[i]=1;
}
long long ret=0;
for(int i=0;i<m;i++) {
int a,b;
scanf("%d %d",&a,&b);
if (p[a]==p[b]||rev[p[b]].find(a)!=rev[p[b]].end()) {
printf("%lld\n",ret);
continue;
}
if (rev[p[a]].find(b)==rev[p[a]].end()) {
rev[p[b]].insert(a);
ret+=sz[p[b]];
in[p[b]]++;
printf("%lld\n",ret);
continue;
}
a=p[a];
b=p[b];
if (sz[a]>sz[b]) {
swap(a,b);
}
ret-=1LL*sz[a]*(sz[a]-1);
ret-=1LL*sz[b]*(sz[b]-1);
ret+=1LL*(sz[a]+sz[b])*(sz[a]+sz[b]-1);
int cnt1=0;
int cnt2=0;
for(int i=0;i<vec[a].size();i++) {
if (rev[b].find(vec[a][i])!=rev[b].end()) {
rev[b].erase(vec[a][i]);
cnt2++;
}
}
vector<int> save;
for(auto now:rev[a]) {
if (p[now]==b) {
cnt1++;
save.push_back(now);
}
}
for(int i=0;i<save.size();i++) {
rev[a].erase(save[i]);
}
ret-=1LL*cnt1*sz[a];
ret-=1LL*cnt2*sz[b];
in[a]-=cnt1;
in[b]-=cnt2;
ret-=1LL*sz[a]*in[a]+1LL*sz[b]*in[b];
ret+=1LL*(sz[a]+sz[b])*in[b];
for(auto now:rev[a]) {
if (rev[b].find(now)==rev[b].end()) {
rev[b].insert(now);
ret+=sz[a]+sz[b];
in[b]++;
}
}
for(int i=0;i<vec[a].size();i++) {
vec[b].push_back(vec[a][i]);
p[vec[a][i]]=b;
}
sz[b]+=sz[a];
printf("%lld\n",ret);
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
