#include <bits/stdc++.h>
using namespace std;
bool sub3(vector<vector<int>> &G){
for(int i=0;i<G.size();i++)if(G[i].size() > 2) return false;
return true;
}
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 main(){
int N,M;
cin>>N>>M;
vector<vector<int>> G(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);
}
if(sub3(G)){
Unionfind uf(N);
for(int i=0;i<N;i++){
for(int x:G[i]) uf.unite(i,x);
}
map<int,int> m;
for(int i=0;i<N;i++) m[uf.root(i)]++;
long long ans = 0;
for(auto x:m){
int n = x.second;
for(long long i=2;i<n;i++) ans += (i-1)*(n-i)*2;
}
cout<<ans<<endl;
return 0;
}
}
Compilation message
count_triplets.cpp: In function 'bool sub3(std::vector<std::vector<int> >&)':
count_triplets.cpp:4:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
4 | for(int i=0;i<G.size();i++)if(G[i].size() > 2) return false;
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
103 ms |
7804 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
102 ms |
7012 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
83 ms |
7068 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |