# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
5706 | baneling100 | 두 섬간의 연결 (kriii1_2) | C++98 | 48 ms | 2648 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <stdio.h>
long long N, anc[100001], Size[100001], Ans1, Ans2;
void input(void)
{
int i;
scanf("%lld",&N);
for(i=1 ; i<=N ; i++)
{
anc[i]=i;
Size[i]=1;
}
}
long long find_anc(long long temp)
{
long long x=temp, temp2;
while(anc[x]!=x)
{
x=anc[x];
}
while(anc[temp]!=x)
{
temp2=anc[temp];
anc[temp]=x;
temp=temp2;
}
return x;
}
void process(void)
{
int i;
long long temp, x;
for(i=1 ; i<=N-1 ; i++)
{
scanf("%lld",&temp);
x=find_anc(temp);
anc[temp+1]=x;
Ans1+=Size[x]*Size[temp+1];
Ans2+=Size[x]*Size[temp+1]*(Size[temp+1]+1)/2+Size[temp+1]*(Size[x]-1)*Size[x]/2;
Size[x]+=Size[temp+1];
Size[temp+1]=0;
printf("%lld %lld\n",Ans1,Ans2);
}
}
int main(void)
{
input();
process();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |