-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwheresmyinternet.cpp
75 lines (63 loc) · 1.4 KB
/
wheresmyinternet.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
68
69
70
71
72
73
74
75
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vectorOfInt;
vector<vectorOfInt> al;
vectorOfInt visited;
void dfs(int node)
{
//mark current node as visited--1
visited[node] = 1;
//visit its neighbours
for(auto &it : al[node])
if(!visited[it]) //if not visited, call dfs(it)
dfs(it);
}
void bfs(int node)
{
list<int> queue;
// Mark the current node as visited and enqueue
visited[node] = 1;
queue.push_back(node);
while(!queue.empty())
{
node = queue.front();
queue.pop_front();
for (auto &it : al[node])
{
if (!visited[it])
{
visited[it] = 1;
queue.push_back(it);
}
}
}
}
int main()
{
freopen("test.txt", "r", stdin);
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0);
int N, M;
cin >> N >> M;
al.assign(N, vectorOfInt());
while(M--)
{
int a, b;
cin >> a >> b;
a--;
b--;
al[a].push_back(b);
al[b].push_back(a);
}
visited.assign(N, 0);
bfs(0);
bool notConnected = false;
for(int i = 0; i < visited.size(); i++)
if(!visited[i]) // 0 means not connected
{
cout << i + 1 << endl;
notConnected = true;
}
if(!notConnected)
cout << "Connected" << endl;
return 0;
}