BOJ 10891 Cactus? 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"; } π ꡬν μ½λ μ κΈ