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
80 changes: 80 additions & 0 deletions 12월 7일/23059.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <iostream>
#include <vector>
#include <queue>
#include <set>
#include <unordered_map>

using namespace std;

unordered_map<string, int> indegree;
unordered_map<string, vector<string>> graph;

//��������
vector<string> topologicalSort(set<string>& item) {
vector<string> result; //result�迭
priority_queue<string, vector<string>, greater<>> pq, pq_temp; //�켱���� ť

for (auto iter = item.begin(); iter != item.end(); iter++) { //������ Ȯ���ϸ鼭
if (indegree.find(*iter) == indegree.end()) //���������� �������� �ʴ´ٸ�(0 �̶��)
pq.push(*iter); //ť�� �ִ´�.
}
while (!pq.empty()) { //��������ʴٸ�
string node = pq.top(); //node�� top(�����ִ� ���� �ְ�)
pq.pop(); //ť���� �����Ѵ�

result.push_back(node); //���� ���� ������ ����
for (int i = 0; i < graph[node].size(); i++) { //�� �������� ����� �������Ȯ��
string next_node = graph[node][i]; //���� ��� �湮
indegree[next_node]--; //����� ������ ���������� 1�� ����
if (indegree[next_node] == 0) //����� ������ ���������� 0�� �Ǿ��ٸ�
pq_temp.push(next_node); //�켱 pq_temp�� ����
}
if (pq.empty() && !pq_temp.empty()) //������ ���������� 0�̿��� ������ ��� Ž���߰�, ���� Ž���� ������ �ִٸ�
swap(pq, pq_temp); //swap
}
return result; //result�迭 ��ȯ
}

/**
* ���ڿ��� �ε����� ����ؾ� �ϱ⶧���� map�� ���
*
* !����! �Ϲ� map�� ����ϸ� �ð��ʰ�. ���� map���� �˻� �ӵ��� ���� unordered_map ����ؾ� ��
* -> map�� ���� Ž�� Ʈ�� ���·� key���� �����ؼ� �ð� ���⵵�� logN ������, unordered_map�� hash���·� �����ؼ� �ð� ���⵵�� 1�̴�
* !����! ���� ������ �� �� ������ ���� ����� ���������� �ش� ������ ������ġ�� �߿� (������ ���� 3 ����)
* 1. ���������� 0�� �����鳢�� �����ؼ� ����ؾ� ��
* 2. ���������� 0�� ������ ���ÿ� ���� ���� ��� �ش� �������� ���� ������ ��ġ�ؾ� ��.
* -> ���� �켱���� ť�� ����Ͽ� ���������� 0�� �������� ����. �̶� ���ÿ� ���������� 0�� �� �������� ���� ��ġ�ؾ� �ϹǷ� �켱���� ť�� 2�� ���.
*/

int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);

set<string> item; //�����ϴ� ������(����)
int n;
string a, b;

//�Է�
cin >> n;
while (n--) { //�Է¼���ŭ ��� �Է�
cin >> a >> b; //a < b
indegree[b]++; //�������� ����
graph[a].push_back(b); //�ܹ��� �׷���
item.insert(a); //������ ����
item.insert(b); //������ ����
}

//����
vector<string> result = topologicalSort(item);

//���
if (result.size() != item.size()) {
cout << "-1\n"; //-1���
return 0; //��
}
for (int i = 0; i < result.size(); i++) //�迭 ũ�⸸ŭ ���鼭
cout << result[i] << '\n'; //�迭 ���

return 0; //��
}
66 changes: 66 additions & 0 deletions 12월 7일/2623.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

//��������
vector<int> topologicalSort(int n, vector<int>& indegree, vector<vector<int>>& graph) {
vector<int> result; //�迭����
queue<int> q; //ť ����

for (int i = 1; i <= n; i++) { //�ݺ��� ���鼭
if (!indegree[i]) //���������� 0�̶��
q.push(i); //ť�� �ֱ�
}

while (!q.empty()) { //�������������
int node = q.front(); //���� ���� ���� ���� ������
q.pop(); //pop�Ͽ� ����

result.push_back(node); //���� ���� ������ ����
for (int i = 0; i < graph[node].size(); i++) { //�׷����� �� ���鼭
int next_node = graph[node][i]; //���� ��带 ã��
indegree[next_node]--; //����� ������ ���������� 1�� ����
if (!indegree[next_node]) //����� ������ ���������� 0�� �Ǿ��ٸ�
q.push(next_node); //next_node�� ť�� �ִ´�.
}
}
return result;//����������� ���Ե� �迭 ��ȯ
}

/**
* �⺻���� ���� ���� ����
* ���� ������ �ٷ� �� ���� ������ ���İ��踦 ����
* ������ ���ϴ� ���� �Ұ����� ���(���������� �� �� ���� ���)�� ��� ������ Ž������ ���� ���
*/

int main() {
int n, m, cnt, prev, cur; //�ʱ�ȭ

//�Է�
cin >> n >> m; //�Է�
vector<int> indegree(n + 1, 0); //�迭 �ʱ�ȭ
vector<vector<int>> graph(n + 1, vector<int>(0)); //�׷��� �ʱ�ȭ
while (m--) { //m���� ��ŭ
cin >> cnt; //��� ������ ��
cin >> prev; //ó�� ���� �Է�
while (--cnt) { //������ ����
cin >> cur; //�Է�
indegree[cur]++; //�������� ����
graph[prev].push_back(cur);//�׷����� �ֱ�
prev = cur; //����
}
}

//����
vector<int> result = topologicalSort(n, indegree, graph); //���������Ѱ��� result�� ��ȯ�ޱ�

//���
if (result.size() != n) { //������ ���ϴ� ���� �Ұ��� ���
cout << "0\n"; //0�� ����ϰ�
return 0; //��
}
for (int i = 0; i < result.size(); i++) //�迭���� ����ؼ�
cout << result[i] << '\n'; //���ĵ� ���� �����ش�.
}
64 changes: 64 additions & 0 deletions 12월 7일/2637.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <iostream>
#include <vector>
#include <queue>

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

//�������� + DP
vector<vector<int>> topologicalSort(int n, vector<int>& indegree, vector<vector<ci>>& graph) {
queue<int> q; //ť �迭
vector<vector<int>> dp(n + 1, vector<int>(n + 1, 0)); //�� ��ǰ�� �� �� �ʿ����� ����

for (int i = 1; i <= n; i++) { //�ݺ��ϸ鼭 �������� Ȯ��
if (!indegree[i]) { //���������� 0�̶�� -> �⺻ ��ǰ
q.push(i); //ť �ֱ�
dp[i][i] = 1; //�⺻ ��ǰ �ʱ�ȭ
}
}
while (!q.empty()) { //ť ��������ʴٸ�
int node = q.front(); //���� ���� ���� ���� ������
q.pop(); //ť���� �����Ѵ�

for (int i = 0; i < graph[node].size(); i++) { //�׷��� �� ���鼭
int next_node = graph[node][i].first; //���� ��带 ã�Ƽ� Ȯ���Ѵ�
int cnt = graph[node][i].second; //�ʿ��� ��ǰ ��
indegree[next_node]--; //����� ������ ���������� 1�� ����
if (!indegree[next_node]) //����� ������ ���������� 0�� �Ǿ��ٸ�
q.push(next_node); //ť���� ����

for (int j = 1; j <= n; j++) //�ݺ��� ���鼭
dp[next_node][j] += dp[node][j] * cnt; //(���� ������ �̷�� ��ǰ ���� * �ʿ��� ���� ������ ����)�� ����
}
}
return dp; //dp��ȯ
}

/**
* �� ��ǰ���� ������ �ʿ��� ��ǰ ������ �����ϱ� ���� 2���� �迭 ��� (��: ��ǰ, ��: ���� �̷�µ� �� ��ǰ�� �ʿ��� ����)
* ���� ���� ������ ����, �̾��� ���� ������ ��ǰ�� ������ (���� ������ �̷�� ��ǰ�� ���� * �ʿ��� ���� ���� ��)�� �����ָ� ����
*/

int main() {
int n, m, x, y, k; //�ʱ�ȭ

//�Է�
cin >> n >> m;
vector<vector<ci>> graph(n + 1, vector<ci>(0)); //�׷��� �ʱ�ȭ
vector<int> indegree(n + 1, 0); //�������� �ʱ�ȭ
while (m--) { //m����ŭ �Է�
cin >> x >> y >> k; //y -> x
indegree[x]++; //�������� ����
graph[y].push_back({ x, k }); //�׷����� �ֱ�
}

//����
vector<vector<int>> result = topologicalSort(n, indegree, graph);

//���
for (int i = 1; i <= n; i++) {
if (result[i][i]) //�⺻ ��ǰ�̶��
cout << i << ' ' << result[n][i] << '\n'; //���
}
return 0; //��
}
76 changes: 76 additions & 0 deletions 12월 7일/3085.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

vector<vector<char>> board;
int dr[2] = { 1, 0 };
int dc[2] = { 0, 1 };

int cntCandy(int n, int row, int col, int dir) {
int ans = 0, cnt = 0; //�ʱ�ȭ
char cur = ' '; //�ʱ�ȭ
for (int i = 0; i < n; i++) { //�Էµ� ����ŭ �ݺ����� ���鼭
if (cur == board[row][col]) { //���ӵ� ����
cnt++; //cnt�� ��������
ans = max(ans, cnt); //�ִ밪�� ans�� �ִ´�
}
else { //�ҿ���
cnt = 1; //cntȽ���� �ʱ�ȭ ��Ű��
cur = board[row][col]; //���簪�� cur�� �ִ´�.
}
row += dr[dir]; //���� �ٲٱ�
col += dc[dir]; //�� ���� �ٲٱ�
}
return ans; //ans����ȯ(ĵ�� ����)
}

int findCandy(int n) {
int ans = 0; //�ʱ�ȭ
for (int i = 0; i < n; i++) { //�ݺ����� ���鼭
ans = max(ans, cntCandy(n, 0, i, 0)); //���� ���� ����
ans = max(ans, cntCandy(n, i, 0, 1)); //���� �࿡ ����
}
return ans; //cntCandy�� ���� ���� max���� ��
}

int switchCandy(int n, int row, int col, char candy) {
int ans = 0; //�ʱ�ȭ
for (int i = 0; i < 2; i++) { //������, �Ʒ��� �ִ� ������ �ٲ㺸��
int nr = row + dr[i], nc = col + dc[i]; //��,�� ���� �ٲٱ�
if (nr < n && nc < n && candy != board[nr][nc]) { //���������ְ� board�� ���� ���̸�
swap(board[row][col], board[nr][nc]); //���ο� �Ͱ� �������� �ٲٰ�
ans = max(ans, findCandy(n)); //ans���� �����ѵ�
swap(board[row][col], board[nr][nc]); //�ٽ� �����Ͱ� ���ο���� swap�Ѵ�
}
}
return ans; //���� ���ŵ� ans���� ��ȯ�Ѵ�.
}

/**
* �Է� ������ ũ�� �����Ƿ� �ٲ� �� �ִ� ������ ��� �ٲ㺸�� ���� �� �ִ� ���� ���
* ������, �Ʒ��� �ִ� �������� �ٲ㵵 ��� ��� ���� ����(����, �� ���� X)
*
* 1. ������ ���� �ٸ� ������ ��ȯ�ϱ�
* 2. �� ��, ���� ��� ���� ������ �� ������ ������ ���ŵ��� �ʵ��� ���� (ans ������ line 18~21���� �ϴ� ���)
*/
int main() {
int n, max_candy = 0; //�ʱ�ȭ

//�Է�
cin >> n;
board.assign(n, vector<char>(n, ' '));//board�� �Է��ϱ����� �ʱ�ȭ ��Ų��.
for (int i = 0; i < n; i++) //�ݺ������鼭
for (int j = 0; j < n; j++) //board��
cin >> board[i][j]; //�Է��Ѵ�.

//����
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) //�ݺ��� ���鼭
max_candy = max(max_candy, switchCandy(n, i, j, board[i][j])); //max���� ��
}

//���
cout << max_candy;
}
Loading