Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- api 비동기처리
- NestJS
- 우아한테크코스
- 음악 url 파일 다운로드
- api 요청 수 제한
- compateto
- 스프링부트
- 프로그래머스
- redis
- 코멘토 #코멘토실무PT #실무PT후기 #실무강의 #리액트강의 #웹프로그래밍 #react #웹개발실무
- invalid_grant
- concurrency limit
- 프론트엔드 과제
- TypeORM
- 자바스크립트
- 타입스크립트
- Deep Dive
- 프론트엔드
- 검색
- this
- AWS
- 프리코스
- 모던 자바스크립트
- 유효시간 설정 url
- Dev-Matching
- 딥다이브
- oauth
- bucket4j
- 파일 url
- 우아한 테크코스
Archives
- Today
- Total
개발 알다가도 모르겠네요
삼성 SDS 대학생 알고리즘 특강 6일차 - 그래프 본문
728x90
그래프 종류
- 무향 그래프
- 유향 그래프
- 가중치 그래프
- 정규 그래프
- 완전 그래프
- 연결 그래프
- 부분 그래프
- 트리 그래프
1717: 집합의 표현
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static int[] parent, depth;
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
parent = new int[N + 1];
depth = new int[N + 1];
for (int i = 1; i <= N; i++) {
parent[i] = i;
depth[i] = 1;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int command = Integer.parseInt(st.nextToken());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
if (command == 0) {
union(a, b);
} else if (command == 1) {
sb.append((find(a) == find(b) ? "YES" : "NO") + "\n");
} else {
continue;
}
}
System.out.println(sb);
br.close();
}
public static int find(int x) {
return parent[x] = (x == parent[x]) ? x :find(parent[x]);
}
public static void union(int x, int y) {
x = find(x);
y = find(y);
if (x != y) {
if (depth[x] < depth[y]) {
int tmp = x;
x = y;
y = tmp;
}
parent[y] = x;
if (depth[x] == depth[y]) depth[x]++;
}
}
}
2252: 줄 세우기
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
StringBuilder sb = new StringBuilder();
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
int[] in = new int[N+1];
ArrayList<Integer>[] adj = new ArrayList[N+1];
for(int i = 0; i < N+1; i++) {
adj[i] = new ArrayList();
}
for(int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int from = Integer.parseInt(st.nextToken());
int to = Integer.parseInt(st.nextToken());
adj[from].add(to);
in[to]++;
}
Queue<Integer> q = new LinkedList();
for(int i = 1; i < N+1; i++) {
if(in[i] == 0) {
q.add(i);
}
}
while(!q.isEmpty()) {
int now = q.poll();
sb.append(now).append(" ");
for(int next: adj[now]) {
in[next]--;
if(in[next] == 0) {
q.add(next);
}
}
}
System.out.println(sb);
}
}
1922: 네트워크 연결
import java.util.*;
import java.io.*;
public class Main {
static int N;
static int M;
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());
st = new StringTokenizer(br.readLine());
M = Integer.parseInt(st.nextToken());
PriorityQueue<int[]> pq = new PriorityQueue<int[]>(new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o1[2] - o2[2];
}
});
parent = new int[N+1];
for(int i = 1; i <= N; i++){
parent[i] = i;
}
int a, b, c;
for(int i = 0 ; i < M; i++){
st = new StringTokenizer(br.readLine());
a = Integer.parseInt(st.nextToken());
b = Integer.parseInt(st.nextToken());
c = Integer.parseInt(st.nextToken());
pq.add(new int[]{a,b,c});
}
int cnt = 0, total = 0;
while(cnt < N-1 && !pq.isEmpty()){
int[] tmp = pq.poll();
if(find(tmp[0]) != find(tmp[1])){
cnt++;
union(tmp[0], tmp[1]);
total += tmp[2];
}
}
System.out.println(total);
}
static int find(int x){
return parent[x] = (parent[x] == x) ? x : find(parent[x]);
}
static void union(int a, int b) {
a = find(a);
b = find(b);
parent[a] = b;
}
}
'학습일지 > 삼성 SDS 알고리즘 특강' 카테고리의 다른 글
삼성 SDS 대학생 알고리즘 특강 8일차 - 그래프 (0) | 2022.07.27 |
---|---|
삼성 SDS 대학생 알고리즘 특강 7일차 - 그래프 (0) | 2022.07.26 |
삼성 SDS 대학생 알고리즘 특강 5일차 - 조합론 (0) | 2022.07.22 |
삼성 SDS 대학생 알고리즘 특강 4일차 - 트라이 트리, 정수론 (0) | 2022.07.21 |
삼성 SDS 대학생 알고리즘 특강 3일차 - 자료구조 (0) | 2022.07.20 |