From 8074492e975ad15952e97e8ef83bfb2a98a32006 Mon Sep 17 00:00:00 2001 From: minkyu Date: Fri, 11 Feb 2022 15:30:31 +0900 Subject: [PATCH 01/12] =?UTF-8?q?=EC=A2=85=EB=A7=8C=EB=B6=81=20=EC=95=8C?= =?UTF-8?q?=EB=9F=AC=EC=A7=80=20=EC=B4=88=EA=B8=B0=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../APSS_ALLERGY_minkyu.cpp | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 4. Combinatorial search/APSS_ALLERGY_minkyu.cpp diff --git a/4. Combinatorial search/APSS_ALLERGY_minkyu.cpp b/4. Combinatorial search/APSS_ALLERGY_minkyu.cpp new file mode 100644 index 0000000..19a37cc --- /dev/null +++ b/4. Combinatorial search/APSS_ALLERGY_minkyu.cpp @@ -0,0 +1,69 @@ +#include +#define endl '\n' + +using namespace std; +using matrix = vector >; +int T, n, m; +int MAX_DEPTH; +// nameIndex[name] : table에서 해당 이름을 가진 사람의 행 번호 저장 +map nameIndex; +// table[i] : i번째 음식을 먹을 수 있는 사람들의 리스트 +matrix foodEater; +// 현재 상태 +vector edible; + +void select(int food) { + for (int saram: foodEater[food]) { + edible[saram]++; + } +} + +void unselect(int food) { + for (int saram: foodEater[food]) { + edible[saram]--; + } +} + +// start 번 음식부터 시작 +void solve(int start, int depth) { + if (depth >= MAX_DEPTH) return; + if (find(edible.begin(), edible.end(), 0) == edible.end()) { + MAX_DEPTH = depth; + return; + } + PASS: + for (int i = start; i < m; i++) { + select(i); + solve(i+1, depth+1); + unselect(i); + } +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(nullptr); + cout.tie(nullptr); + cin >> T; + while(T--) { + MAX_DEPTH = 21; + cin >> n >> m; + foodEater = matrix(m); + edible = vector(n, 0); + for (int i = 0; i < n; i++) { + string name; + cin >> name; + nameIndex[name] = i; + } + for (int i = 0; i < m; i++) { + int num; + cin >> num; + while(num--) { + string name; + cin >> name; + foodEater[i].push_back(nameIndex[name]); + } + } + solve(0, 0); + cout << MAX_DEPTH << endl; + } +} \ No newline at end of file From 5512d29246cb334afdad6413ce2b6bf8693e0928 Mon Sep 17 00:00:00 2001 From: minkyu Date: Fri, 11 Feb 2022 15:31:02 +0900 Subject: [PATCH 02/12] =?UTF-8?q?=EC=A2=85=EB=A7=8C=EB=B6=81=20=EC=95=8C?= =?UTF-8?q?=EB=9F=AC=EC=A7=80=20=EC=B4=88=EA=B8=B0=20=EA=B5=AC=ED=98=842?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 4. Combinatorial search/APSS_ALLERGY_minkyu.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/4. Combinatorial search/APSS_ALLERGY_minkyu.cpp b/4. Combinatorial search/APSS_ALLERGY_minkyu.cpp index 19a37cc..337a503 100644 --- a/4. Combinatorial search/APSS_ALLERGY_minkyu.cpp +++ b/4. Combinatorial search/APSS_ALLERGY_minkyu.cpp @@ -25,18 +25,17 @@ void unselect(int food) { } // start 번 음식부터 시작 -void solve(int start, int depth) { - if (depth >= MAX_DEPTH) return; +void solve(int start, int num) { + if (num >= MAX_DEPTH) return; if (find(edible.begin(), edible.end(), 0) == edible.end()) { - MAX_DEPTH = depth; + MAX_DEPTH = num; return; } - PASS: - for (int i = start; i < m; i++) { - select(i); - solve(i+1, depth+1); - unselect(i); - } + if (start == m) return; + solve(start+1, num); + select(start); + solve(start+1, num+1); + unselect(start); } int main() { From f320e5136e75b08118dae6fa69f3fe7be1c69373 Mon Sep 17 00:00:00 2001 From: minkyu Date: Fri, 11 Feb 2022 15:42:53 +0900 Subject: [PATCH 03/12] =?UTF-8?q?=EC=A2=85=EB=A7=8C=EB=B6=81=20=EC=95=8C?= =?UTF-8?q?=EB=9F=AC=EC=A7=80:=20=ED=9C=B4=EB=A6=AC=EC=8A=A4=ED=8B=B1=20?= =?UTF-8?q?=EC=A0=81=EC=9A=A9=20=ED=9B=84=20=EC=8B=9C=EA=B0=84=EC=B4=88?= =?UTF-8?q?=EA=B3=BC=20=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../APSS_ALLERGY_minkyu.cpp | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/4. Combinatorial search/APSS_ALLERGY_minkyu.cpp b/4. Combinatorial search/APSS_ALLERGY_minkyu.cpp index 337a503..6995c7d 100644 --- a/4. Combinatorial search/APSS_ALLERGY_minkyu.cpp +++ b/4. Combinatorial search/APSS_ALLERGY_minkyu.cpp @@ -7,11 +7,17 @@ int T, n, m; int MAX_DEPTH; // nameIndex[name] : table에서 해당 이름을 가진 사람의 행 번호 저장 map nameIndex; -// table[i] : i번째 음식을 먹을 수 있는 사람들의 리스트 +// foodEater[i] : i번째 음식을 먹을 수 있는 사람들의 리스트 matrix foodEater; +// eaterFood[i] : i번째 사람이 먹을 수 있는 음식 리스트 +matrix eaterFood; // 현재 상태 vector edible; +int search(int start) { + return distance(edible.begin(), find(edible.begin(), edible.end(), 0)); +} + void select(int food) { for (int saram: foodEater[food]) { edible[saram]++; @@ -31,11 +37,12 @@ void solve(int start, int num) { MAX_DEPTH = num; return; } - if (start == m) return; - solve(start+1, num); - select(start); - solve(start+1, num+1); - unselect(start); + int inedible = search(start); + for (int food: eaterFood[inedible]) { + select(food); + solve(food+1, num+1); + unselect(food); + } } int main() { @@ -47,6 +54,7 @@ int main() { MAX_DEPTH = 21; cin >> n >> m; foodEater = matrix(m); + eaterFood = matrix(n); edible = vector(n, 0); for (int i = 0; i < n; i++) { string name; @@ -59,7 +67,9 @@ int main() { while(num--) { string name; cin >> name; - foodEater[i].push_back(nameIndex[name]); + int person = nameIndex[name]; + foodEater[i].push_back(person); + eaterFood[person].push_back(i); } } solve(0, 0); From a082b6ca7a423870541021228a995c5e1ed0a42a Mon Sep 17 00:00:00 2001 From: minkyu Date: Fri, 11 Feb 2022 17:25:33 +0900 Subject: [PATCH 04/12] =?UTF-8?q?=EC=A2=85=EB=A7=8C=EB=B6=81=20DARPA=20?= =?UTF-8?q?=ED=92=80=EC=9D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../APSS_DARPA_minkyu.cpp" | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 "5. \354\265\234\354\240\201\355\231\224 \352\262\260\354\240\225 \353\254\270\354\240\234/APSS_DARPA_minkyu.cpp" diff --git "a/5. \354\265\234\354\240\201\355\231\224 \352\262\260\354\240\225 \353\254\270\354\240\234/APSS_DARPA_minkyu.cpp" "b/5. \354\265\234\354\240\201\355\231\224 \352\262\260\354\240\225 \353\254\270\354\240\234/APSS_DARPA_minkyu.cpp" new file mode 100644 index 0000000..f92033b --- /dev/null +++ "b/5. \354\265\234\354\240\201\355\231\224 \352\262\260\354\240\225 \353\254\270\354\240\234/APSS_DARPA_minkyu.cpp" @@ -0,0 +1,44 @@ +#include +#define endl '\n' + +using namespace std; + +int C, N, M; +vector settable; + +bool decision(int cameras, double gap) { + double limit = -1; + int installed = 0; + for (int i = 0; i < settable.size(); i++) { + if (limit <= settable[i]) { + installed++; + limit = settable[i] + gap; + } + } + return installed >= cameras; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(nullptr); + cout.tie(nullptr); + cin >> C; + cout << fixed; + cout.precision(2); + while (C--) { + cin >> N >> M; + settable = vector(M); + for (int i = 0; i < M; i++) { + cin >> settable[i]; + } + cout << endl; + double lo = 0, hi = 241; + double mid; + for (int i = 0; i < 100; i++) { + mid = (lo + hi) / 2.0; + if(decision(N, mid)) lo = mid; + else hi = mid; + } + cout << lo << endl; + } +} \ No newline at end of file From dbd678ef191b168290109b885d6eaee1e00ea22d Mon Sep 17 00:00:00 2001 From: minkyu Date: Sat, 12 Feb 2022 01:52:46 +0900 Subject: [PATCH 05/12] =?UTF-8?q?=EC=B5=9C=EC=A0=81=ED=99=94->=EA=B2=B0?= =?UTF-8?q?=EC=A0=95=20=EB=B2=95=EC=9C=BC=EB=A1=9C=20ARCTIC=20=ED=92=80?= =?UTF-8?q?=EC=9D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../APSS_ARTIC_minkyu.cpp" | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 "5. \354\265\234\354\240\201\355\231\224 \352\262\260\354\240\225 \353\254\270\354\240\234/APSS_ARTIC_minkyu.cpp" diff --git "a/5. \354\265\234\354\240\201\355\231\224 \352\262\260\354\240\225 \353\254\270\354\240\234/APSS_ARTIC_minkyu.cpp" "b/5. \354\265\234\354\240\201\355\231\224 \352\262\260\354\240\225 \353\254\270\354\240\234/APSS_ARTIC_minkyu.cpp" new file mode 100644 index 0000000..258c862 --- /dev/null +++ "b/5. \354\265\234\354\240\201\355\231\224 \352\262\260\354\240\225 \353\254\270\354\240\234/APSS_ARTIC_minkyu.cpp" @@ -0,0 +1,70 @@ +#include +#define endl '\n' + +using namespace std; +using matrix = vector >; + +struct Point { + double x, y; +}; + +int C, n; +vector base; +matrix dist; +vector visited; +vector decisioned; + +bool decision(int here, double gap) { + decisioned[here] = true; + visited[here] = true; + for (int i = 0; i < n; i++) { + if (dist[here][i] < gap) { + visited[i] = true; + } + } + if (find(visited.begin(), visited.end(), 0) != visited.end()) { + for (int i = 0; i < n; i++) { + if (visited[i] && !decisioned[i]) return decision(i, gap); + } + return false; + } + return true; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(nullptr); + cout.tie(nullptr); + cout << fixed; + cout.precision(2); + cin >> C; + while(C--) { + cin >> n; + base = vector(); + dist = matrix(n, vector(n, 0)); + for (int i = 0; i < n; i++) { + double x, y; + cin >> x >> y; + base.push_back(Point{x, y}); + } + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + if (i == j) continue; + double gap = pow(pow(abs(base[i].x-base[j].x), 2) + pow(abs(base[i].y-base[j].y), 2), 0.5); + dist[i][j] = gap; + dist[j][i] = gap; + } + } + + double lo = 0, hi = 2e6+1; + double mid; + for (int i = 0; i < 100; i++) { + visited = vector(n, 0); + decisioned = vector(n, 0); + mid = (lo + hi) / 2.0; + if (decision(0, mid)) hi = mid; + else lo = mid; + } + cout << mid << endl; + } +} \ No newline at end of file From 03e80de96695ca0f6f58bfe3618b56c296e04d60 Mon Sep 17 00:00:00 2001 From: minkyu Date: Tue, 15 Feb 2022 15:42:14 +0900 Subject: [PATCH 06/12] =?UTF-8?q?=EB=B0=B1=EC=A4=80=202470=20=ED=92=80?= =?UTF-8?q?=EC=9D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BJ_2470_minkyu.cpp" | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 "5. \354\265\234\354\240\201\355\231\224 \352\262\260\354\240\225 \353\254\270\354\240\234/BJ_2470_minkyu.cpp" diff --git "a/5. \354\265\234\354\240\201\355\231\224 \352\262\260\354\240\225 \353\254\270\354\240\234/BJ_2470_minkyu.cpp" "b/5. \354\265\234\354\240\201\355\231\224 \352\262\260\354\240\225 \353\254\270\354\240\234/BJ_2470_minkyu.cpp" new file mode 100644 index 0000000..e584669 --- /dev/null +++ "b/5. \354\265\234\354\240\201\355\231\224 \352\262\260\354\240\225 \353\254\270\354\240\234/BJ_2470_minkyu.cpp" @@ -0,0 +1,51 @@ +#include +#define endl '\n' + +using namespace std; + +int N; +vector liquid; + +bool decision(int sum) { + + return true; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(nullptr); + cout.tie(nullptr); + + cin >> N; + liquid = vector(N); + for (int i = 0; i < N; i++) cin >> liquid[i]; + + sort(liquid.begin(), liquid.end()); + + vector::iterator i = liquid.begin(), j = liquid.end()-1; + vector::iterator left = liquid.begin(), right = liquid.end()-1; + int gap = *i + *j; + int mingap = abs(gap); + while (i < j-1) { + if (gap > 0) { + j--; + gap = *i + *j; + if (mingap > abs(gap)) { + mingap = abs(gap); + left = i; + right = j; + } + } + else if (gap < 0) { + i++; + gap = *i + *j; + if (mingap > abs(gap)) { + mingap = abs(gap); + left = i; + right = j; + } + } + else break; + } + cout << *left << ' ' << *right << endl; +} \ No newline at end of file From 9c99a28e9dd6b117b54369b51511ad7c5d22be5c Mon Sep 17 00:00:00 2001 From: minkyu Date: Tue, 15 Feb 2022 16:56:34 +0900 Subject: [PATCH 07/12] =?UTF-8?q?=EB=B0=B1=EC=A4=80=201561=20=ED=92=80?= =?UTF-8?q?=EC=9D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BJ_1561_minkyu.cpp" | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 "5. \354\265\234\354\240\201\355\231\224 \352\262\260\354\240\225 \353\254\270\354\240\234/BJ_1561_minkyu.cpp" diff --git "a/5. \354\265\234\354\240\201\355\231\224 \352\262\260\354\240\225 \353\254\270\354\240\234/BJ_1561_minkyu.cpp" "b/5. \354\265\234\354\240\201\355\231\224 \352\262\260\354\240\225 \353\254\270\354\240\234/BJ_1561_minkyu.cpp" new file mode 100644 index 0000000..4d2ce2b --- /dev/null +++ "b/5. \354\265\234\354\240\201\355\231\224 \352\262\260\354\240\225 \353\254\270\354\240\234/BJ_1561_minkyu.cpp" @@ -0,0 +1,54 @@ +#include +#define endl '\n' + +using namespace std; + +int N, M; +vector rides; + +bool decision(long long n) { + long long ret = 0; + for (int i = 1; i <= M; i++) { + ret += ceil(n / (double)rides[i]); + } + return ret >= N; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(nullptr); + cout.tie(nullptr); + + cin >> N >> M; + rides = vector(M+1); + for (int i = 1; i <= M; i++) cin >> rides[i]; + + if (M >= N) { + cout << N; + return 0; + } + + // 아래 for문이 끝나면 모든 아이들이 빠짐없이 놀이기구를 타는데 hi분이 걸리는 것을 알 수 있음 + long long lo = 0; + long long hi = 1e15; + long long mid; + for (int i = 0; i < 100; i++) { + mid = (lo + hi) / 2; + if (decision(mid)) hi = mid; + else lo = mid; + } + + // 이제 hi분을 기준으로 가장 마지막에 탄 놀이기구가 무엇인지 찾으면 됨 + long long child = 0; + for (int i = 1; i <= M; i++) { + child += ceil(mid / (double)rides[i]); + } + for (int i = 1; i <= M; i++) { + if (mid % rides[i] == 0) { + if (++child == N) { + cout << i << endl; + return 0; + } + } + } +} \ No newline at end of file From a8839ce690fa8fc832c05ad5be8a2ba455e2c781 Mon Sep 17 00:00:00 2001 From: minkyu Date: Tue, 15 Feb 2022 17:49:08 +0900 Subject: [PATCH 08/12] =?UTF-8?q?=EB=B0=B1=EC=A4=80=201477=20=ED=92=80?= =?UTF-8?q?=EC=9D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BJ_1477_minkyu.cpp" | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 "5. \354\265\234\354\240\201\355\231\224 \352\262\260\354\240\225 \353\254\270\354\240\234/BJ_1477_minkyu.cpp" diff --git "a/5. \354\265\234\354\240\201\355\231\224 \352\262\260\354\240\225 \353\254\270\354\240\234/BJ_1477_minkyu.cpp" "b/5. \354\265\234\354\240\201\355\231\224 \352\262\260\354\240\225 \353\254\270\354\240\234/BJ_1477_minkyu.cpp" new file mode 100644 index 0000000..3a1f75e --- /dev/null +++ "b/5. \354\265\234\354\240\201\355\231\224 \352\262\260\354\240\225 \353\254\270\354\240\234/BJ_1477_minkyu.cpp" @@ -0,0 +1,49 @@ +#include +#define endl '\n' + +using namespace std; + +int N, M, L; +vector station; +vector distances; + +// 모든 간격이 low 이상으로 가능? +bool decision(double low) { + int howmany = 0; + for (int d: distances) { + howmany += ceil(d / low)-1; + } + return howmany <= M; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(nullptr); + cout.tie(nullptr); + + cin >> N >> M >> L; + if (N == 0) { + cout << ceil(L/(double)(M+1)) << endl; + return 0; + } + station = vector(N); + distances = vector(N+1); + for (int i = 0; i < N; i++) cin >> station[i]; + sort(station.begin(), station.end()); + distances[0] = station[0]; + for (int i = 1; i < N; i++) { + distances[i] = station[i] - station[i-1]; + } + distances[N] = L - station[N-1]; + + double lo = -1, hi = 2001; + double mid; + for (int i = 0; i < 100; i++) { + mid = (lo + hi) / 2; + if (decision(mid)) hi = mid; + else lo = mid; + } + + // 반드시 올림 해야됨 + cout << ceil(mid) << endl; +} \ No newline at end of file From 70bb875c199a243173612b554ae2daa1d3a32e46 Mon Sep 17 00:00:00 2001 From: minkyu Date: Tue, 15 Feb 2022 19:01:58 +0900 Subject: [PATCH 09/12] =?UTF-8?q?=EB=B0=B1=EC=A4=80=201939?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BJ_1939_minkyu.cpp" | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 "5. \354\265\234\354\240\201\355\231\224 \352\262\260\354\240\225 \353\254\270\354\240\234/BJ_1939_minkyu.cpp" diff --git "a/5. \354\265\234\354\240\201\355\231\224 \352\262\260\354\240\225 \353\254\270\354\240\234/BJ_1939_minkyu.cpp" "b/5. \354\265\234\354\240\201\355\231\224 \352\262\260\354\240\225 \353\254\270\354\240\234/BJ_1939_minkyu.cpp" new file mode 100644 index 0000000..187a7d1 --- /dev/null +++ "b/5. \354\265\234\354\240\201\355\231\224 \352\262\260\354\240\225 \353\254\270\354\240\234/BJ_1939_minkyu.cpp" @@ -0,0 +1,51 @@ +#include +#define endl '\n' + +using namespace std; +using matrix = vector >; + +int N, M; +int A, B, C; +matrix bridge; +int f1, f2; + +bool DFS(int start, int weight, vector& visited) { + if (start == f2) return true; + visited[start] = true; + + bool ret = false; + for (pair next: bridge[start]) { + if (!visited[next.first] && next.second >= weight) ret = DFS(next.first, weight, visited); + if (ret) return true; + } + + return false; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(nullptr); + cout.tie(nullptr); + + cin >> N >> M; + bridge = matrix(N+1); + + for (int i = 0; i < M; i++) { + cin >> A >> B >> C; + int& temp = bridge[A][B]; + temp = max(temp, C); + bridge[B][A] = temp; + } + cin >> f1 >> f2; + + + int lo = -1, hi = 1e9+1; + int mid; + for (int i = 0; i < 100; i++) { + mid = (lo + hi) / 2; + vector visited (N+1, false); + if(DFS(f1, mid, visited)) lo = mid; + else hi = mid; + } + cout << mid << endl; +} \ No newline at end of file From e0ed0d89b8dd063097a50bb308120641671b68d1 Mon Sep 17 00:00:00 2001 From: minkyu Date: Tue, 22 Feb 2022 01:35:46 +0900 Subject: [PATCH 10/12] =?UTF-8?q?=EC=A2=85=EB=A7=8C=EB=B6=81=20=EA=B3=A0?= =?UTF-8?q?=EB=8C=80=EC=96=B4=20=ED=92=80=EC=9D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 6. BFS, DFS/APSS_DICTIONARY_minkyu.cpp | 90 ++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 6. BFS, DFS/APSS_DICTIONARY_minkyu.cpp diff --git a/6. BFS, DFS/APSS_DICTIONARY_minkyu.cpp b/6. BFS, DFS/APSS_DICTIONARY_minkyu.cpp new file mode 100644 index 0000000..b837a02 --- /dev/null +++ b/6. BFS, DFS/APSS_DICTIONARY_minkyu.cpp @@ -0,0 +1,90 @@ +#include +#define endl '\n' + +using namespace std; + +int C, N; +vector > G; +vector inorder; +list result; +int numAlpha; + +int pop(list& q) { + int ret = q.back(); + q.pop_back(); + return ret; +} + +bool toplogical() { + list q; + for (int i = 0; i < 26; i++) { + if (inorder[i] == 0) q.push_back(i); + } + while(q.size() > 0) { + int here = pop(q); + for (int there: G[here]) { + if(--inorder[there] == 0) q.push_back(there); + } + result.push_back(here+'a'); + --numAlpha; + } + return numAlpha == 0; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(nullptr); + cout.tie(nullptr); + + cin >> C; + while (C--) { + G = vector >(26); + inorder = vector(26, -1); + result.clear(); + numAlpha = 0; + + cin >> N; + string before; + string now; + cin >> before; + + while (--N) { + cin >> now; + int size = min(before.size(), now.size()); + for (int i = 0; i < size; i++) { + if (before[i] != now[i]) { + int from = before[i] - 'a'; + int to = now[i] - 'a'; + G[from].push_back(to); + if (inorder[from] < 0) inorder[from] = 0; + if (inorder[to] < 0) { + inorder[to] = 1; + } + else ++inorder[to]; + break; + } + } + before = now; + } + + for (int i = 0; i < 26; ++i) { + if (inorder[i] < 0) { + result.push_back(i+'a'); + continue; + } + ++numAlpha; + } + + bool valid = toplogical(); + + if (valid) { + for (char c: result) cout << c; + cout << endl; + continue; + } else { + cout << "INVALID HYPOTHESIS" << endl; + continue; + } + + } +} \ No newline at end of file From d15ce550ea5cd5069df1ae771e1d98b886d839c8 Mon Sep 17 00:00:00 2001 From: minkyu Date: Wed, 23 Feb 2022 03:13:33 +0900 Subject: [PATCH 11/12] =?UTF-8?q?=EB=B0=B1=EC=A4=80=202021=20=ED=92=80?= =?UTF-8?q?=EC=9D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 6. BFS, DFS/BJ_2021_minkyu.cpp | 76 ++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 6. BFS, DFS/BJ_2021_minkyu.cpp diff --git a/6. BFS, DFS/BJ_2021_minkyu.cpp b/6. BFS, DFS/BJ_2021_minkyu.cpp new file mode 100644 index 0000000..f4fc1fb --- /dev/null +++ b/6. BFS, DFS/BJ_2021_minkyu.cpp @@ -0,0 +1,76 @@ +#include +#define endl '\n' + +using namespace std; +int N, L, src, dst; +vector > hosun; +vector > jiha; +vector hosunSeen; +vector jihaSeen; + +int pop(vector& q) { + int ret = q.back(); + q.pop_back(); + return ret; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(nullptr); + cout.tie(nullptr); + cin >> N >> L; + hosun = vector >(L+1); + hosunSeen = vector(L+1, false); + jiha = vector >(N+1); + jihaSeen = vector(N+1, false); + + for (int i = 0; i < L; ++i) { + int j; + cin >> j; + while(j > 0) { + hosun[i].push_back(j); + jiha[j].push_back(i); + cin >> j; + } + } + + cin >> src >> dst; + + int here = src; + vector q1; + vector q2; + int minimumTransfer = 0; + + for(int e: jiha[here]) { + q1.push_back(e); + } + + for (; ; ++minimumTransfer) { + if (q1.empty()) { + cout << -1 << endl; + return 0; + } + while(!q1.empty()) { + int hosun_num = pop(q1); + if (hosunSeen[hosun_num]) continue; + for (int jiha_num: hosun[hosun_num]) { + if (jihaSeen[jiha_num]) continue; + if (jiha_num == dst) goto FINISH; + q2.push_back(jiha_num); + } + hosunSeen[hosun_num] = true; + } + while(!q2.empty()) { + here = pop(q2); + if (jihaSeen[here]) continue; + for(int e: jiha[here]) { + if (hosunSeen[e]) continue; + q1.push_back(e); + } + jihaSeen[here] = true; + } + } + FINISH: + cout << minimumTransfer << endl; + return 0; +} \ No newline at end of file From 302b9c38343384af32481b975e795547f664fffd Mon Sep 17 00:00:00 2001 From: minkyu Date: Wed, 23 Feb 2022 03:39:40 +0900 Subject: [PATCH 12/12] =?UTF-8?q?=EB=B0=B1=EC=A4=80=203584=20=ED=92=80?= =?UTF-8?q?=EC=9D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 6. BFS, DFS/BJ_3584_minkyu.cpp | 48 ++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 6. BFS, DFS/BJ_3584_minkyu.cpp diff --git a/6. BFS, DFS/BJ_3584_minkyu.cpp b/6. BFS, DFS/BJ_3584_minkyu.cpp new file mode 100644 index 0000000..d06ea76 --- /dev/null +++ b/6. BFS, DFS/BJ_3584_minkyu.cpp @@ -0,0 +1,48 @@ +#include +#define endl '\n' + +using namespace std; + +int T, N; +vector parentOf; +vector seen; + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(nullptr); + cout.tie(nullptr); + cin >> T; + while(T--) { + cin>> N; + parentOf = vector(N+1, 0); + seen = vector(N+1, false); + int parent, child; + for (int i = 1; i < N; ++i) { + cin >> parent >> child; + parentOf[child] = parent; + } + int A, B; + cin >> A >> B; + parent = A; + while (parent) { + if (parent == B) { + cout << parent << endl; + goto FINISH; + } + seen[parent] = true; + parent = parentOf[parent]; + } + + parent = B; + while(parent) { + if (seen[parent]) { + cout << parent << endl; + goto FINISH; + } + parent = parentOf[parent]; + } + + FINISH: + continue; + } +} \ No newline at end of file