백준 24479 c++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector <int> graph[200001];//벡터 배열 : 정수배열 200001개가 아님
bool visited[200001];
int result[200001];
int cnt = 0;
void input_graph(int M)
{
int i, u, v;
for (i = 1; i <= M; i++)
{
cin >> u >> v;
graph[u].push_back(v);
graph[v].push_back(u);
}
for (i = 1; i <= M; i++)
{
sort(graph[i].begin(), graph[i].end());
}
return;
}
void dfs(int n)
{
int i;
if (visited[n] == true)
{
return;
}
else
{
visited[n] = true;
cnt++;
result[n] = cnt;
for (i = 0; i < graph[n].size(); i++)
{
dfs(graph[n][i]);
}
}
}
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int N, M, R;
cin >> N >> M >> R;
input_graph(M);
dfs(R);
for (int i = 1; i <= N; i++)
{
cout << result[i] << "\n";
}
return 0;
}