Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[최소 신장 트리] 5월 30일 #16

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
57 changes: 57 additions & 0 deletions #18 0530_최소_신장_트리/BOJ_1244_sample.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <iostream>
#include <vector>

using namespace std;

//남학생의 스위치 바꾸기
vector<int> changeSwitchBoy(int n, int number, vector<int> switches) {
for (int i = number; i <= n; i += number) { //배수에 있는 스위치 바꾸기
switches[i] = !switches[i];
}
return switches;
}

//여학생의 스위치 바꾸기
vector<int> changeSwitchGirl(int n, int number, vector<int> 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<int> 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;
}
79 changes: 79 additions & 0 deletions #18 0530_최소_신장_트리/BOJ_16202_sample.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include <iostream>
#include <vector>
#include <algorithm>
#include <tuple>

using namespace std;
typedef tuple<int, int, int> tp;

vector<int> 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<tp> &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<tp> 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;
}
73 changes: 73 additions & 0 deletions #18 0530_최소_신장_트리/BOJ_1647_sample.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <iostream>
#include <vector>
#include <tuple>
#include <algorithm>

using namespace std;
typedef tuple<int, int, int> tp;

vector<int> 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<tp> &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<tp> 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;
}
95 changes: 95 additions & 0 deletions #18 0530_최소_신장_트리/BOJ_1774_sample.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include <iostream>
#include <vector>
#include <algorithm>
#include <tuple>
#include <cmath>

using namespace std;
typedef tuple<double, int, int> tp;
typedef pair<double, double> ci;

vector<int> 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<tp> &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<tp> edge;
vector<ci> 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;
}
Loading