Skip to content

Commit 8e6418c

Browse files
committed
solve: 코딩테스트 문제 풀이 - 19일차
1 parent dc6e973 commit 8e6418c

File tree

3 files changed

+191
-0
lines changed

3 files changed

+191
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.*;
5+
6+
public class Main {
7+
static int N; // 사람 수
8+
static int M; // 파티 수
9+
static int truthCount; // 진실을 아는 사람 수
10+
static int[] truthArr; // 진실을 아는 사람
11+
12+
static int[] parent;
13+
14+
public static void main(String[] args) throws IOException {
15+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
16+
String[] parts = br.readLine().split(" ");
17+
18+
N = Integer.parseInt(parts[0]);
19+
M = Integer.parseInt(parts[1]);
20+
21+
parts = br.readLine().split(" ");
22+
truthCount = Integer.parseInt(parts[0]);
23+
truthArr = new int[truthCount];
24+
for (int i=0; i<truthCount; i++) {
25+
truthArr[i] = Integer.parseInt(parts[i+1]);
26+
}
27+
28+
List<List<Integer>> parties = new ArrayList<>();
29+
while (M-- > 0) {
30+
parts = br.readLine().split(" ");
31+
int n = Integer.parseInt(parts[0]);
32+
List<Integer> party = new ArrayList<>();
33+
for (int i=0; i<n; i++) {
34+
party.add(Integer.parseInt(parts[i + 1]));
35+
}
36+
parties.add(party);
37+
}
38+
parent = new int[N + 1];
39+
for (int i=1; i<=N; i++) {
40+
parent[i] = i;
41+
}
42+
43+
for (List<Integer> party: parties) {
44+
for (int i=1; i<party.size(); i++) {
45+
union(party.get(0), party.get(i));
46+
}
47+
}
48+
49+
Set<Integer> truthRoots = new HashSet<>();
50+
for (int t: truthArr) {
51+
truthRoots.add(find(t));
52+
}
53+
54+
int answer = 0;
55+
for (List<Integer> party: parties) {
56+
boolean canLie = true;
57+
for (int p: party) {
58+
if (truthRoots.contains(find(p))) {
59+
canLie = false;
60+
break;
61+
}
62+
}
63+
if (canLie)
64+
answer++;
65+
}
66+
System.out.println(answer);
67+
}
68+
69+
public static int find(int x) {
70+
if (parent[x] == x) return x;
71+
return parent[x] = find(parent[x]);
72+
}
73+
public static void union(int a, int b) {
74+
int rootA = find(a);
75+
int rootB = find(b);
76+
77+
if (rootA > rootB) {
78+
parent[rootA] = rootB;
79+
} else {
80+
parent[rootB] = rootA;
81+
}
82+
}
83+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
import java.util.*;
7+
8+
public class Main {
9+
static class Node {
10+
int to, weight;
11+
public Node(int to, int weight) {
12+
this.to = to;
13+
this.weight = weight;
14+
}
15+
}
16+
static int n;
17+
static List<Node>[] tree;
18+
public static void main(String[] args) throws IOException {
19+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
20+
n = Integer.parseInt(br.readLine());
21+
22+
tree = new ArrayList[n + 1];
23+
for (int i=1; i<=n; i++) {
24+
tree[i] = new ArrayList<>();
25+
}
26+
for (int i=0; i<n-1; i++) {
27+
String[] parts = br.readLine().split(" ");
28+
int u = Integer.parseInt(parts[0]);
29+
int v = Integer.parseInt(parts[1]);
30+
int weight = Integer.parseInt(parts[2]);
31+
32+
tree[u].add(new Node(v, weight));
33+
tree[v].add(new Node(u, weight));
34+
}
35+
36+
int[] result1 = bfs(1);
37+
int farNode = result1[0];
38+
39+
int[] result2 = bfs(farNode);
40+
int answer = result2[1];
41+
42+
System.out.println(answer);
43+
44+
/*
45+
for (int i=1; i<=n; i++) {
46+
for (Node next: tree[i]) {
47+
System.out.print(next.to + " ");
48+
}
49+
System.out.println();
50+
}
51+
*/
52+
}
53+
public static int[] bfs(int start) {
54+
boolean[] visited = new boolean[n + 1];
55+
int[] distance = new int[n + 1];
56+
Queue<Integer> queue = new LinkedList<>();
57+
queue.offer(start);
58+
visited[start] = true;
59+
60+
int maxDistance = 0;
61+
int farNode = start;
62+
while (!queue.isEmpty()) {
63+
int curr = queue.poll();
64+
65+
for (Node next: tree[curr]) {
66+
if (!visited[next.to]) {
67+
visited[next.to] = true;
68+
distance[next.to] = distance[curr] + next.weight;
69+
queue.offer(next.to);
70+
71+
if (distance[next.to] > maxDistance) {
72+
maxDistance = distance[next.to];
73+
farNode = next.to;
74+
}
75+
}
76+
}
77+
}
78+
79+
return new int[] {farNode, maxDistance};
80+
}
81+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public int[] solution(int n, long k) {
5+
List<Integer> numbers = new ArrayList<>();
6+
for (int i=1; i<=n; i++) {
7+
numbers.add(i);
8+
}
9+
10+
int[] result = new int[n];
11+
long[] fact = new long[n+1];
12+
fact[0] = 1;
13+
for (int i=1; i<=n; i++) {
14+
fact[i] = fact[i-1] * i;
15+
}
16+
17+
k--;
18+
for (int i=0; i<n; i++) {
19+
long f = fact[n - 1 - i];
20+
int index = (int)(k / f);
21+
result[i] = numbers.remove(index);
22+
k %= f;
23+
}
24+
25+
return result;
26+
}
27+
}

0 commit comments

Comments
 (0)