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 <bits/stdc++.h>
using namespace std;
#define int long long
struct Unionfind{
vector<int> par, siz;
//初期化
Unionfind(int n) : par(n,-1) , siz(n,1) {}
int root(int x) {
if(par[x] == -1) return x;
else return par[x] = root(par[x]);
}
bool issame(int x,int y) {
return root(x) == root(y);
}
bool unite(int x, int y){
x = root(x); y = root(y);
if(x == y) return false;
if(siz[x] < siz[y]) swap(x,y);
par[y] = x;
siz[x] += siz[y];
return true;
}
int size(int x) {
return siz[root(x)];
}
};
int N,M;
vector<vector<int>> G(100100);
vector<int> A(100100,0);
void f(int u, int p){
if(A[u]) return;
A[u] = 1;
for(int x:G[u]){
if(x == p) continue;
f(x,u);
A[u] += A[x];
}
return;
}
signed main(){
cin>>N>>M;
Unionfind uf(N);
for(int i=0;i<M;i++){
int a,b;
cin>>a>>b;
a--;
b--;
G[a].push_back(b);
G[b].push_back(a);
uf.unite(a,b);
}
for(int i=0;i<N;i++) f(i,-1);
int ans = 0;
for(int i=0;i<N;i++){
int n = uf.size(i);
vector<int> S = {n-A[i]};
for(int x:G[i]){
if(A[x] > A[i]) continue;
S.push_back(A[x]);
}
int sum = 0;
int dec = 0;
for(int x:S){
sum += x;
dec += x*x;
}
ans += sum*sum-dec;
}
cout<<ans<<endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |