diff --git "a/#18 0530_\354\265\234\354\206\214_\354\213\240\354\236\245_\355\212\270\353\246\254/BOJ_1244_sample.cpp" "b/#18 0530_\354\265\234\354\206\214_\354\213\240\354\236\245_\355\212\270\353\246\254/BOJ_1244_sample.cpp" new file mode 100644 index 0000000..18f97a7 --- /dev/null +++ "b/#18 0530_\354\265\234\354\206\214_\354\213\240\354\236\245_\355\212\270\353\246\254/BOJ_1244_sample.cpp" @@ -0,0 +1,57 @@ +#include +#include + +using namespace std; + +//남학생의 스위치 바꾸기 +vector changeSwitchBoy(int n, int number, vector switches) { + for (int i = number; i <= n; i += number) { //배수에 있는 스위치 바꾸기 + switches[i] = !switches[i]; + } + return switches; +} + +//여학생의 스위치 바꾸기 +vector changeSwitchGirl(int n, int number, vector switches) { + switches[number] = !switches[number]; + for (int i = 1; i < number; i++) { + if ((number + i > n) || (switches[number - i] != switches[number + i])) { //스위치 범위 넘어가거나 좌우 대칭이 아니면 + break; + } + switches[number - i] = !switches[number - i]; + switches[number + i] = !switches[number + i]; + } + return switches; +} + +int main() { + int n, m; + int student, number; + + //입력 + cin >> n; + vector switches(n + 1, 0); + for (int i = 1; i <= n; i++) { + cin >> switches[i]; + } + + //입력 & 연산 + cin >> m; + while (m--) { + cin >> student >> number; + if (student == 1) { + switches = changeSwitchBoy(n, number, switches); + } else { + switches = changeSwitchGirl(n, number, switches); + } + } + + //출력 + for (int i = 1; i <= n; i++) { + cout << switches[i] << ' '; + if (i % 20 == 0) { + cout << '\n'; + } + } + return 0; +} \ No newline at end of file diff --git "a/#18 0530_\354\265\234\354\206\214_\354\213\240\354\236\245_\355\212\270\353\246\254/BOJ_16202_sample.cpp" "b/#18 0530_\354\265\234\354\206\214_\354\213\240\354\236\245_\355\212\270\353\246\254/BOJ_16202_sample.cpp" new file mode 100644 index 0000000..6ca6fda --- /dev/null +++ "b/#18 0530_\354\265\234\354\206\214_\354\213\240\354\236\245_\355\212\270\353\246\254/BOJ_16202_sample.cpp" @@ -0,0 +1,79 @@ +#include +#include +#include +#include + +using namespace std; +typedef tuple tp; + +vector parent; + +//Find 연산 +int findParent(int x) { + if (parent[x] < 0) { + return x; + } + return parent[x] = findParent(parent[x]); +} + +//Union 연산 +bool unionNodes(int x, int y) { + int px = findParent(x); + int py = findParent(y); + + if (px == py) { + return false; + } + if (parent[px] < parent[py]) { //새로운 루트 px + parent[px] += parent[py]; + parent[py] = px; + } else { //새로운 루트 py + parent[py] += parent[px]; + parent[px] = py; + } + return true; +} + +int kruskal(int n, int idx, vector &edge) { + int cnt = 0, sum = 0; + for (int i = idx; i < edge.size(); i++) { + int dist = get<0>(edge[i]); + int x = get<1>(edge[i]); + int y = get<2>(edge[i]); + if (!unionNodes(x, y)) { + continue; + } + sum += dist; + cnt++; + if (cnt == n - 1) { + return sum; + } + } + return 0; //mst를 만들 수 없음 +} + +int main() { + int n, m, k, x, y; + + cin >> n >> m >> k; + vector edge; + for (int i = 1; i <= m; i++) { + cin >> x >> y; + edge.push_back({i, x, y}); + } + + bool flag = false; + for (int i = 0; i < k; i++) { + if (flag) { //더이상 mst를 만들 수 없음 + cout << 0 << ' '; + continue; + } + parent.assign(n + 1, -1); //초기화 + int ans = kruskal(n, i, edge); + if (ans == 0) { + flag = true; + } + cout << ans << ' '; + } + return 0; +} \ No newline at end of file diff --git "a/#18 0530_\354\265\234\354\206\214_\354\213\240\354\236\245_\355\212\270\353\246\254/BOJ_1647_sample.cpp" "b/#18 0530_\354\265\234\354\206\214_\354\213\240\354\236\245_\355\212\270\353\246\254/BOJ_1647_sample.cpp" new file mode 100644 index 0000000..c791501 --- /dev/null +++ "b/#18 0530_\354\265\234\354\206\214_\354\213\240\354\236\245_\355\212\270\353\246\254/BOJ_1647_sample.cpp" @@ -0,0 +1,73 @@ +#include +#include +#include +#include + +using namespace std; +typedef tuple tp; + +vector parent; + +//Find 연산 +int findParent(int x) { + if (parent[x] < 0) { + return x; + } + return parent[x] = findParent(parent[x]); +} + +//Union 연산 +bool unionNodes(int x, int y) { + int px = findParent(x); + int py = findParent(y); + + if (px == py) { + return false; + } + if (parent[px] < parent[py]) { //새로운 루트 px + parent[px] += parent[py]; + parent[py] = px; + } else { //새로운 루트 py + parent[py] += parent[px]; + parent[px] = py; + } + return true; +} + +int kruskal(int n, vector &edge) { + int sum = 0, cnt = 0; + for (int i = 0; i < edge.size(); i++) { + int w = get<0>(edge[i]); + int u = get<1>(edge[i]); + int v = get<2>(edge[i]); + if (!unionNodes(u, v)) { + continue; + } + sum += w; + cnt++; + if (cnt == n - 1) { + return sum; + } + } + return 0; +} + +int main() { + int n, m, a, b, c; + + //입력 + cin >> n >> m; + vector edge; + parent.assign(n + 1, -1); + while (m--) { + cin >> a >> b >> c; + edge.push_back({c, a, b}); + } + + //연산 + sort(edge.begin(), edge.end()); + + //연산&출력 + cout << kruskal(n - 1, edge); + return 0; +} \ No newline at end of file diff --git "a/#18 0530_\354\265\234\354\206\214_\354\213\240\354\236\245_\355\212\270\353\246\254/BOJ_1774_sample.cpp" "b/#18 0530_\354\265\234\354\206\214_\354\213\240\354\236\245_\355\212\270\353\246\254/BOJ_1774_sample.cpp" new file mode 100644 index 0000000..b0e39c8 --- /dev/null +++ "b/#18 0530_\354\265\234\354\206\214_\354\213\240\354\236\245_\355\212\270\353\246\254/BOJ_1774_sample.cpp" @@ -0,0 +1,95 @@ +#include +#include +#include +#include +#include + +using namespace std; +typedef tuple tp; +typedef pair ci; + +vector parent; + +//Find 연산 +int findParent(int x) { + if (parent[x] < 0) { + return x; + } + return parent[x] = findParent(parent[x]); +} + +//Union 연산 +bool unionNodes(int x, int y) { + int px = findParent(x); + int py = findParent(y); + + if (px == py) { + return false; + } + if (parent[px] < parent[py]) { //새로운 루트 px + parent[px] += parent[py]; + parent[py] = px; + } else { //새로운 루트 py + parent[py] += parent[px]; + parent[px] = py; + } + return true; +} + +double kruskal(int v, vector &edge) { + double sum = 0; + int cnt = 0; + + for (int i = 0; i < edge.size(); i++) { + double cost = get<0>(edge[i]); + int x = get<1>(edge[i]); + int y = get<2>(edge[i]); + + if (!unionNodes(x, y)) { + continue; + } + sum += cost; + cnt++; + if (cnt == v - 1) { + return sum; + } + } + return 0; +} + +int main() { + int n, m, a, b, v = 0; + double x, y; + + //입력 + cin >> n >> m; + parent.assign(n + 1, -1); + vector edge; + vector star(n + 1); + + for (int i = 1; i <= n; i++) { + cin >> x >> y; + for (int j = 1; j < i; j++) { + //임의의 두 별에 대한 거리(간선) 모두 구하기 + double dx = x - star[j].first; + double dy = y - star[j].second; + edge.push_back({sqrt(dx * dx + dy * dy), i, j}); + } + star[i] = {x, y}; + } + + //연산 + while (m--) { + cin >> a >> b; + if (unionNodes(a, b)) { //이미 연결된 통로 + v++; + } + } + sort(edge.begin(), edge.end()); + + //연산 & 출력 + cout << fixed; + cout.precision(2); + cout << kruskal(n - v, edge); + return 0; +} \ No newline at end of file diff --git "a/#18 0530_\354\265\234\354\206\214_\354\213\240\354\236\245_\355\212\270\353\246\254/PG__sample.cpp" "b/#18 0530_\354\265\234\354\206\214_\354\213\240\354\236\245_\355\212\270\353\246\254/PG__sample.cpp" new file mode 100644 index 0000000..64173cb --- /dev/null +++ "b/#18 0530_\354\265\234\354\206\214_\354\213\240\354\236\245_\355\212\270\353\246\254/PG__sample.cpp" @@ -0,0 +1,106 @@ +#include +#include + +using namespace std; + +vector>> board; + +//현재 상태가 조건에 만족하는지 (x, y) 중심으로 확인 +bool validate(int x, int y, int p, int n) { + if (p == 0) { //기둥 + if (y == 0) { //바닥 위 + return true; + } + if (board[x][y][1] || (x > 0 && board[x - 1][y][1])) { //보의 왼쪽/오른쪽 끝 + return true; + } + if (y > 0 && board[x][y - 1][0]) { //기둥 위 + return true; + } + } else { //보 + if (y > 0 && board[x][y - 1][0]) { //왼쪽 끝이 기둥 위 + return true; + } + if (x < n && y > 0 && board[x + 1][y - 1][0]) { //오른쪽 끝이 기둥 위 + return true; + } + if (x > 0 && x < n && board[x - 1][y][1] && board[x + 1][y][1]) { //양쪽 끝이 보와 연결 + return true; + } + } + return false; +} + +//(x, y)에 있는 구조물을 삭제할 수 있는지 확인 +bool checkRemovable(int x, int y, int n) { + int dx[3] = {-1, 0, 1}; + int dy[3] = {-1, 0, 1}; + //기둥이 삭제된 경우, 해당 기둥 위에 보가 있었을 수 있으므로 (조건 2-b) 대각선도 확인이 필요 + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + int nx = x + dx[i]; + int ny = y + dy[j]; + if (nx < 0 || nx > n || ny < 0 || ny > n) { + continue; + } + for (int k = 0; k < 2; k++) { + if (board[nx][ny][k] && !validate(nx, ny, k, n)) { + return false; + } + } + } + } + return true; +} + +vector> solution(int n, vector> build_frame) { + vector> answer; + board.assign(n + 1, vector>(n + 1, vector(2, false))); //현재 설치 현황 + //시뮬레이션 + for (int i = 0; i < build_frame.size(); i++) { + int x = build_frame[i][0]; + int y = build_frame[i][1]; + int p = build_frame[i][2]; + int cmd = build_frame[i][3]; + if (!cmd) { //삭제 + board[x][y][p] = false; + //삭제할 수 없으면 + if (!checkRemovable(x, y, n)) { + board[x][y][p] = true; + } + } else if (validate(x, y, p, n)) { //설치 가능한 경우 + board[x][y][p] = true; + } + } + //결과 + for (int i = 0; i <= n; i++) { + for (int j = 0; j <= n; j++) { + for (int k = 0; k < 2; k++) { + if (board[i][j][k]) { + answer.push_back({i, j, k});; + } + } + } + } + return answer; +} + +int main() { + int n = 5; + vector> build_frame = {{1, 0, 0, 1}, + {1, 1, 1, 1}, + {2, 1, 0, 1}, + {2, 2, 1, 1}, + {5, 0, 0, 1}, + {5, 1, 0, 1}, + {4, 2, 1, 1}, + {3, 2, 1, 1}}; + vector> ans = solution(n, build_frame); + for (int i = 0; i < ans.size(); i++) { + for (int j = 0; j < ans[0].size(); j++) { + cout << ans[i][j] << ' '; + } + cout << '\n'; + } + return 0; +} \ No newline at end of file