Submission #4181

# Submission time Handle Problem Language Result Execution time Memory
4181 2013-09-03T06:19:10 Z cki86201 두 섬간의 연결 (kriii1_2) C++
1 / 1
48 ms 1868 KB
#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
1 Correct 40 ms 1868 KB Output is correct
2 Correct 36 ms 1868 KB Output is correct
3 Correct 40 ms 1868 KB Output is correct
4 Correct 48 ms 1868 KB Output is correct
5 Correct 32 ms 1868 KB Output is correct
6 Correct 40 ms 1868 KB Output is correct