[BOJ] MooTube $($Silver) 15591 C++ 풀이
Updated:
문제
접근방법
구현
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
typedef struct
{
int idx, val;
}tube;
int main(void)
{
ios::sync_with_stdio(false); cin.tie(0);
int N, Q;
vector<tube> v[5001];
bool visited[5001];
tube input;
int start, end, val, res, usado;
int n, q;
cin>>N>>Q;
for(int i=0; i<N-1; i++)
{
cin>>start>>end>>val;
input.idx = start;
input.val = val;
v[end].push_back(input);
input.idx = end;
v[start].push_back(input);
}
for(int i=0; i<Q; i++)
{
cin>>usado>>start;
memset(visited, false, sizeof(visited));
res = 0;
visited[start] = true; // BFS start
queue<int> q;
q.push(start);
while(!q.empty())
{
int u = q.front();
q.pop();
for(int j=0; j<v[u].size(); j++)
{
int next = v[u][j].idx;
if(visited[next]) continue; // 이미 방문한 노드
int nv = v[u][j].val;
if(nv >= usado)
{
visited[next] = true; // 방문 처리
res++;
q.push(next);
}
}
}
cout<<res<<'\n';
}
return 0;
}
Leave a comment