-
Notifications
You must be signed in to change notification settings - Fork 1
/
MooTube.cpp
74 lines (61 loc) · 1.13 KB
/
MooTube.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
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long int ull;
struct node
{
int a; ull w;
};
// index에 연결된 노드를 모아놓는 변수
vector<node> arr[5001];
int main()
{
int vCnt, lCnt;
cin >> vCnt >> lCnt;
for (int i = 0; i < vCnt - 1; i++)
{
int a, b;
ull w;
cin >> a >> b >> w;
// 양방향 간선
arr[a].push_back({ b,w });
arr[b].push_back({ a,w });
}
for (int i = 0; i < lCnt; i++)
{
// BFS
bool visited[5001];
fill_n(visited, 5001, false);
queue<int> q;
int target, count = 0;
ull usado;
// 처음 탐색할 노드를 queue에 넣는다
cin >> usado >> target;
q.push(target);
// queue가 빌 때까지
while (!q.empty())
{
int node = q.front();
q.pop();
// 탐색하지 않은 곳이라면 => 탐색
if (!visited[node])
{
visited[node] = true; // 방문했음을 체크
// 인접한 노드들을 모두 탐색
for (int j = 0; j < arr[node].size(); j++)
{
if (arr[node][j].w >= usado)
{
int n = arr[node][j].a;
// 방문하지 않았다면 enque & count++
if (!visited[n])
{
q.push(n);
count++;
}
}
}
}
}
cout << count << endl;
}
}