Algorithm๐Ÿฐ/๋ฐฑ์ค€

[๋ฐฑ์ค€] 5567 ๊ฒฐํ˜ผ์‹ ๊ตฌํ˜„

Jouureee 2021. 6. 13. 16:28

๋ฌธ์ œ :

https://www.acmicpc.net/problem/5567

 

5567๋ฒˆ: ๊ฒฐํ˜ผ์‹

2์™€ 3์€ ์ƒ๊ทผ์ด์˜ ์นœ๊ตฌ์ด๋‹ค. ๋˜, 3๊ณผ 4๋Š” ์นœ๊ตฌ์ด๊ธฐ ๋•Œ๋ฌธ์—, 4๋Š” ์ƒ๊ทผ์ด์˜ ์นœ๊ตฌ์˜ ์นœ๊ตฌ์ด๋‹ค. 5์™€ 6์€ ์นœ๊ตฌ๋„ ์•„๋‹ˆ๊ณ , ์นœ๊ตฌ์˜ ์นœ๊ตฌ๋„ ์•„๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 2,3,4 3๋ช…์˜ ์นœ๊ตฌ๋ฅผ ๊ฒฐํ˜ผ์‹์— ์ดˆ๋Œ€ํ•œ๋‹ค.

www.acmicpc.net

 

๋ถ„์„ :

๊ฒฐํ˜ผ์‹์— ์˜ฌ ๋™๊ธฐ๋“ค ์ค‘ ๋‚ด ์นœ๊ตฌ, ๋‚ด ์นœ๊ตฌ์˜ ์นœ๊ตฌ๋งŒ ์ดˆ๋Œ€ ํ•˜๋ ค๋ฉด ๋ช‡๋ช… ์ดˆ๋Œ€ ํ•ด์•ผ ๋ฌป๋Š” ๋ฌธ์ œ์˜€๋‹ค.

์นœ๊ตฌ ๊ด€๊ณ„๋ฅผ friends vector์— ๋„ฃ์–ด friends[์นœ๊ตฌ] = ["์นœ๊ตฌ์˜ ์นœ๊ตฌ๋“ค ๋ฆฌ์ŠคํŠธ"] ๋ฅผ ์ฐพ์œผ๋ฉด ๋˜๋Š” ๋ฌธ์ œ์ง€๋งŒ 

 

* ์ฃผ์˜ ํ•ด์•ผ ํ•  ์ ์ด ์žˆ๋‹ค.

์นœ๊ตฌ์˜ ์นœ๊ตฌ๊ฐ€ ๋‚ด ์นœ๊ตฌ์ผ ์ˆ˜๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

๋”ฐ๋ผ์„œ ์šฐ์„  ๋‚ด ์นœ๊ตฌ๋“ค๋งŒ queue์— ๋„ฃ์–ด result ๊ฐ’์„ ์ฆ๊ฐ€์‹œ์ผœ์ค€ ๋‹ค์Œ queue์— ์žˆ๋Š” ์นœ๊ตฌ๋ฅผ next๋กœ ์ฐพ์•„  

๋‚ด ์นœ๊ตฌ์˜ ์นœ๊ตฌ๊ฐ€ ์ด๋ฏธ ๋‚ด ์นœ๊ตฌ์ธ ๊ฒฝ์šฐ check ๋ฐฐ์—ด์—์„œ true๊ฐ€ ๋˜์—ˆ์„ ๊ฒƒ์ด๋ฏ€๋กœ if ๋ฌธ์— ๊ฑธ๋ฆฌ์ง€ ์•Š๊ฒŒ ๋งŒ๋“ค์—ˆ๋‹ค.

๊ทธ๋ž˜์„œ ์นœ๊ตฌ์˜ ์นœ๊ตฌ๊ฐ€ ์ด๋ฏธ ๋‚ด ์นœ๊ตฌ๋กœ main ํ•จ์ˆ˜์—์„œ result ๊ฐ’์ด ์ฆ๊ฐ€ ๋˜์—ˆ๋Š”๋ฐ find_real_friends์— ์ค‘๋ณต๋˜์ง€ ์•Š๊ฒŒ ํ•ด์คฌ๋‹ค !

 

 

c++ ์ฝ”๋“œ :

//
//  5567_wedding.cpp
//  SOMA๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป
//
//  Created by JoSoJeong on 2021/05/07.
//

#include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
#include <string.h>

using namespace std;
int N;
int Tcase;
int result = 0;
vector<int> friends[501];
int check[501];
queue<pair<int, int>> q;

void find_real_friends(){
    while(!q.empty()){
        int next;
        if(q.front().first == 1){
            next = q.front().second; // ์นœ๊ตฌ
        }else{
            next = q.front().first; // ์นœ๊ตฌ
        }
      
        check[next] = true;
         
        for(int i = 0; i < friends[next].size(); i++){ // ์นœ๊ตฌ์˜ ์นœ๊ตฌ
            if(check[friends[next][i]] == false){
                check[friends[next][i]] = true;
                result++;
            }
        }
        q.pop();
    }
}

int main(){
    cin >> N;
    cin >> Tcase;
    memset(check, false, sizeof(check));
    for(int i = 1; i <= Tcase; i++){
        int a, b;
        cin >> a >> b;
        if(a == 1 || b == 1){
            q.push({a, b}); // ๋‚ด ์นœ๊ตฌ๋“ค๋งŒ push
            check[a] = check[b] = true;
            result++;
        }
        
        friends[a].push_back(b);
        friends[b].push_back(a);
 
        
    }

    find_real_friends();
    cout << result << '\n';
    return 0;
}