# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
20213 | joonas | 두 섬간의 연결 (kriii1_2) | C++98 | 44 ms | 8 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 <cstdio>
typedef long long lld;
int r[100001];
lld C[100001];
int find(int n){
if(n == r[n]) return n;
return r[n] = find(r[n]);
}
int merge(int p, int q){
p = find(p);
q = find(q);
if(p != q){
r[q] = p;
C[p] += C[q];
C[q] = 1;
}
return C[p];
}
lld sum3(int n){
return n*(n-1LL)*(n+1)/6LL;
}
int main(){
int i, n, k;
lld S[100001]={0};
lld pairs = 0, bridges = 0;
scanf("%d", &n);
for(i=1; i<=n; ++i){
r[i] = i, C[i] = 1;
S[i] = S[i-1] + i;
}
for(i=1; i<n; ++i){
scanf("%d", &k);
int k0 = find(k), k1 = find(k+1);
pairs -= S[C[k0]] + S[C[k1]];
bridges -= sum3(C[k0]) + sum3(C[k1]);
bridges += sum3(merge(k0, k1));
pairs += S[C[k0]] * S[C[k1]];
printf("%lld %lld\n", pairs, bridges);
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |