-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCIO _Core_Challenge_Hackerrank.cpp
More file actions
146 lines (117 loc) · 2.56 KB
/
CIO _Core_Challenge_Hackerrank.cpp
File metadata and controls
146 lines (117 loc) · 2.56 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//Given an undirected graph containing cycle. Find minimum distance of every node from cycle.
//Concepts used : Finding cycle in an undirected graph, multi-source BFS using priority queue
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pi;
vector < vector <int> > adj;
vector <int> color, parent, cycle;
int cycle_start, cycle_end, n;
bool dfs(int v)
{
color[v] = 1;
for(int u : adj[v])
{
if(parent[v] != u && color[u] == 0)
{
parent[u] = v;
if(dfs(u))
return true;
}
else if(parent[v] != u && color[u] == 1)
{
cycle_end = v;
cycle_start = u;
return true;
}
}
color[v] = 2;
return false;
}
void find_cycle()
{
color.assign(n, 0);
parent.assign(n, -1);
cycle_start = -1;
for(int v = 0; v < n; v++)
{
if(dfs(v))
break;
}
if(cycle_start == -1)
return; // or cout << "Acyclic" << endl;
else
{
for(int v = cycle_end; v != cycle_start; v = parent[v])
cycle.push_back(v);
cycle.push_back(cycle_start);
}
}
vector<int> findRemoteness(int g_nodes, vector<int> g_from, vector<int> g_to)
{
n = g_nodes;
vector <int> remote(n);
adj.resize(n);
int edges = g_from.size();
for(int i = 0; i < edges; i++)
{
adj[g_from[i]-1].push_back(g_to[i]-1);
adj[g_to[i]-1].push_back(g_from[i]-1);
}
find_cycle();
vector < bool > visited(n, false);
priority_queue < pi, vector <pi>, greater<pi> > q;
for(int i = 0; i < cycle.size(); i++)
{
q.push({0, cycle[i]});
visited[cycle[i]] = true; // since multi-source BFS
}
while(!q.empty())
{
int ele = q.top().second;
int rem = q.top().first;
q.pop();
visited[ele] = true;
remote[ele] = rem;
for(int u : adj[ele])
{
if(!visited[u])
q.push({rem+1, u});
}
}
return remote;
}
int main()
{
int nodes, edges;
cin >> nodes >> edges;
vector<int> g_from(edges);
vector<int> g_to(edges);
for (int i = 0; i < edges; i++)
cin >> g_from[i] >> g_to[i];
vector<int> result = findRemoteness(nodes, g_from, g_to);
for (int i = 0; i < result.size(); i++)
cout << result[i] << endl;
return 0;
}
// Sample Input
// 6 (nodes) 6 (edges)
// 1 2
// 3 4
// 6 4
// 2 3
// 1 3
// 3 5
// Sample Output
// 0
// 0
// 0
// 1
// 1
// 2
// 6 6
// 1 2
// 3 4
// 6 4
// 2 3
// 1 3
// 3 5