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

[최소신장트리] 12월 7일 #21

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
68 changes: 68 additions & 0 deletions 11월 30일/1368.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <iostream>
#include <vector>
#include <queue>

using namespace std;
const int INF = 1e5 + 1; //��� ����

int prim(int size, int start, vector<vector<int>>& graph) {
int sum = 0; //�ʱ�ȭ
vector<int> dist(size, INF); //�� �������� ���
vector<bool> visited(size, false); //�� �湮 ����
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq; //�켱���� ť ����

//�ʱ�ȭ
dist[start] = 0;
pq.push({ 0, start });

while (!pq.empty()) {
int cost = pq.top().first; //���� ����ġ
int cur = pq.top().second; //���� ��
pq.pop(); //�����ϱ�

if (visited[cur]) //�̹� Ȯ���ߴ� ����
continue;
sum += cost; //MST ���� ����ġ ����
visited[cur] = true; //�湮 ó��

for (int i = 0; i < size; i++) { //�ݺ�������
if (!visited[i] && graph[cur][i] < dist[i]) { //�̹湮 �����̸鼭 �� ª�� ������ ���� �� �� �ִٸ�
dist[i] = graph[cur][i]; //������
pq.push({ dist[i], i }); //�켱���� ť�� �־��ֱ�
}
}
}
return sum; //��ȯ
}

/**
* �� ���� ������ ������ �����ϰ�, �칰�� �Ĵ� ��쵵 ����? -> ����
* ���� �߰��� ��� �칰�� ����Ǵ� ������ �ִٰ� ����!
* ->���� ���� �칰�� �Ĵ� ���� ������ �� �� ������ ���� ����ġ��� �� �� ����
*
* 0 2 2 2 5
* 2 0 3 3 4
* 2 3 0 4 4
* 2 3 4 0 3
* 5 4 4 3 0
*
* ��� 0 ~ n-1�� ��, ��� n�� ����
* 1�� �̻��� ���� �ݵ�� ���� �칰�� �ľ� �ϹǷ� ����(n)���� �����ϴ� ���� �˰�����
*/
int main() {
int n, w; //����

cin >> n; //�Է�
vector<vector<int>> graph(n + 1, vector<int>(n + 1, 0)); //���߹迭 �ʱ�ȭ
for (int i = 0; i < n; i++) { //��������� ���� ������� ���
cin >> w; //�Է�
graph[i][n] = graph[n][i] = w; //�׷��� ������ ǥ��
}

for (int i = 0; i < n; i++) { //�ݺ��� ���鼭 �׷��� ���(���) �Է�
for (int j = 0; j < n; j++)
cin >> graph[i][j]; //���� ���̿��� ���� ������� ���
}

cout << prim(n + 1, n, graph); //�������� �����ϴ� ���� �˰�����
}
87 changes: 87 additions & 0 deletions 11월 30일/16202.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#include <iostream>
#include <vector>
#include <algorithm>
#include <tuple>

using namespace std;
typedef tuple<int, int, int> tp; //Ʃ�� ����

vector<int> parent; //�θ� �迭

//Find ����
int findParent(int node) {
if (parent[node] < 0) //���� ������ ��Ʈ ����
return node; //node ��ȯ
return parent[node] = findParent(parent[node]); //�׷��� �����ϸ� ��Ʈ ���� ã��
}

//Union ����
bool unionInput(int x, int y) {
int xp = findParent(x); //�θ�ã��
int yp = findParent(y); //�θ�ã��

if (xp == yp) //���� ���տ� �ִٸ� ���Ͽ� �� �� ����
return false; //���Ͽ� �Ұ�
if (parent[xp] < parent[yp]) { //���ο� ��Ʈ xp
parent[xp] += parent[yp]; //��尳�� ǥ��
parent[yp] = xp; //xp�� ��
}
else { //���ο� ��Ʈ yp
parent[yp] += parent[xp]; //��尳�� ǥ��
parent[xp] = yp; //yp�� ��
}
return true; //���Ͽ�����
}

int kruskal(int n, int idx, vector<tp>& edges) {
int cnt = 0, sum = 0; //�ʱ�ȭ
for (int i = idx; i < edges.size(); i++) {
if (cnt == n - 1) //n-1���� ������ ��� ������
break;//�ߴ�
int dist = get<0>(edges[i]); //Ʃ�ð� �޾ƿ���
int x = get<1>(edges[i]);//Ʃ�ð� �޾ƿ���
int y = get<2>(edges[i]);//Ʃ�ð� �޾ƿ���

if (unionInput(x, y)) { //union�ϱ�
cnt++; //���� �߰�
sum += dist; //�Ÿ� ����
}
}
if (cnt < n - 1) //mst�� ���� �� ����
return 0; //0���� ǥ��
return sum; //�Ÿ� ��ȯ
}

/**
* MST �˰������� ���� �� �����ص� �ɱ�?
* 1. ũ�罺Į �˰������� �ð� ���⵵�� O(ElogE)
* �̴� ���� ������ �����ϴ� ������ �ð� ���⵵!
* ��, ��� ������ �� �� �����ؼ� �����صθ� ���� �� ���� �˰������� �����Ͽ��� ���� �ð��� ū ������ ����
* 2. ���� ������ ���� �켱���� ť�� �ƴ� ���Ϳ� �����ϰ� ũ�罺Į �˰����� k�� ����
* 3. �Ź� ũ�罺Į�� ������ ������ ���� ���� �߰��� ������ ������
* -> ���ܵ� ������ �迭�� 0��° �������� 1, 2, 3��° �������� ������ ����
* 4. ���� �� �� MST�� ���� �� ���ٴ°� Ȯ�εƴٸ� ���Ŀ��� MST�� ���� �� �����Ƿ� flag ������ ���ʿ��� ���� ����
*/
int main() {
int n, m, k, x, y; //�ʱ�ȭ

cin >> n >> m >> k; //�Է�
vector<tp> edges; //�����ҰŶ� �켱���� ť�� �ƴ� ���Ϳ� ����
for (int i = 1; i <= m; i++) { //�ݺ�
cin >> x >> y; //�Է�
edges.emplace_back(i, x, y); //Ʃ�ú��Ϳ� �־��ش�
}

bool flag = false; //flag
for (int i = 0; i < k; i++) { //�ݺ����鼭 mst�����
if (flag) { //���̻� mst�� ���� �� ����
cout << 0 << ' ';
continue;
}
parent.assign(n + 1, -1); //�ʱ�ȭ
int ans = kruskal(n, i, edges); //sum�ޱ�
if (ans == 0) //0�̸� mst�� ��������ٴ� ������ flagǥ��
flag = true; //true�� �ؼ� ������ conitnue�ϵ��� �����
cout << ans << ' '; //ans���
}
}
116 changes: 116 additions & 0 deletions 11월 30일/16235.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#include <iostream>
#include <vector>
#include <queue>
#include <deque>
#include <tuple>
#include <algorithm>

using namespace std;
typedef vector<vector<int>> matrix; //���߹迭
typedef tuple<int, int, int> tp; //Ʃ�� ��� 3��

queue<tp> spring(matrix& land, deque<tp>& tree, queue<pair<int, int>>& breeding_tree) {
queue<tp> dead_tree; //queue����
int size = tree.size(); //size ����
while (size--) { //��� ���� �˻�
int age = get<0>(tree.front()); //����
int row = get<1>(tree.front()); //��
int col = get<2>(tree.front()); //��
tree.pop_front(); //�տ� �ִ� ���� �����Ѵ�.

if (land[row][col] < age) { //�ڽ��� ���̸�ŭ ����� ���� �� ���ٸ�
dead_tree.push({ age, row, col }); //�׾��ٰ� ǥ��
continue; //����ϱ�
}
land[row][col] -= age; //���̸�ŭ �����ϱ�
tree.emplace_back(age + 1, row, col); //Ʈ���� ���� �߰�
if ((age + 1) % 5 == 0) //���̰� 5�� ������
breeding_tree.push({ row, col }); //���� �߰�
}
return dead_tree;
}

void summer(queue<tp>& dead_tree, matrix& land) {
while (!dead_tree.empty()) { //���� ������ �ִ� �迭�� ��������ʴٸ�
int age = get<0>(dead_tree.front()); //���� ������ ����
int row = get<1>(dead_tree.front()); //���� ������ �� ��ġ
int col = get<2>(dead_tree.front()); //���� ������ �� ��ġ
dead_tree.pop(); //�ϳ��� ����
land[row][col] += (age / 2); //land�� ����
}
}

void fall(int n, deque<tp>& tree, queue<pair<int, int>>& breeding_tree) {
int dr[8] = { -1, 1, 0, 0, -1, -1, 1, 1 }; //����ǥ�� �����¿� �밢��
int dc[8] = { 0, 0, -1, 1, -1, 1, -1, 1 }; //����ǥ��

while (!breeding_tree.empty()) {
int row = breeding_tree.front().first; //������ ������ ��
int col = breeding_tree.front().second; //������ ������ ��
breeding_tree.pop(); //���ij����� �ִ°� �ϳ�����

for (int j = 0; j < 8; j++) { //���⵹������
int nr = row + dr[j]; //��
int nc = col + dc[j]; //��
if (nr < 0 || nr >= n || nc < 0 || nc >= n) //�迭 ������ �Ѿ�ٸ� ����
continue; //�ݺ��� �����
tree.push_front({ 1, nr, nc }); //���� ���� ����
}
}
}

void winter(int n, matrix& a, matrix& land) {
for (int i = 0; i < n; i++) //�ݺ��� ���鼭 land ���� �߰�
for (int j = 0; j < n; j++) //���߹ݺ���
land[i][j] += a[i][j]; //land �߰�
}

/**
* [���� ����] - �ܼ� ���� ����
* ��: �ϳ��� ĭ���� ���̰� � �������� �ڽ��� ���̸�ŭ ����� �԰�, ���̰� 1 ������
* �� ĭ�� ����� ������ �ڽ��� ���̸�ŭ ����� �� �Դ� ������ ��� ����
* ����: ���� ���� ������ ������� ����. ���� �������� ���̸� 2�� ���� ���� ������� �߰� (�Ҽ��� ����)
* ����: ���̰� 5�� ����� ������ ����. ������ 8�� ĭ�� ���̰� 1�� ������ ����
* �ܿ�: �κ�(S2D2)�� ���� ���ƴٴϸ鼭 A[r][c]��ŭ �� ĭ�� ��� �߰�
*
* K���� ���� �� ���� ���� ����ִ� ������ ����
*
* [���� Ǯ��]
* a: �κ�(S2D2)�� �ܿ£ �ִ� ����� ��
* land: ���� ���
* breeding_tree: ���̰� 5�� ����� Ʈ�� (������ Ʈ��)
* tree: ���� ���� ���� ����, ��, �� ����
* -> deque �����̳ʸ� Ȱ���� ������ ������ �տ� �־��ָ� �Է� �Ŀ��� �����ؼ� ��� ����
*
* ������ ������� ������ ������ ����
*/

int main() {
int n, m, k, x, y, z;

//�Է�
cin >> n >> m >> k;
matrix a(n, vector<int>(n, 0)); //�ʱ�ȭ
matrix land(n, vector<int>(n, 5)); //ó�� ��� ��� ĭ�� 5
queue<pair<int, int>> breeding_tree; //������ Ʈ��
deque<tp> tree; //tree��ü
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
cin >> a[i][j]; //�ݺ��� ���鼭 �Է¹ޱ�
while (m--) {
cin >> x >> y >> z; //�Է�
tree.emplace_back(z, x - 1, y - 1); //(0, 0)���� �����ϵ��� �����ϱ����� 1�� ���� �ε����� ����
}

//����
sort(tree.begin(), tree.end()); //� ���� ������ ����
while (k--) { //k��ŭ �ݺ�����
queue<tp> dead_tree = spring(land, tree, breeding_tree); //���� ������ ���� ����
summer(dead_tree, land); //����
fall(n, tree, breeding_tree); //����
winter(n, a, land); //�ܿ�
}

//���
cout << tree.size(); //���� ũ�� ���
}
54 changes: 54 additions & 0 deletions 11월 30일/1713.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <iostream>
#include <vector>
#include <map>

using namespace std;
typedef pair<int, int> ci; //pair������

map<int, ci>::iterator delCandidate(map<int, ci>& candidate) {
auto del = candidate.begin(); //ó�� �ĺ��� �����Ѵ� ����
int cnt = candidate.begin()->second.first; //ó�� �ĺ��� ��õ Ƚ��
int t = candidate.begin()->second.second; //ó�� �ĺ��� �Խ� �ð�
for (auto iter = ++candidate.begin(); iter != candidate.end(); iter++) { //�ĺ� �ݺ����鼭
int cur_cnt = iter->second.first; //��õȽ��
int cur_t = iter->second.second; //�Խýð�
if (cur_cnt < cnt) { //��õ Ƚ���� ���� ���� �ĺ� ã��
cnt = cur_cnt; //��õ ȹ�� ����
t = cur_t; //�Խýð� ����
del = iter; //del�� �ֱ�
}
else if (cur_cnt == cnt && cur_t < t) { //��õ Ƚ���� ���� ���� �ĺ��� �������̶��, �Խ� �ð��� ������ �ĺ� ã��
t = cur_t; //�Խýð� ����
del = iter; //del�� �ֱ�
}
}
return del; //��ȯ
}

/**
* 1. ����ִ� ����Ʋ�� ���� ���, ���� ��õ���� ���� �л� �� �Խ� �ð��� ������ �л��� ����
* 2. �ĺ� �л��� �ٷ� ã�� ���� �� Ǯ�̴� map �����̳ʸ� ����� ����
*
* !����! �Խ� �ð� ���� ���� ��, �ĺ��� �ö� ���� ù �ð��� ����. �̹� �ĺ��� �ִµ� �Խ� �ð��� ���ŵ��� �ʵ��� ����.
*/

int main() {
int n, m, input; //��������

//�Է� & ����
cin >> n >> m;
map<int, ci> candidate; //first: �ĺ� �л�, second: {��õ Ƚ��, �Խ� �ð�}
for (int i = 0; i < m; i++) { //�ݺ��� ���鼭
cin >> input; //�Է¹ޱ�
if (candidate.size() == n && candidate.find(input) == candidate.end()) //����ִ� ����Ʋ�� ���� ���
candidate.erase(delCandidate(candidate)); //����

if (candidate.find(input) == candidate.end()) //ù �Խö��
candidate[input].second = i; //�ʱ�ȭ
candidate[input].first++; //��õ Ƚ�� ����
}

//���
for (auto iter = candidate.begin(); iter != candidate.end(); iter++)
cout << iter->first << ' '; //���� ���
}
Loading