# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
3359 | cuk123 | 두 섬간의 연결 (kriii1_2) | C++98 | 44 ms | 1868 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<stdio.h>
int n, parent[100001], size[100001];
long long r1, r2;
int find(int idx){
if(parent[idx]==idx)
return idx;
return parent[idx]=find(parent[idx]);
}
void Union(int a, int b){
int tmp;
long long n1, n2;
a=find(a);
b=find(b);
if(size[a]<size[b]){
tmp=a;
a=b;
b=tmp;
}
n1=(long long)size[a];
n2=(long long)size[b];
parent[b]=a;
r1+=n1*n2;
r2+=(n1*n2*(n2+1)/2LL+n2*n1*(n1-1)/2LL);
size[a]+=size[b];
}
int main(){
int i, num;
scanf("%d", &n);
for(i=1;i<n+1;++i){
parent[i]=i;
size[i]=1;
}
r1=r2=0;
for(i=1;i<n;++i){
scanf("%d", &num);
Union(num, num+1);
printf("%lld %lld\n", r1, r2);
}
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |