Submission #4181

#TimeUsernameProblemLanguageResultExecution timeMemory
4181cki86201두 섬간의 연결 (kriii1_2)C++98
1 / 1
48 ms1868 KiB
#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 timeMemoryGrader output
Fetching results...