라벨이 codeforce인 게시물 표시

CodeForce707B - Bakery (배열 그래프)

 이 문제는 storage들 중에서 가장 가까운 도시를 찾는 문제이다. 그래프의 정보를 배열로 저장하여 storage와의 엣지가 가장 작은 node를 찾아낸다. 코드는 다음과 같다. 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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 import java.util.Scanner ; /** * Created by kjm81 on 2017-04-07. */ public class CodeForce707B { static int [] u = new int [ 100005 ]; static int [] v = new int [ 100005 ]; static int [] l = new int [ 100005 ]; static int [] storages = new int [ 100005 ]; public static void main (String[] args) { int n, m, k, x, y, z; Scanner sc = new Scanner(System. in ); n = sc. nextInt (); m = sc. nextInt (); k = sc. nextInt (); for ( int i = 0 ; i < m; i++) { u[i] = sc. nextInt (); v[i] = sc. nextInt (); l[i] = sc. nextInt (); } for ( int i ...

CodeForce429A - Xor-tree (트리와 XOR)

 이 문제는 xor의 특성을 갖고 있는 트리와 dfs를 이용하여 문제이다. 1~n 까지 번호가 매겨진 n개의 노드가 있는 트리에서 루트 노드는 1이고, 각 노드는 0 또는 1의 초기 값을 갖는다. 또 어떤 node를 뒤집으면 그 노드의 child node는 그대로이고 child node 의 child node는 같이 뒤집히는 형식을 띈다. 이 문제를 풀기위해서는 tree를 구현하고, 시작 tree -> 목표 tree로 진행하는데 몇 번의 flip을 해야하며, 어떤 node에서 flip을 해야하는지 체크를 하면 된다. 또한 뒤집혀야하는 count를 세서 뒤집는 과정을 수행해야 한다. 코드는 다음과 같다. 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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 import java.util.ArrayList ; import java.util.LinkedList ; import java.util.Scanner ; /** * Created by user on 2017-03-31. */ public class CodeForce429A { static ArrayList<Integer> changedList = ne...

CodeForce158C - Linux 터미널 cd and pwd

 이 문제는 리눅스 터미널의 cd와 pwd를 구현하는 문제로 linkedList를 이용하는 문제이다. 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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 import java.lang.reflect.Array ; import java.util.Arrays ; import java.util.LinkedList ; import java.util.Scanner ; import java.util.Stack ; public class CodeForce158C { static boolean[][] nodeTable = new boolean[ 505 ][ 505 ]; static String[] nodeString = new String[ 505 ]; static int currentNode = 0 , createdNode; static boolean[] visited = new boolean[ 505 ]; static String[] pathDi...

CodeForce158B - 택시 태워 보내기

 이 문제는 택시에 사람들을 태워 보낼 것인데 가장 효율이 좋게 보내는 방법을 생각해 내면 된다. 직관적으로 푸는 문제이므로 설명은 필요 없을 것 같다. 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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 import java.util.Scanner ; public class CodeForce158B { public static void main(String[] args) { Scanner sc = new Scanner(System. in ); int n; int count = 0 , one = 0 , two = 0 , three = 0 , four = 0 ; String str ; n = sc.nextInt(); sc.nextLine(); str = sc.nextLine(); String[] groups = str .split( " " ); for ( int i = 0 ; i < n; i++) { switch (Integer.parseInt(groups[i])) { case 1 : one++; break ; case 2 : two++; brea...

CodeForce107A - Dorm Water Supply (find non-cycle end-to-end)

 이 문제는 사이클이 아닌 그래프의 시작과 끝을 찾는 문제이다. 또한 연결된 그래프의 어떤 값 이 문제에서는 diameter 즉 지름이 가장 작은 것을 출력하면 된다. 풀이: 하나만 연결 된 즉 리프 노드를 찾아 내고 그 것으로 시작하여 연결된 그래프를 찾아낸다. 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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 import java.util.ArrayList ; import java.util.Arrays ; import java.util.LinkedList ; import java.util.Scanner ; public class CodeForce107A { static int n, p, t = 0 ; static int [] vertex = new int [ 1005 ]; static int [] diameter = new int [ 1005 ]; static int [] vertexCount = new int [ 1005 ]; static ArrayList<Integer> start = new ArrayList<Integer>(); static int [] minList = new int [ 1005 ]; static int [] p1 = new int [ 1005 ]; static int [] p2 = new int [ 1005 ]; static boolean flag = false; public static vo...

CodeForce103B - Cthulhu (그래프에서 cycle 찾기)

 이 문제는 그래프가 주어졌을 때, cycle을 찾는 문제로써 전형적인 DFS문제이다. 풀이:          연결된 상태를 2차원 배열에 저장하여 해당 노드를 방문했는지를 체크하면서 탐색            을 하는 것이다.          node 수 = edge 수 이면 cycle이 존재하고 방문한 노드의 개수가 n 개 이면          cycle인 그래프가 1개가 존재함 나타낸다.          따라서 다음과 같은 코드로 나타낼 수 있다. 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 31 32 33 34 35 36 37 38 39 import java.util.Scanner; /** * Created by user on 2017-03-31. * cycle 찾기 */ public class CodeForce103B { static boolean [][] table = new boolean [ 101 ][ 101 ]; static boolean [] visit = new boolean [ 101 ]; static int n, m, x, y, count = 0 ; public static void main( String [] args) { Scanner sc = new Scanner(System. in ); n = sc.nextInt(); m = sc.nextInt(); for ( int i...