-
Notifications
You must be signed in to change notification settings - Fork 10
/
HackerEarth-Strongly Connected Components
88 lines (85 loc) · 1.46 KB
/
HackerEarth-Strongly Connected Components
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include<bits/stdc++.h>
using namespace std;
int c=0,d=0;
int bar=0;
vector<int>v[17];
vector<int>rev[17];
stack<int>s;
void dfs(int u,bool visited[])
{
visited[u]=true;
vector<int>::iterator it;
for( it=v[u].begin();it!=v[u].end();it++)
{
if(!visited[*it])
{
dfs((*it),visited);
}
}
//cout<<u<<endl;
s.push((u));
}
void dfs2(int u,bool nwvisited[])
{
bar++;
nwvisited[u]=true;
vector<int>::iterator it;
for( it=rev[u].begin();it!=rev[u].end();it++)
{
if(!nwvisited[*it])
{
dfs2((*it),nwvisited);
}
}
}
int main()
{
int m;
int n,u,t;
cin>>n>>m;
for(int i=0;i<m;i++)
{
cin>>u>>t;
v[u].push_back(t);
rev[t].push_back(u);
}
bool *visited=new bool[n+1];
for(int i=1;i<=n;i++)
{
visited[i]=false;
}
for(int i=1;i<=n;i++)
{
bar=1;
if(!visited[i]){
dfs(i,visited);}
//bar++;
}
bool *nwvisited=new bool[n+1];
for(int i=1;i<=n;i++)
{
nwvisited[i]=false;
}
while(!s.empty())
{
int b=s.top();
//cout<<"stack"<<b<<endl;
s.pop();
bar=0;
if(!nwvisited[b])
{
dfs2(b,nwvisited);
}
if(bar%2==0)
{
c=c+bar;
}
else
{
d=d+bar;
}
//cout<<bar<<endl;
}
cout<<(d-c)<<endl;
return 0;
}