๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/๋ฐฑ์ค€(BOJ)315

[๋ฐฑ์ค€,c++] 9489๋ฒˆ - ์‚ฌ์ดŒ ๋ฌธ์ œ 9489๋ฒˆ: ์‚ฌ์ดŒ ์ž…๋ ฅ์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. ๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ์ฒซ์งธ ์ค„์—๋Š” ๋…ธ๋“œ์˜ ์ˆ˜ n๊ณผ ์‚ฌ์ดŒ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•ด์•ผ ํ•˜๋Š” ๋…ธ๋“œ์˜ ๋ฒˆํ˜ธ k๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ n ≤ 1,000, 1 ≤ k ≤ 1,000,000) ๋‹ค์Œ ์ค„ www.acmicpc.net ์ฝ”๋“œ #include #include #include using namespace std; int graph[1000001]; vector idx; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); int n, k; while(cin>>n>>k){ if(n==0 && k==0) break; int parent_idx = -1, before = 0, ans = 0; for(int i.. 2022. 7. 26.
[๋ฐฑ์ค€,c++] 20924๋ฒˆ - ํŠธ๋ฆฌ์˜ ๊ธฐ๋‘ฅ๊ณผ ๊ฐ€์ง€ ๋ฌธ์ œ 20924๋ฒˆ: ํŠธ๋ฆฌ์˜ ๊ธฐ๋‘ฅ๊ณผ ๊ฐ€์ง€ ์ฒซ ๋ฒˆ์งธ ์ค„์—๋Š” ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜ $N$($1 \le N \le 200\,000$)๊ณผ ๋ฃจํŠธ ๋…ธ๋“œ์˜ ๋ฒˆํ˜ธ $R$($1 \le R \le N$)์ด ์ฃผ์–ด์ง„๋‹ค. ์ดํ›„ $N-1$๊ฐœ์˜ ์ค„์— ์„ธ ๊ฐœ์˜ ์ •์ˆ˜ $a$, $b$, $d$($1 \le a, b \le N$, $ a \ne b$)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ด๋Š” $a$๋ฒˆ www.acmicpc.net ์ฝ”๋“œ #include #include #include using namespace std; vectortree[200001]; bool visited[200001]; int trunk_len, start_branch, branch_len, end_branch; void find_trunk(int node, int len){ visited[node.. 2022. 7. 24.
[๋ฐฑ์ค€,c++] 20365๋ฒˆ - ๋ธ”๋กœ๊ทธ2 ๋ฌธ์ œ 20365๋ฒˆ: ๋ธ”๋กœ๊ทธ2 neighbor ๋ธ”๋กœ๊ทธ๋ฅผ ์šด์˜ํ•˜๋Š” ์ผ์šฐ๋Š” ๋งค์ผ ์•„์นจ ํ’€๊ณ  ์‹ถ์€ ๋ฌธ์ œ๋ฅผ ๋ฏธ๋ฆฌ ์ •ํ•ด๋†“๊ณ  ๊ธ€์„ ์˜ฌ๋ฆฐ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋งค์ผ ๋ฐค ๊ฐ๊ฐ์˜ ๋ฌธ์ œ์— ๋Œ€ํ•˜์—ฌ, ํ•ด๊ฒฐํ•œ ๊ฒฝ์šฐ ํŒŒ๋ž€์ƒ‰, ํ•ด๊ฒฐํ•˜์ง€ ๋ชปํ•œ ๊ฒฝ์šฐ ๋นจ๊ฐ„์ƒ‰์œผ๋กœ ์น ํ•œ www.acmicpc.net ์ฝ”๋“œ #include #include #include using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); int N; cin>>N; string s; cin>>s; char before = 'B'; bool flag = true; int job = 1; for(int i=0; i 2022. 7. 24.
[๋ฐฑ์ค€,c++] 12933๋ฒˆ - ์˜ค๋ฆฌ ๋ฌธ์ œ 12933๋ฒˆ: ์˜ค๋ฆฌ ์ฒซ์งธ ์ค„์— ์˜์„ ์ด๊ฐ€ ๋…น์Œํ•œ ์†Œ๋ฆฌ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์†Œ๋ฆฌ์˜ ๊ธธ์ด๋Š” 5๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 2500๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๊ณ , 'q','u','a','c','k'๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. www.acmicpc.net ์ฝ”๋“œ #include #include using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); string inp; cin>>inp; int ans = 0; for(int i=0; i 2022. 7. 19.
[๋ฐฑ์ค€,c++] 1343๋ฒˆ - ํด๋ฆฌ์˜ค๋ฏธ๋…ธ ๋ฌธ์ œ 1343๋ฒˆ: ํด๋ฆฌ์˜ค๋ฏธ๋…ธ ์ฒซ์งธ ์ค„์— ์‚ฌ์ „์ˆœ์œผ๋กœ ๊ฐ€์žฅ ์•ž์„œ๋Š” ๋‹ต์„ ์ถœ๋ ฅํ•œ๋‹ค. ๋งŒ์•ฝ ๋ฎ์„ ์ˆ˜ ์—†์œผ๋ฉด -1์„ ์ถœ๋ ฅํ•œ๋‹ค. www.acmicpc.net ์ฝ”๋“œ #include #include using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); string s; cin>>s; s = regex_replace(s, regex("XXXX"), "AAAA"); s = regex_replace(s, regex("XX"), "BB"); if(s.find("X") != -1) cout 2022. 7. 14.
[๋ฐฑ์ค€,c++] 1967๋ฒˆ - ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„ ๋ฌธ์ œ 1967๋ฒˆ: ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„ ํŒŒ์ผ์˜ ์ฒซ ๋ฒˆ์งธ ์ค„์€ ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜ n(1 ≤ n ≤ 10,000)์ด๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ n-1๊ฐœ์˜ ์ค„์— ๊ฐ ๊ฐ„์„ ์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ๋“ค์–ด์˜จ๋‹ค. ๊ฐ„์„ ์— ๋Œ€ํ•œ ์ •๋ณด๋Š” ์„ธ ๊ฐœ์˜ ์ •์ˆ˜๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ •์ˆ˜๋Š” ๊ฐ„์„ ์ด ์—ฐ www.acmicpc.net ์ฝ”๋“œ #include #include #include using namespace std; int max_len = 0; int max_node; bool visited[10001]; int ans; vectorgraph[10002]; void dfs(int node, int len){ visited[node] = 1; if(max_len>N; for(int i=0; i>a>>b>>c; graph[a].push_back({b,c}); g.. 2022. 7. 11.
[๋ฐฑ์ค€,c++] 3584๋ฒˆ - ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๊ณตํ†ต ์กฐ์ƒ ๋ฌธ์ œ 3584๋ฒˆ: ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๊ณตํ†ต ์กฐ์ƒ ๋ฃจํŠธ๊ฐ€ ์žˆ๋Š” ํŠธ๋ฆฌ(rooted tree)๊ฐ€ ์ฃผ์–ด์ง€๊ณ , ๊ทธ ํŠธ๋ฆฌ ์ƒ์˜ ๋‘ ์ •์ ์ด ์ฃผ์–ด์งˆ ๋•Œ ๊ทธ๋“ค์˜ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๊ณตํ†ต ์กฐ์ƒ(Nearest Common Anscestor)์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜๋ฉ๋‹ˆ๋‹ค. ๋‘ ๋…ธ๋“œ์˜ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๊ณตํ†ต ์กฐ์ƒ์€, ๋‘ www.acmicpc.net ์ฝ”๋“œ #include #include #include using namespace std; bool visited[10001]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int T; cin >> T; while (T--) { int N; cin >> N; vectorgraph(N + 1); for (int i = 1; i > a >> b; g.. 2022. 7. 11.
[๋ฐฑ์ค€,c++] 14675๋ฒˆ - ๋‹จ์ ˆ์ ๊ณผ ๋‹จ์ ˆ์„  ๋ฌธ์ œ 14675๋ฒˆ: ๋‹จ์ ˆ์ ๊ณผ ๋‹จ์ ˆ์„  ํ”„๋กœ๊ทธ๋žจ์˜ ์ž…๋ ฅ์€ ํ‘œ์ค€ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›๋Š”๋‹ค. ์ž…๋ ฅ์˜ ์ฒซ ์ค„์—๋Š” ํŠธ๋ฆฌ์˜ ์ •์  ๊ฐœ์ˆ˜ N์ด ์ฃผ์–ด์ง„๋‹ค. (2 ≤ N ≤ 100,000) ํŠธ๋ฆฌ์˜ ์ •์ ์€ 1๋ฒˆ๋ถ€ํ„ฐ n๋ฒˆ๊นŒ์ง€ ์กด์žฌํ•œ๋‹ค. ๋‹ค์Œ ์ค„๋ถ€ํ„ฐ N-1๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ ๊ฐ„์„ ์˜ ์ • www.acmicpc.net ์ฝ”๋“œ #include #include using namespace std; vectorgraph; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); int N; cin>>N; graph.resize(N+1); for(int i=0; i>a>>b; graph[a].push_back(b); graph[b].push_back(a); } int q; cin>>q; for(int i=0;.. 2022. 6. 30.
[๋ฐฑ์ค€,c++] 6416๋ฒˆ - ํŠธ๋ฆฌ์ธ๊ฐ€? ๋ฌธ์ œ 6416๋ฒˆ: ํŠธ๋ฆฌ์ธ๊ฐ€? ํŠธ๋ฆฌ๋Š” ๊ต‰์žฅํžˆ ์ž˜ ์•Œ๋ ค์ง„ ์ž๋ฃŒ ๊ตฌ์กฐ์ด๋‹ค. ํŠธ๋ฆฌ๋ฅผ ๋งŒ์กฑํ•˜๋Š” ์ž๋ฃŒ ๊ตฌ์กฐ๋Š” ๋น„์–ด ์žˆ๊ฑฐ๋‚˜(๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜๊ฐ€ 0๊ฐœ), ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜๊ฐ€ 1๊ฐœ ์ด์ƒ์ด๊ณ  ๋ฐฉํ–ฅ ๊ฐ„์„ ์ด ์กด์žฌํ•˜๋ฉฐ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•ด์•ผ ํ•œ๋‹ค. www.acmicpc.net ์ฝ”๋“œ #include #include #include #include #include #define MAX 101 using namespace std; vectorindegree[MAX + 1]; unordered_map outdegree; // node, cnt unordered_set node; int edge_cnt = 0; int remove_leaf() { queue q; int remove_cnt = 0; for (auto cur : node) { .. 2022. 6. 30.