·405 words·2 mins
π λ¬Έμ μ 보 # λ§ν¬: https://www.acmicpc.net/problem/1734 π§ κ΄μ°° λ° μ κ·Ό # 무ν₯ κ·Έλν $G = (V, E)$κ° μ£Όμ΄μ§λ€.
μ¬κΈ°μ λκ°μ§ μΏΌλ¦¬κ° μ£Όμ΄μ§λ€.
κ°μ $e \in E$ νλλ₯Ό μμ΄μ λ, μ μ $A, B$μ μ°κ²°μ± νμ μ μ $v \in V$ νλλ₯Ό μμ΄μ λ, μ μ $A, B$μ μ°κ²°μ± νμ κ°κ° μ΄ν΄λ³΄μ. λ¨Όμ , λ¬Έμ 쑰건μ μν΄ μ»΄ν¬λνΈλ νλμ΄λ―λ‘ A, Bλ κΈ°λ³Έμ μΌλ‘ μ°κ²°λμ΄μλ€κ³ νλ¨νμ.
·118 words·1 min
π λ¬Έμ μ 보 # λ§ν¬: https://www.acmicpc.net/problem/14675 π§ κ΄μ°° λ° μ κ·Ό # νΈλ¦¬μμμ λ¨μ μ κ³Ό λ¨μ μ μ μκ°ν΄λ³΄μ. νΈλ¦¬μμ λͺ¨λ κ°μ μ λ¨μ μ μ΄λ€. μ¬μ΄ν΄ μλ μ°κ²° κ·ΈλνλκΉ νΈλ¦¬μμ 리νλ
Έλλ₯Ό μ μΈν λͺ¨λ λ
Έλλ λ¨μ μ μ΄λ€. μμ μ΄μ κ° κ°λ€. μ°νκ²½λ‘λ‘ μΈ back edgeκ° μλ€. π» νμ΄ # μ½λ (C++): void solve(){ cin >> N; rep(i, 0, N-1){ int u, v; cin >> u >> v; links[u].push_back(v); links[v].push_back(u); } cin >> Q; while(Q--){ int t, k; cin >> t >> k; if(t == 1) cout << ((int)links[k].size() == 1 ? "no\n" : "yes\n"); else cout << "yes\n"; } } π ꡬν μ½λ μ κΈ
·175 words·1 min
π λ¬Έμ μ 보 # λ§ν¬: https://www.acmicpc.net/problem/10891 π§ κ΄μ°° λ° μ κ·Ό # μ΄λ€ μ μ μ΄ μ΅λ νκ°μ μ¬μ΄ν΄μλ§ μν΄μλ€λ©΄, ν΄λΉ κ·Έλνλ μ μ μ μΈμ₯μ΄λ€. $\text{DP}[i]$ : μ μ $i$μ λΆλͺ¨μ μ μ $i$μ μμμ μλ tree edgeκ° μλ κ°μ μ μλΌκ³ μ μνμ. μ΄λ€ κ·Έλνκ° μ μ μ μΈμ₯μΌ νμμΆ©λΆμ‘°κ±΄μ $\forall i$ μ λν΄ $\text{DP}[i] \leq 1$μΈ κ²μ΄λ€. μ΄λ DFS+λμ ν©μ²λΌ κ³μ°ν μ μλ€. π» νμ΄ # μ½λ (C++): void dfs(int c, int p, int d){ depth[c] = d; par[c] = p; DP[c] = 0; for(auto n: links[c]){ if(n == p) continue; if(depth[n] == 0){ dfs(n, c, d+1); DP[c] += DP[n]; } else if(depth[n] < depth[c]) DP[c]++; else DP[par[c]]--; } } void solve(){ cin >> N >> M; rep(i, 0, M){ int u, v; cin >> u >> v; links[u].push_back(v); links[v].push_back(u); } dfs(1, -1, 1); bool isCactus = true; rep(i, 1, N+1) if(DP[i] > 1) isCactus = false; if(isCactus) cout << "Cactus"; else cout << "Not cactus"; } π ꡬν μ½λ μ κΈ
·175 words·1 min
π λ¬Έμ μ 보 # λ§ν¬: https://www.acmicpc.net/problem/10891 π§ κ΄μ°° λ° μ κ·Ό # μ΄λ€ μ μ μ΄ μ΅λ νκ°μ μ¬μ΄ν΄μλ§ μν΄μλ€λ©΄, ν΄λΉ κ·Έλνλ μ μ μ μΈμ₯μ΄λ€. $\text{DP}[i]$ : μ μ $i$μ λΆλͺ¨μ μ μ $i$μ μμμ μλ tree edgeκ° μλ κ°μ μ μλΌκ³ μ μνμ. μ΄λ€ κ·Έλνκ° μ μ μ μΈμ₯μΌ νμμΆ©λΆμ‘°κ±΄μ $\forall i$ μ λν΄ $\text{DP}[i] \leq 1$μΈ κ²μ΄λ€. μ΄λ DFS+λμ ν©μ²λΌ κ³μ°ν μ μλ€. π» νμ΄ # μ½λ (C++): void dfs(int c, int p, int d){ depth[c] = d; par[c] = p; DP[c] = 0; for(auto n: links[c]){ if(n == p) continue; if(depth[n] == 0){ dfs(n, c, d+1); DP[c] += DP[n]; } else if(depth[n] < depth[c]) DP[c]++; else DP[par[c]]--; } } void solve(){ cin >> N >> M; rep(i, 0, M){ int u, v; cin >> u >> v; links[u].push_back(v); links[v].push_back(u); } dfs(1, -1, 1); bool isCactus = true; rep(i, 1, N+1) if(DP[i] > 1) isCactus = false; if(isCactus) cout << "Cactus"; else cout << "Not cactus"; } π ꡬν μ½λ μ κΈ