# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
4181 | cki86201 | 두 섬간의 연결 (kriii1_2) | C++98 | 48 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>
#include<algorithm>
#include<string.h>
#include<vector>
#include<math.h>
#include<stdlib.h>
using namespace std;
int N;
int p[100010],r[100010];
long long ans1,ans2;
int fi(int x)
{
int r=x;
while(p[r]!=r)r=p[r];
while(x!=r){x=p[x];p[x]=r;}
return p[x]=r;
}
void uni(int a,int b)
{
a=fi(a),b=fi(b);
if(r[a]>r[b]){p[b]=a;ans1+=(long long)r[a]*r[b];ans2+=(long long)r[a]*r[b]+(long long)r[b]*r[a]*(r[a]-1)/2+(long long)r[a]*r[b]*(r[b]-1)/2;r[a]+=r[b];}
else{p[a]=b;ans1+=(long long)r[a]*r[b];ans2+=(long long)r[a]*r[b]+(long long)r[b]*r[a]*(r[a]-1)/2+(long long)r[a]*r[b]*(r[b]-1)/2;r[b]+=r[a];}
printf("%lld %lld\n",ans1,ans2);
}
int main()
{
scanf("%d",&N);
int i;
for(i=1;i<=N;i++){r[i]=1;p[i]=i;}
for(i=1;i<N;i++){
int x;scanf("%d",&x);
uni(x,x+1);
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |