Skip to content

Commit ad6516b

Browse files
committed
전력망 둘로 나누기
1 parent 6b8d63a commit ad6516b

File tree

8 files changed

+292
-36
lines changed

8 files changed

+292
-36
lines changed

C/.vscode/c_cpp_properties.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"configurations": [
3+
{
4+
"name": "Win32",
5+
"includePath": [
6+
"${workspaceFolder}/**"
7+
],
8+
"defines": [
9+
"_DEBUG",
10+
"UNICODE",
11+
"_UNICODE"
12+
],
13+
"cStandard": "c17",
14+
"cppStandard": "c++17",
15+
"intelliSenseMode": "windows-msvc-x64"
16+
}
17+
],
18+
"version": 4
19+
}

C/.vscode/launch.json

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
// IntelliSense를 사용하여 가능한 특성에 대해 알아보세요.
3+
// 기존 특성에 대한 설명을 보려면 가리킵니다.
4+
// 자세한 내용을 보려면 https://go.microsoft.com/fwlink/?linkid=830387을(를) 방문하세요.
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "(Windows) 시작",
9+
"type": "cppvsdbg",
10+
"request": "launch",
11+
"program": "프로그램 이름 입력(예: ${workspaceFolder}/a.exe)",
12+
"args": [],
13+
"stopAtEntry": false,
14+
"cwd": "${fileDirname}",
15+
"environment": [],
16+
"console": "externalTerminal"
17+
},
18+
{
19+
"name": "(Windows) 시작",
20+
"type": "cppvsdbg",
21+
"request": "launch",
22+
"program": "프로그램 이름 입력(예: ${workspaceFolder}/a.exe)",
23+
"args": [],
24+
"stopAtEntry": false,
25+
"cwd": "${fileDirname}",
26+
"environment": [],
27+
"console": "externalTerminal"
28+
}
29+
]
30+
}

C/.vscode/settings.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"C_Cpp.errorSquiggles": "Disabled"
3+
}

C/Test.ccp

Whitespace-only changes.

C/Test.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <stdio.h>
2+
3+
int main(void){
4+
5+
int a = 5;
6+
int &b = a;
7+
func(b);
8+
printf("%d",a);
9+
return 0;
10+
}
11+
12+
int func(int &x){
13+
x = 3;
14+
}
+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
1. U를 루트로 하는 서브트리에 속한 정점의 수를 출력
3+
4+
2. tree DP
5+
6+
3. DFS를 통해 서브트리의 노드 개수를 DP에 저장
7+
*/
8+
9+
import java.util.*;
10+
import java.io.*;
11+
12+
public class BJ15681_트리와쿼리 {
13+
14+
public static ArrayList<Integer>[] tree;
15+
public static int[] dp;
16+
public static void main(String[] args) throws IOException{
17+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
18+
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
19+
20+
StringTokenizer st;
21+
22+
st = new StringTokenizer(br.readLine());
23+
24+
int n = Integer.parseInt(st.nextToken()); // 정점의 개수
25+
int r = Integer.parseInt(st.nextToken()); // 루트 정점
26+
int q = Integer.parseInt(st.nextToken()); // 쿼리 개수
27+
28+
dp = new int[n+1];
29+
30+
tree = new ArrayList[n+1];
31+
for(int i = 1; i <= n; i++){
32+
tree[i] = new ArrayList<>();
33+
}
34+
35+
for(int i = 0; i < n-1; i++){
36+
st = new StringTokenizer(br.readLine());
37+
int a = Integer.parseInt(st.nextToken());
38+
int b = Integer.parseInt(st.nextToken());
39+
40+
tree[a].add(b);
41+
tree[b].add(a);
42+
}
43+
44+
checkSubTree(r); // 정점부터 서브트리 노드 수 파악
45+
46+
for(int i = 0; i < q; i++){
47+
int u = Integer.parseInt(br.readLine());
48+
bw.write(dp[u] +"\n");
49+
}
50+
51+
bw.flush();
52+
bw.close();
53+
54+
}
55+
56+
57+
public static int checkSubTree(int node){
58+
if(dp[node] != 0){ //현재 노드를 방문했었으면 0반환
59+
return 0;
60+
}
61+
62+
dp[node] = 1; // 현재 노드 1개 초기화
63+
64+
for(int next : tree[node]){
65+
dp[node] += checkSubTree(next); // 자식노드의 개수 더하기
66+
}
67+
68+
return dp[node];
69+
}
70+
}

Java/백준/Test.java

+85-36
Original file line numberDiff line numberDiff line change
@@ -11,45 +11,94 @@
1111
import java.util.*;
1212

1313
public class Test {
14-
public static void main(String[] args) throws IOException{
15-
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
16-
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
1714

18-
int n = Integer.parseInt(br.readLine());
19-
20-
// Queue<Integer> q = new LinkedList<>();
15+
public static class Person{
16+
String name;
2117

22-
Deque<Integer> dq = new ArrayDeque<>();
23-
24-
StringTokenizer st;
25-
26-
for(int i = 0; i < n; i++){
27-
28-
st = new StringTokenizer(br.readLine());
29-
String command = st.nextToken();
30-
31-
if(command.equals("push")){
32-
int num = Integer.parseInt(st.nextToken());
33-
34-
dq.offer(num);
35-
}else if(command.equals("pop")){
36-
int num = dq.isEmpty() == true ? -1 : dq.poll();
37-
bw.write(num +"\n");
38-
}else if(command.equals("size")){
39-
bw.write(dq.size() +"\n");
40-
}else if(command.equals("empty")){
41-
int num = dq.isEmpty() == true ? 1 : 0;
42-
bw.write(num + "\n");
43-
}else if(command.equals("front")){
44-
int num = dq.isEmpty() == true ? -1 : dq.peekFirst();
45-
bw.write(num +"\n");
46-
}else{
47-
int num = dq.isEmpty() == true ? -1 : dq.peekLast();
48-
bw.write(num +"\n");
49-
}
18+
public Person(String name){
19+
this.name = name;
20+
}
21+
22+
public void setName(String name){
23+
this.name = name;
24+
}
25+
26+
public String toString(){
27+
return name;
5028
}
5129

52-
bw.flush();
53-
bw.close();
5430
}
31+
public static void main(String[] args) {
32+
Person p = new Person("a");
33+
System.out.println(p); // 원본변수 p 출력
34+
35+
createPerson(p); // p의 주소값을 복사하여 createPerson의 매개변수로 전달.
36+
// 내부에서 매개변수에 다른 객체의 주소값을 저장하였지만,
37+
// 내부에서 쓰는 p와 원본변수의 p는 다른 것이 됨.
38+
// 처음엔 주소값이 동일하므로 같은 객체의 프로퍼티에 접근은 가능!
39+
40+
System.out.println(p);
41+
42+
changeName(p);
43+
System.out.println(p);
44+
45+
} // main의 끝
46+
47+
public static void createPerson(Person p){
48+
p = new Person("c");
49+
System.out.println(p);
50+
}
51+
52+
public static void changeName(Person p){
53+
p.setName("b");
54+
}
55+
// public static void main(String[] args) {
56+
// // int width = 2;
57+
// // int height = 2;
58+
// // int[][] diagonals = {{1,1},{2,2}};
59+
60+
// int width = 51;
61+
// int height = 37;
62+
// int[][] diagonals = {{17,19}};
63+
// int answer = solution(width, height, diagonals);
64+
// System.out.println(answer);
65+
// }
66+
67+
// public static int solution(int width, int height, int[][] diagonals) {
68+
// int answer = 0;
69+
70+
// int div = 10_000_019;
71+
72+
// long[][] dp = new long[height+1][width+1];
73+
// dp[0][0] = 1;
74+
75+
// for(int i = 0; i <= height; i++){
76+
// for(int j = 0; j <= width; j++){
77+
// if(i == 0 & j == 0){
78+
// continue;
79+
// }
80+
// if(i > 0){
81+
// dp[i][j] += dp[i-1][j];
82+
// }
83+
// if(j > 0){
84+
// dp[i][j] += dp[i][j-1];
85+
// }
86+
// dp[i][j]%=div;
87+
// }
88+
// }
89+
90+
// // 대각선 처리
91+
// for(int i = 0; i < diagonals.length; i++){
92+
// int b = diagonals[i][0];
93+
// int a = diagonals[i][1];
94+
95+
// long ab = (dp[a][b-1] * dp[height-a+1][width-b])%div;
96+
// long ba = (dp[a-1][b] * dp[height-a][width-b+1])%div;
97+
98+
// answer += (ab+ba)%div;
99+
// answer %= div;
100+
// }
101+
102+
// return answer;
103+
// }
55104
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
1. 정점 100 간선 99
3+
4+
2. 간선마다 끊어서 확인..? (99 * )
5+
6+
3. 체크함수 bfs? n 100
7+
**/
8+
9+
import java.util.*;
10+
11+
class Solution {
12+
13+
public ArrayList<Integer>[] graph;
14+
public int solution(int n, int[][] wires) {
15+
int answer = 100;
16+
graph = new ArrayList[n+1];
17+
for(int i = 1 ; i <= n; i++){
18+
graph[i] = new ArrayList<>();
19+
}
20+
21+
for(int[] wire : wires){
22+
int a = wire[0];
23+
int b = wire[1];
24+
25+
graph[a].add(b);
26+
graph[b].add(a);
27+
}
28+
29+
for(int[] wire: wires){
30+
answer = Math.min(answer,wireCut(wire));
31+
}
32+
33+
return answer;
34+
}
35+
36+
public int wireCut(int[] wire){
37+
38+
int a = wire[0];
39+
int b = wire[1];
40+
41+
int cntA = bfs(a,b);
42+
int cntB = bfs(b,a);
43+
44+
return Math.abs(cntA-cntB);
45+
}
46+
47+
public int bfs(int node, int cutNode){
48+
49+
boolean[] visited = new boolean[graph.length];
50+
51+
Queue<Integer> q = new LinkedList<>();
52+
q.offer(node);
53+
visited[node] = true;
54+
55+
int cnt = 1;
56+
57+
while(!q.isEmpty()){
58+
int cur = q.poll();
59+
60+
for(int next : graph[cur]){
61+
if(visited[next] == false && next != cutNode){
62+
visited[next] = true;
63+
q.offer(next);
64+
cnt++;
65+
}
66+
}
67+
}
68+
69+
return cnt;
70+
}
71+
}

0 commit comments

Comments
 (0)