티스토리 뷰
문제 링크 : https://www.acmicpc.net/problem/1043
🍊해결방법
- 파티가 주어질 때마다 맨 처음 수를 기준으로 union하자
- 다시 파티들을 보면서 비밀을 아는 사람의 부모와 주어진 파티 참가자들의 부모가 같은지 판단
😀풀이
import java.util.*;
import java.io.*;
public class Main {
static int N;
static int M;
static int[] arr;
static List<Integer>[] list;
static int[] parent;
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
int t = Integer.parseInt(st.nextToken());
arr = new int[t]; // 비밀을 아는 사람
for(int i=0; i<t; i++){
arr[i] = Integer.parseInt(st.nextToken());
}
parent = new int[N+1];
for(int i=1; i<=N; i++){
parent[i] = i;
}
list = new List[M];
for(int i=0; i<M; i++){
st = new StringTokenizer(br.readLine());
int cnt = Integer.parseInt(st.nextToken());
list[i] = new ArrayList<>();
if (cnt == 0) continue;
int p = Integer.parseInt(st.nextToken());
list[i].add(p);
for(int j=0; j<cnt-1; j++){
int num = Integer.parseInt(st.nextToken());
union(p, num);
list[i].add(num);
}
}
int ans = 0;
for(int i=0; i<M; i++){
boolean check = true;
o : for(int x : list[i]){
// 비밀을 알고있는 사람이 있는지
for(int j=0; j<arr.length; j++){
// 비밀을 암
if(find(parent[x]) == find(arr[j])){
check = false;
break o;
}
}
}
if (check) ans++;
}
System.out.println(ans);
}// end main
static void union(int a, int b){
a = find(a);
b = find(b);
if (a == b)
return;
parent[b] = a;
}
static int find(int x){
if (parent[x] == x){
return x;
}
parent[x] = find(parent[x]);
return parent[x];
}
}
'알고리즘 > 백준' 카테고리의 다른 글
[백준] 31589 포도주 시음 - 투포인터 (0) | 2024.11.13 |
---|---|
[백준] 10422 괄호 - DP (0) | 2024.11.13 |
[백준] 9019 DSLR - BFS (0) | 2024.11.11 |
[백준] 11404 플로이드 - 플로이드워셜 (0) | 2024.11.06 |
[백준] 11058 크리보드 - DP (0) | 2024.10.25 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 오블완
- 백준 경사로
- sql
- 스프링부트3
- 스프링부트3 자바 버전
- #스프링부트 자바버전 오류
- leetcode 1552
- 백준 14890 경사로
- dp
- 스프링부트3 자바 17 오류
- 누적합
- 바텀업
- 조합
- 탑다운
- 티스토리챌린지
- 스프링부트3 java 버전오류
- 이진탐색
- 1482
- 백준 14890 자바
- 투포인터
- 백준
- 탑다운dp
- 구현
- 유니온파인드
- 백준 경사로 자바
- dfs
- 백준 14890
- BFS
- 위상정렬
- 슬라이딩윈도우
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
글 보관함