Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

21-seongwon030 #78

Merged
merged 1 commit into from
Aug 4, 2024
Merged

21-seongwon030 #78

merged 1 commit into from
Aug 4, 2024

Conversation

seongwon030
Copy link
Collaborator

πŸ”— 문제 링크

트리

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

1μ‹œκ°„

✨ μˆ˜λ„ μ½”λ“œ

κ³Όμ •

리프 λ…Έλ“œλž€ μžμ‹μ΄ μ—†λŠ” λ…Έλ“œμ΄λ―€λ‘œ μ‚­μ œλ…Έλ“œκ°€ μ •ν•΄μ‘Œμ„ λ•Œ ν•΄λ‹Ή λ…Έλ“œλ₯Ό κΈ°μ€€μœΌλ‘œ ν•΄μ„œ μžμ‹ λ…Έλ“œλ₯Ό μž¬κ·€μ μœΌλ‘œ μ—†μ• λ©΄ λœλ‹€.

λ…Έλ“œ μ‚­μ œ

  1. μ‚­μ œλ…Έλ“œμ˜ 값을 λ…Έλ“œ λ²ˆν˜Έμ™€ κ²ΉμΉ˜μ§€ μ•ŠλŠ” κ°’μœΌλ‘œ μ„€μ •ν•œλ‹€.
  2. λ…Έλ“œ 개수만큼 λ°˜λ³΅ν•˜λ©° λ…Έλ“œμ˜ 값이 이전에 μ„€μ •ν•œμ˜λ―Έ μ—†λŠ” 값이라면 1번으둜 λŒμ•„κ°€ 같은 과정을 λ°˜λ³΅ν•œλ‹€.
  3. μœ„ 과정을 λ°˜λ³΅ν•˜λ©΄μ„œ μ‚­μ œλ…Έλ“œμ˜ μžμ‹λ…Έλ“œκΉŒμ§€ λͺ¨λ‘ μ—†μ•€λ‹€.
def dfs(node):
  k[node] = -2
  for i in range(n):
    if node == k[i]:
      dfs(i)

dfs(del_node)

λ¦¬ν”„λ…Έλ“œμ˜ 개수

λͺ¨λ“  λ…Έλ“œλ₯Ό μˆœνšŒν•˜λ©° 값이 의미 μ—†λŠ” 값이 μ•„λ‹ˆμ–΄μ•Ό ν•˜κ³  λ…Έλ“œ λ˜ν•œ λ…Έλ“œ λ¦¬μŠ€νŠΈμ— μ—†μ–΄μ•Ό ν•œλ‹€.

cnt = 0
for j in range(n):
  if k[j] != -2 and j not in k:
    cnt+=1

print(cnt)

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

dfs에 λŒ€ν•΄ 파 λ³Ό μ˜ˆμ •μž…λ‹ˆλ‹€.

Copy link
Collaborator

@yuyu0830 yuyu0830 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

풀이λ₯Ό λ³΄λ©΄μ„œ ꡳ이 DFSλ₯Ό ν†΅ν•΄μ„œ 트리λ₯Ό μ œκ±°ν•  ν•„μš”κ°€ μžˆμ„κΉŒ? 라고 μƒκ°ν–ˆμŠ΅λ‹ˆλ‹€. 트리λ₯Ό DFS둜 μ œκ±°ν•˜λŠ” λΆ€λΆ„κ³Ό μ•„λž˜μ— 트리λ₯Ό 탐색할 λ•Œ j not in k 이 뢀뢄이 쑰금 λΉ„νš¨μœ¨ 적인 것 κ°™μ•„μ„œ μ €λŠ” λ‹€λ₯Έ λ°©μ‹μœΌλ‘œ ν’€μ–΄λ΄€μ–΄μš” (n이 μ΅œλŒ€ 50이기 λ•Œλ¬Έμ— νš¨μœ¨μ„ λ”°μ§ˆ λ¬Έμ œκ°€ μ•„λ‹ˆκΈ΄ ν•©λ‹ˆλ‹€)

μ €λŠ” 트리λ₯Ό 받은 λ’€ λ£¨νŠΈλΆ€ν„° DFS둜 탐색을 ν•˜λ˜, 탐색 도쀑 ν˜„μž¬ 탐색할 λ…Έλ“œκ°€ μ‚­μ œν•  λ…Έλ“œ 번호라면 탐색을 μ€‘μ§€ν•˜λŠ” λ°©ν–₯으둜 트리 순회 ν•œλ²ˆλ§Œ ν•˜λ©΄ λ˜κ²Œλ” ν–ˆμŠ΅λ‹ˆλ‹€. 짜고 λ³΄λ‹ˆ λ³΅μž‘λ„ μžμ²΄λŠ” 크게 차이가 μ—†λŠ” 것 κ°™μ•„μš”..

#include "iostream"
#include "vector"

using namespace std;

vector<int> v[51];

int main() {
    int n, root; cin >> n;

    for (int i = 0; i < n; i++) {
        int t; cin >> t;
        if (t == -1) root = i;
        else v[t].push_back(i);
    }   

    int del, ans = 0; cin >> del;

    vector<int> s;
    if (root != del) s.push_back(root);

    while (!s.empty()) {
        int cur = s.back();
        s.pop_back();

        int cnt = 0;
        
        for (int i : v[cur]) {
            if (i == del) continue;
            s.push_back(i);
            cnt++;
        }
        
        if (!cnt) ans++;
    }

    printf("%d\n", ans);
}

@seongwon030
Copy link
Collaborator Author

seongwon030 commented Jul 30, 2024

문제 ν’€ λ•Œ n이 크지 μ•Šμ•„μ„œ λͺ¨λ“  λ…Έλ“œλ₯Ό νƒμƒ‰ν•˜λ„λ‘ ν–ˆλŠ”λ° n이 컀질수둝 λ…Έλ“œνƒμƒ‰μ„ μ€‘μ§€ν•˜λŠ” 방법이 훨씬 νš¨μœ¨μ μ΄κ² λ„€μš”. 덕뢄에 μ°Έκ³ ν•΄μ„œ λ‹€λ₯Έ λ¬Έμ œμ—λ„ μ μš©ν•  수 μžˆμ„ 것 κ°™μ•„μš”!

Copy link
Collaborator

@InSange InSange left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

주어진 인덱슀 κ°’μ—μ„œ ν•΄λ‹Ήν•˜λŠ” λ…Έλ“œ 인덱슀 값듀을 dfs둜 μ—°μ‡„μ‹œμΌœ λ¨Όμ € μ œκ±°ν›„ μ°¨λ‘€λŒ€λ‘œ νƒμƒ‰ν•˜λŠ”κ΅°μš”. μ΄λ ‡κ²Œ μ ‘κ·Όν•˜λŠ” 방식도 μžˆλ‹€λŠ” 것을 보고 κ°‘λ‹ˆλ‹€.

@seongwon030 seongwon030 merged commit cadb852 into main Aug 4, 2024
1 check passed
@seongwon030 seongwon030 deleted the 21-seongwon030 branch August 4, 2024 14:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants