반응형
using System;
using System.Collections.Generic;
namespace test2
{
internal class Program
{
static bool[] visited;
static List<int> [] graph; // 그래프 배열..?
static void Main(string[] args)
{
// 컴퓨터의 수
int computerCount = int.Parse(Console.ReadLine());
// 연결된 쌍의 수
int connectionCount = int.Parse(Console.ReadLine());
// 그래프 초기화(1부터 시작하기 위해 크기 늘림)
graph = new List<int>[computerCount +1];
visited = new bool[computerCount + 1];
for(int j =1; j<=computerCount;j++)
{
graph[j] = new List<int>();
}
for(int i = 0; i < connectionCount; i++)
{
// 방문 여부와 연결이 되어있는 경우를 생각한다
int x = int.Parse(pair[0]);
int y = int.Parse(pair[1]);
// 양방향 연결이므로
graph[x].Add(y);
graph[y].Add(x);
}
int result = BFS(1);
Console.WriteLine(result -1);
}
static int BFS(int start)
{
int count = 0;
visited[start] = true;
Queue<int> q = new Queue<int>();
q.Enqueue(start); // 시작 컴퓨터 삽입
while (q.Count > 0)
{
// 큐에서 하나 꺼냄
int current = q.Dequeue();
count++;
// List하나를 꺼낸다
foreach(int next in graph[current])
{
if (!visited[next])
{
visited[next] = true;
q.Enqueue(next);
}
}
}
return count;
}
}
}
반응형
'C#' 카테고리의 다른 글
C# 클로저 (0) | 2024.01.31 |
---|---|
라이브러리(Library)란? (0) | 2023.10.11 |
C# : 백준 <유기농 배추> (0) | 2023.06.01 |
C# : 백준 <택시 기하학> (0) | 2022.08.11 |
C# : 백준 <5의 수난> (0) | 2022.07.21 |