-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAJTM.cpp
More file actions
45 lines (45 loc) · 823 Bytes
/
AJTM.cpp
File metadata and controls
45 lines (45 loc) · 823 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <bits/stdc++.h>
using namespace std;
int check[100009];
int carray[100009];
int main()
{
int n , I;
cin>>n>>I;
vector<int> v[n+9];
for(int i = 0 ; i < I ; i++)
{
int a , b;
cin>>a>>b;
v[a].push_back(b);
v[b].push_back(a);
}
int count = 1;
for(int i = 0 ; i < n ; i++)
{
if(check[i]==0){
queue<int> q;
q.push(i);
while(!q.empty())
{
int x = q.front();
for(int j = 0 ; j < v[x].size() ; j++)
if(check[v[x][j]]==0)
q.push(v[x][j]);
check[x] = count;
q.pop();
}
count++;
}
}
for(int i = 0 ; i < n; i++)
if(check[i]!=0)
carray[check[i]]++;
long long temp = n;
long long total = (temp*(temp-1))/2;
long long res = 0;
for(int i = 1 ; i<=count ; i++)
res += ((long long)(carray[i]*(carray[i] - 1))/2);
cout<<total - res<<endl;
return 0;
}