Skip to main content

Cactus

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"; } πŸ”’ κ΅¬ν˜„ μ½”λ“œ 잠금

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"; } πŸ”’ κ΅¬ν˜„ μ½”λ“œ 잠금