-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
72afca0
commit 3ac273a
Showing
6 changed files
with
554 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package BOJ.silver1; | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
class Graph{ | ||
|
||
class Node{ | ||
int data; | ||
Set<Node> linked; | ||
Node(int data){ | ||
this.data = data; | ||
linked = new HashSet<>(); | ||
} | ||
} | ||
|
||
Node[] nodes; | ||
boolean[] isVisited; | ||
|
||
Graph(int size){ | ||
nodes = new Node[size + 1]; | ||
isVisited = new boolean[size + 1]; | ||
|
||
for(int i = 1; i<= size; i++) { | ||
nodes[i] = new Node(i); | ||
} | ||
} | ||
|
||
public void add(int a, int b) { | ||
Node n1 = nodes[a]; | ||
Node n2 = nodes[b]; | ||
|
||
n1.linked.add(n2); | ||
} | ||
|
||
int count; | ||
|
||
public void bfs(int start) { | ||
Queue<Node> que = new ArrayDeque<>(); | ||
Node n = nodes[start]; | ||
que.add(n); | ||
count = 1; | ||
|
||
while(!que.isEmpty()) { | ||
Node n1 = que.poll(); | ||
if(!isVisited[n1.data]) { | ||
isVisited[n1.data] = true; | ||
for(Node n2 : n1.linked) { | ||
count++; | ||
que.add(n2); | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
public void initList(int size) { | ||
for(int i = 1; i<= size; i++) { | ||
if(isVisited[i]) { | ||
isVisited[i] = false; | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
public class BOJ_1325 { | ||
|
||
public static void main(String[] args) throws IOException{ | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); | ||
|
||
StringTokenizer st = new StringTokenizer(br.readLine()); | ||
|
||
int n = Integer.parseInt(st.nextToken()); | ||
int m = Integer.parseInt(st.nextToken()); | ||
|
||
Graph gh = new Graph(n); | ||
|
||
for(int i = 0; i<m; i++) { | ||
st = new StringTokenizer(br.readLine()); | ||
|
||
int a = Integer.parseInt(st.nextToken()); | ||
int b = Integer.parseInt(st.nextToken()); | ||
|
||
gh.add(b, a); | ||
} | ||
|
||
int max = 1; | ||
List<Integer> ll = new ArrayList<>(); | ||
|
||
for(int i = 1; i<=n; i++) { | ||
if(!gh.isVisited[i]) { | ||
gh.bfs(i); | ||
|
||
if(gh.count > max) { | ||
max = gh.count; | ||
ll.clear(); //한번 비우기 | ||
ll.add(i); | ||
}else if(gh.count == max) { | ||
ll.add(i); | ||
} | ||
gh.initList(n); //초기화 | ||
} | ||
} | ||
|
||
Collections.sort(ll); //오름차순 정렬 | ||
|
||
for(int i = 0; i < ll.size(); i++) { | ||
bw.write(ll.get(i) + " "); | ||
} | ||
bw.write("\n"); | ||
bw.flush(); | ||
br.close(); | ||
bw.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package BOJ.silver1; | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
class Graph_1389{ | ||
|
||
class Node{ | ||
int data; | ||
Set<Node> linked; | ||
|
||
Node(int data){ | ||
this.data = data; | ||
linked = new HashSet<>(); | ||
} | ||
} | ||
|
||
Node[] nodes; | ||
boolean[] isVisited; | ||
|
||
Graph_1389(int size){ | ||
nodes = new Node[size + 1]; | ||
isVisited = new boolean[size + 1]; | ||
|
||
for(int i = 1; i<= size; i++) { | ||
nodes[i] = new Node(i); | ||
} | ||
} | ||
|
||
public void add(int a, int b) { | ||
Node n1 = nodes[a]; | ||
Node n2 = nodes[b]; | ||
|
||
n1.linked.add(n2); | ||
n2.linked.add(n1); | ||
} | ||
|
||
int bacon; | ||
|
||
public void bfs(int start) { | ||
Queue<Node> que = new ArrayDeque<>(); | ||
Queue<Integer> distance = new ArrayDeque<>(); | ||
|
||
que.add(nodes[start]); | ||
distance.add(0); | ||
|
||
while(!que.isEmpty()) { | ||
Node n1 = que.poll(); | ||
int d = distance.poll(); | ||
|
||
if(!isVisited[n1.data]) { | ||
isVisited[n1.data] = true; | ||
bacon += d; | ||
|
||
for(Node n2 : n1.linked) { | ||
que.add(n2); | ||
distance.add(d+1); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public void initVisit(int size) { | ||
for(int i = 1; i<= size; i++) { | ||
isVisited[i] = false; | ||
} | ||
} | ||
} | ||
|
||
public class BOJ_1389 { | ||
|
||
public static void main(String[] args) throws IOException{ | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); | ||
|
||
StringTokenizer st = new StringTokenizer(br.readLine()); | ||
|
||
int n = Integer.parseInt(st.nextToken()); | ||
int m = Integer.parseInt(st.nextToken()); | ||
|
||
Graph_1389 gh = new Graph_1389(n); | ||
|
||
for(int i = 0; i<m; i++) { | ||
st = new StringTokenizer(br.readLine()); | ||
gh.add(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken())); | ||
} | ||
|
||
int min = 0; | ||
int minIndex = 1; | ||
|
||
|
||
for(int i = 1; i<=n; i++) { | ||
gh.bacon = 0; | ||
gh.bfs(i); | ||
|
||
if(i == 1) { //초기 상태 | ||
min = gh.bacon; | ||
}else { | ||
if(min > gh.bacon) { | ||
min = gh.bacon; | ||
minIndex = i; | ||
} | ||
} | ||
gh.initVisit(n); | ||
} | ||
|
||
bw.write(minIndex + "\n"); | ||
bw.flush(); | ||
br.close(); | ||
bw.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package BOJ.silver1; | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
public class BOJ_1697 { | ||
|
||
static int m; | ||
static boolean[] isVisit; | ||
static int ans; | ||
|
||
public static void main(String[] args) throws IOException{ | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); | ||
|
||
StringTokenizer st = new StringTokenizer(br.readLine()); | ||
|
||
int n = Integer.parseInt(st.nextToken()); | ||
m = Integer.parseInt(st.nextToken()); | ||
|
||
isVisit = new boolean[Math.max(n, m) + 2]; //아하....뒤로 갈 수 있기 때문에 배열은 동생 기준으로 잡을 수 없다! | ||
ans = 0; | ||
bfs(n); | ||
bw.write(ans + "\n"); | ||
bw.flush(); | ||
bw.close(); | ||
br.close(); | ||
} | ||
|
||
public static void bfs(int start) { | ||
Queue<Integer> que = new ArrayDeque<>(); | ||
Queue<Integer> distance = new ArrayDeque<>(); | ||
|
||
que.add(start); | ||
distance.add(0); | ||
|
||
while(!que.isEmpty()) { | ||
int q = que.poll(); | ||
int d = distance.poll(); | ||
|
||
if(q == m) { | ||
ans = d; | ||
break; | ||
} | ||
|
||
if(!isVisit[q]) { | ||
isVisit[q] = true; | ||
|
||
if(q*2 > 0 && q*2 <= m+1) { | ||
que.add(q*2); | ||
distance.add(d+1); | ||
} | ||
|
||
if(q+1 > 0 && q+1 <= m+1) { | ||
que.add(q+1); | ||
distance.add(d+1); | ||
} | ||
|
||
if(q-1 >= 0) { | ||
que.add(q-1); | ||
distance.add(d+1); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package BOJ.silver1; | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
|
||
public class BOJ_2583 { | ||
|
||
static int n; | ||
static int m; | ||
|
||
static int[] goX = {-1,0,1,0}; | ||
static int[] goY = {0,1,0,-1}; | ||
|
||
static boolean[][] isVisit; | ||
|
||
static int d; | ||
|
||
public static void main(String[] args) throws IOException { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); | ||
|
||
StringTokenizer st = new StringTokenizer(br.readLine()); | ||
|
||
n = Integer.parseInt(st.nextToken()); | ||
m = Integer.parseInt(st.nextToken()); | ||
int z = Integer.parseInt(st.nextToken()); | ||
|
||
isVisit = new boolean[n][m]; | ||
|
||
for(int i = 0; i<z; i++) { | ||
st = new StringTokenizer(br.readLine()); | ||
int x1 = Integer.parseInt(st.nextToken()); | ||
int y1 = (n-1) - Integer.parseInt(st.nextToken()); | ||
|
||
int x2 = Integer.parseInt(st.nextToken()); | ||
int y2 = (n-1) - Integer.parseInt(st.nextToken()); | ||
|
||
for(int p = y1; p>y2; p--) { | ||
for(int q = x1; q<x2;q++) { | ||
isVisit[p][q] = true; | ||
} | ||
} | ||
} | ||
|
||
List<Integer> l = new ArrayList<>(); | ||
|
||
int count = 0; | ||
|
||
for(int i = 0; i<n; i++) { | ||
for(int j = 0; j<m; j++) { | ||
if(!isVisit[i][j]) { | ||
isVisit[i][j] = true; | ||
d = 1; | ||
count++; | ||
|
||
dfs(j, i, d); //x,y | ||
l.add(d); | ||
} | ||
} | ||
} | ||
|
||
Collections.sort(l); | ||
bw.write(count + "\n"); | ||
|
||
for(int i = 0; i<l.size(); i++) { | ||
bw.write(l.get(i) + " "); | ||
} | ||
bw.write("\n"); | ||
bw.flush(); | ||
br.close(); | ||
bw.close(); | ||
} | ||
|
||
public static void dfs(int x, int y, int dept) { | ||
for(int i = 0; i<4; i++) { | ||
if(isIndexTrue(x, y, i) && !isVisit[y + goY[i]][x + goX[i]]){ | ||
isVisit[y + goY[i]][x + goX[i]] = true; | ||
d++; | ||
dfs(x + goX[i], y + goY[i], dept+1); | ||
} | ||
} | ||
} | ||
|
||
public static boolean isIndexTrue(int x, int y, int z) { | ||
if(x + goX[z] >= 0 && x + goX[z] < m && y + goY[z] >= 0 && y + goY[z] < n) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
} |
Oops, something went wrong.