import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int testcase = in.nextInt();
		for (int t = 1; t <= testcase; t++) {
			int limit = in.nextInt();
			int relationNum = in.nextInt();

			List<List<Integer>> relations = new ArrayList<List<Integer>>();
			for (int i = 0; i <= limit; i++) {
				relations.add(new ArrayList<Integer>());
			}
			for (int i = 0; i < relationNum; i++) {
				int start = in.nextInt();
				int end = in.nextInt();
				relations.get(start).add(end);
				relations.get(end).add(start);
			}

			HashSet<Integer> invited = isBestFriend(new HashSet<Integer>(), relations, true);
			invited.remove(1);
			System.out.println("#" + t + " " + invited.size());
		}
	}

	public static HashSet<Integer> isBestFriend(HashSet<Integer> invited, List<List<Integer>> relations, boolean isFirst) {
		if (!isFirst) {
			HashSet<Integer> newHashSet = new HashSet<Integer>(invited);
			Iterator<Integer> it = invited.iterator();
			while (it.hasNext()) {
				newHashSet.addAll(relations.get(it.next()));
			}

			return newHashSet;
		}

		invited.addAll(relations.get(1));
		return isBestFriend(invited, relations, false);
	}
}

 

+ 주의할 점

JAVA concurrent memory error

'알고리즘 공부' 카테고리의 다른 글

SWEA 3462: 선표의 축구 경기 예측  (0) 2021.01.10
SWEA 1949: 등산로 조성  (0) 2021.01.07
SWEA 1267: 작업순서  (0) 2021.01.03
SWEA 5653: 줄기세포 배양  (0) 2021.01.03
SWEA: 5215 햄버거 다이어트  (0) 2020.12.15

+ Recent posts