SMALL

https://www.acmicpc.net/problem/2252

 

2252번: 줄 세우기

첫째 줄에 N(1 ≤ N ≤ 32,000), M(1 ≤ M ≤ 100,000)이 주어진다. M은 키를 비교한 회수이다. 다음 M개의 줄에는 키를 비교한 두 학생의 번호 A, B가 주어진다. 이는 학생 A가 학생 B의 앞에 서야 한다는 의

www.acmicpc.net


  • 문제


  • 문제풀이

BFS를 시행해주고 뒤의 숫자에 카운트를 해주어서 순서를 정할 수 있도록 해준다.

 


  • 코드 1
namespace YONGYONG2
{
    internal class Program
    {
        public static List<int>[] list;
        public static int[] Count;
        public static void bfs(int N)
        {
            Queue<int> queue = new Queue<int>();

            for (int i = 1; i < N + 1; i++)
            {
                if (Count[i] == 0)
                {
                    queue.Enqueue(i);
                }
            }
            while (queue.Count != 0)
            {
                int temp = queue.Dequeue();
                Console.Write(temp + " ");
                foreach (int a in list[temp])
                {
                    Count[a]--;
                    if (Count[a] == 0)
                    {
                        queue.Enqueue(a);
                    }
                }
            }
        }
        static void Main(string[] args)
        {
            string[] input = Console.ReadLine().Split();
            int N = int.Parse(input[0]);
            int M = int.Parse(input[1]);
            list = new List<int>[N + 1];
            for (int i = 1; i < N + 1; i++)
            {
                list[i] = new List<int>();
            }
            Count = new int[N + 1];
            for (int i = 0; i < M; i++)
            {
                string[] input2 = Console.ReadLine().Split();
                int a = int.Parse(input2[0]);
                int b = int.Parse(input2[1]);
                list[a].Add(b);
                Count[b]++;
            }
            bfs(N);
        }
    }
}

 


  • 후기

리스트안에 리스트를 넣어주는것이 상당히 좋은것 같다.

LIST

+ Recent posts