-
Notifications
You must be signed in to change notification settings - Fork 7
/
journey-to-the-moon.cpp
67 lines (62 loc) · 938 Bytes
/
journey-to-the-moon.cpp
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//https://www.hackerrank.com/challenges/journey-to-the-moon
//mandeep singh @msdeep14
#include<iostream>
#include<vector>
#include<stack>
#include<climits>
using namespace std;
stack<int> s;
int dfs(vector<vector <int> > &g,int taken[],int val)
{
s.push(val);
int count=1;
taken[val]=val;
while(!s.empty())
{
int x=s.top();
for(int i=0;i<g[x].size();i++)
{
if(g[x][i]!=taken[g[x][i]])
{
taken[g[x][i]]=g[x][i];
s.push(g[x][i]);
count++;
x=s.top();
i=-1;
}
}
s.pop();
}
return count;
}
int main()
{
int n,k;
int a,b,x;
long long res=0;
vector<vector <int> > g;
cin>>n>>k;
g.resize(n+1);
int taken[100004];
for(int i=0;i<=n;i++)
{
taken[i]=INT_MAX;
}
for(int i=0;i<k;i++)
{
cin>>a>>b;
g[a].push_back(b);
g[b].push_back(a);
}
for(int i=0;i<n;i++)
{
if(i!=taken[i])
{
x=dfs(g,taken,i);
res+=x*(n-x);
}
}
res=res/2;
cout<<res<<endl;
return 0;
}