-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodesInAcircle.cpp
More file actions
77 lines (62 loc) · 1.65 KB
/
nodesInAcircle.cpp
File metadata and controls
77 lines (62 loc) · 1.65 KB
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
//Finding nodes in circle:
#define lim 100010
ll ens[ lim ]; //koyta circle ekhane shuru hoise
bool iscycle[lim]; //node ta ki cycle er moddhe pore ?
ll color[lim]; // kaj shuru , shesh naki choltese
vector <ll> graph[lim];
ll nCycle = 0;
bool dfs( ll node , ll parent ) {
if ( color[node] == 1 ) {
// cout<<node<<' '<<parent<<endl;
nCycle++;
ens[node]++;
return true;
}
//if( color[node] == 2 ) return false;
color[node] = 1;
ll prev = nCycle;
for ( ll i = 0 ; i < graph[node].size() ; i++ ) {
if ( parent == graph[node][i] || color[graph[node][i]] == 2 ) continue;
bool cycle = dfs( graph[node][i], node );
if ( cycle ) {
if ( nCycle > 0 ) iscycle[node] = true;
if ( ens[node] > 0 ) {
nCycle -= ens[node];
ens[node] = 0;
}
}
}
color[node] = 2;
//if(node==8) printf("8 = %lld\n", nCycle);
if ( nCycle > prev ) return true;
else return false;
}
int main() {
// freopen("in.txt","r",stdin);
ll n, m;
sl(n); sl(m);
for ( ll i = 0 ; i < m ; i++ ) {
ll u, v;
sl(u); sl(v);
graph[u].pb(v);
graph[v].pb(u);
}
nCycle = 0;
memset( color, 0, sizeof color );
memset( ens, 0, sizeof ens );
memset( iscycle, false, sizeof iscycle );
for ( ll i = 1 ; i <= n ; i++ ) {
if ( color[i] == 0 ) {
nCycle = 0;
dfs(i, 0);
}
}
ll ans = 0;
for ( ll i = 1 ; i <= n ; i++ ) {
if ( iscycle[i] == false ) {
ans++;
printf("%lld\n", i);
}
}
return 0;
}