-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0401-2.cpp
More file actions
39 lines (38 loc) · 926 Bytes
/
0401-2.cpp
File metadata and controls
39 lines (38 loc) · 926 Bytes
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
#include <string>
#include <vector>
#include <queue>
using namespace std;
queue<int> q;
bool isVisited[20001] = {false};
vector<vector<int>> connected = vector<vector<int>>(20001);
int solution(int n, vector<vector<int>> edge)
{
int answer = 0;
for (int i = 0; i < edge.size(); i++)
{
connected[edge[i][0]].push_back(edge[i][1]);
connected[edge[i][1]].push_back(edge[i][0]);
}
q.push(1);
while (!q.empty())
{
int limit = q.size();
int cnt = 0;
for (int i = 0; i < limit; i++)
{
int x = q.front();
q.pop();
if (isVisited[x])
continue;
cnt++;
isVisited[x] = true;
for (int j = 0; j < connected[x].size(); j++)
{
q.push(connected[x][j]);
}
}
if (cnt != 0)
answer = cnt;
}
return answer;
}