Skip to content

Instantly share code, notes, and snippets.

@developer-sdk
Created February 7, 2018 14:43
Show Gist options
  • Select an option

  • Save developer-sdk/c66728b20dc76a72f112bb9a4fdf647b to your computer and use it in GitHub Desktop.

Select an option

Save developer-sdk/c66728b20dc76a72f112bb9a4fdf647b to your computer and use it in GitHub Desktop.
백준, 2623, 그래프, 음악프로그램
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
/**
* 백준, 2623, 그래프
* 음악프로그램
*
* @author whitebeard-k
*
*/
public class Problem2623 {
static int N;
static int M;
static int[][] link;
static LinkedList<Integer>[] list;
static int[] in;
@SuppressWarnings("unchecked")
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
M = sc.nextInt();
in = new int[N + 1];
link = new int[N + 1][N + 1];
list = new LinkedList[N + 1];
for (int i = 1; i <= N; i++)
list[i] = new LinkedList<>();
for (int i = 0; i < M; i++) {
int size = sc.nextInt();
int from = sc.nextInt();
for (int k = 1; k < size; k++) {
int to = sc.nextInt();
if (link[from][to] == 0) {
link[from][to] = 1;
list[from].add(to);
in[to]++;
}
from = to;
}
}
sc.close();
StringBuffer buffer = new StringBuffer();
Queue<Integer> queue = new LinkedList<>();
for (int i = 1; i <= N; i++) {
if (in[i] == 0) {
queue.add(i);
buffer.append(i).append("\n");
}
}
if (queue.isEmpty()) {
System.out.println(0);
return;
}
while (!queue.isEmpty()) {
int from = queue.poll();
for (int to : list[from]) {
if (link[from][to] == 1) {
if (--in[to] == 0) {
queue.add(to);
link[from][to] = 0;
buffer.append(to).append("\n");
}
}
}
}
for (int i = 1; i <= N; i++) {
if (in[i] != 0) {
System.out.println(0);
return;
}
}
System.out.println(buffer.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment