Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions daeun/boj1043.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {

static boolean visited[];

public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int N= sc.nextInt(); //총 사람 수 (1부터)(노드 번호)
int M = sc.nextInt();//파티 수(간선 수)
int K = sc.nextInt();//진실아는 사람 수

visited = new boolean[N+1];
ArrayList<Integer>[] graph = new ArrayList[N+1];

for(int i=1; i<=N; i++) {
graph[i] = new ArrayList<>();
}

boolean[] knowT = new boolean[N+1];
for(int i=0; i<K; i++) {
int knowp = sc.nextInt();//아는 사람 번호
knowT[knowp] = true;
}


ArrayList<Integer>[] parties = new ArrayList[M];
for(int i=0; i<M; i++) {
int partynum = sc.nextInt();//각 파티마다 오는 사람 수
parties[i] = new ArrayList<>();
for(int j=0; j<partynum; j++) {
int p = sc.nextInt();
parties[i].add(p); //각 파티에 오는사람들 추가

}

for(int j =0; j<parties[i].size(); j++) {
for(int k=j+1; k<parties[i].size(); k++) {
graph[parties[i].get(j)].add(parties[i].get(k));
graph[parties[i].get(k)].add(parties[i].get(j));
}
}
}

for(int i=1; i<=N; i++) {
if(knowT[i] && !visited[i]) {
bfs(graph, i);

}
}

int cnt =0;
for(int i=0; i<M; i++) {
boolean dontknow = true;
for(int p :parties[i]) {
if(visited[p])
dontknow = false;
break;
}
if(dontknow) {

cnt++;
}
}

System.out.println(cnt);


}//main

static void bfs(ArrayList<Integer>[] graph, int num) {
Queue<Integer>q = new LinkedList<>();
q.add(num);
visited[num] = true;

while(!q.isEmpty()) {
int curr =q.poll();//현재 사람
for(int next: graph[curr]) {
if(!visited[next]) {
visited[next] = true;
q.add(next);
}
}
}

}

}
4 changes: 4 additions & 0 deletions daeun/boj1068.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

public class boj1068 {

}
105 changes: 105 additions & 0 deletions daeun/boj1197.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class 최소스패닝트리 {

static int V, E;


static class Edge implements Comparable<Edge> {

int to; //방문 정점
int cost; //가중치

public Edge(int to, int cost) {
this.to =to;
this.cost = cost;
}

@Override
public int compareTo(Edge o) {
return Integer.compare(this.cost, o.cost);
}


}

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
st = new StringTokenizer(br.readLine());
V = Integer.parseInt(st.nextToken());
E = Integer.parseInt(st.nextToken());

List<Edge>[] graph= new ArrayList[V+1]; //간선들을 리스트로 저장하니까

for(int i=1; i<=V; i++) {
graph[i] = new ArrayList<>();
}

for(int i=0; i<E; i++) {
st = new StringTokenizer(br.readLine());

int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int cost = Integer.parseInt(st.nextToken());


graph[a].add(new Edge(b, cost));
graph[b].add(new Edge(a, cost));


}
int total = prim(graph);


System.out.println(total);
}//main


private static int prim(List<Edge>[] graph) {
int total =0;
PriorityQueue<Edge> pq = new PriorityQueue<>();
boolean visited[] = new boolean [V+1];

pq.add(new Edge(1, 0));

while(!pq.isEmpty()) {
Edge edge = pq.poll();

int currNode = edge.to;
int cost = edge.cost;

if(visited[currNode]) {
continue;
}


total+= cost;
visited[currNode] = true;


for(Edge e: graph[currNode]) {
if(!visited[e.to]) {
pq.add(e);
}
}

}

return total;

}





}
102 changes: 102 additions & 0 deletions daeun/boj1647.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class 도시분할계획 {

static int N, M; //마을(정점), 길(간선)


static class Edge implements Comparable<Edge> {

int to;
int cost;
public Edge(int to, int cost) {
this.to = to;
this.cost = cost;
}

@Override
public int compareTo(Edge o) {
return this.cost - o.cost; //가중치 기준 오름차순 정렬할 것
}

}


public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

StringTokenizer st;
st = new StringTokenizer(br.readLine());

N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());

List<Edge> graph[] = new ArrayList[N+1];

for(int i=1; i<=N; i++) {
graph[i] = new ArrayList<>();
}


for(int i=0; i<M; i++) {
st = new StringTokenizer(br.readLine());
int A = Integer.parseInt(st.nextToken());
int B = Integer.parseInt(st.nextToken());
int C = Integer.parseInt(st.nextToken());

graph[A].add(new Edge(B, C));
graph[B].add(new Edge(A, C));

}


int total = prim(graph);

System.out.println(total);


}//main


//프림
private static int prim(List<Edge>[] graph) {
boolean visited[] = new boolean[N+1];
PriorityQueue<Edge> pq = new PriorityQueue<>();
int total =0;
int maxcost =0;
pq.add(new Edge(1,0));


while(!pq.isEmpty()) {
Edge edge = pq.poll();
int currNode = edge.to;
int cost = edge.cost;

if(visited[currNode]) {
continue;
}

visited[currNode] = true;
total+=cost;

maxcost = Math.max(maxcost, cost);

for(Edge e: graph[currNode]) {
if(!visited[e.to]) {
pq.add(new Edge(e.to, e.cost));
}
}


}

return total-maxcost;
}

}
76 changes: 76 additions & 0 deletions daeun/boj1707.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class 이분그래프 {

static int visited[];
static int V, E;
static boolean isgraph;


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int K = sc.nextInt();



for(int i=0; i<K; i++) {
isgraph = true;
V = sc.nextInt();//정점 개수(1부터 시작)
E = sc.nextInt();//간선ㄱ ㅐ수
visited = new int[V+1];
ArrayList<Integer>[] graph = new ArrayList[V+1];
for(int j=1; j<=V; j++) {
graph[j] = new ArrayList<>();
}

for(int j=0; j<E; j++) {
int num1 = sc.nextInt();
int num2 = sc.nextInt();
graph[num1].add(num2);
graph[num2].add(num1);
}

for(int k=1; k<=V; k++) {
if(visited[k]==0) {
if(!bfs(graph, k)) {
isgraph= false;
break;
}
}

}

if(isgraph)
System.out.println("YES");
else
System.out.println("NO");

}//k줄
}//main


static boolean bfs(ArrayList<Integer>[] graph, int num) {
Queue<Integer> q = new LinkedList<>();
q.add(num);
visited[num]=1;
while(!q.isEmpty()) {
int curr = q.poll();

for(int next: graph[curr]) {
if(visited[next]==0) {
visited[next] =(visited[curr]==1)? 2:1;
q.add(next);
}else if(visited[next]==visited[curr]) {
return false;
}
}
}

return true;

}

}
Loading