728x90
728x90
728x90
728x90

문제 링크 : https://www.acmicpc.net/problem/4195

 

4195번: 친구 네트워크

첫째 줄에 테스트 케이스의 개수가 주어진다. 각 테스트 케이스의 첫째 줄에는 친구 관계의 수 F가 주어지며, 이 값은 100,000을 넘지 않는다. 다음 F개의 줄에는 친구 관계가 생긴 순서대로 주어진

www.acmicpc.net

 

문제

민혁이는 소셜 네트워크 사이트에서 친구를 만드는 것을 좋아하는 친구이다. 우표를 모으는 취미가 있듯이, 민혁이는 소셜 네트워크 사이트에서 친구를 모으는 것이 취미이다.

어떤 사이트의 친구 관계가 생긴 순서대로 주어졌을 때, 두 사람의 친구 네트워크에 몇 명이 있는지 구하는 프로그램을 작성하시오.

친구 네트워크란 친구 관계만으로 이동할 수 있는 사이를 말한다.

입력

첫째 줄에 테스트 케이스의 개수가 주어진다. 각 테스트 케이스의 첫째 줄에는 친구 관계의 수 F가 주어지며, 이 값은 100,000을 넘지 않는다. 다음 F개의 줄에는 친구 관계가 생긴 순서대로 주어진다. 친구 관계는 두 사용자의 아이디로 이루어져 있으며, 알파벳 대문자 또는 소문자로만 이루어진 길이 20 이하의 문자열이다.

출력

친구 관계가 생길 때마다, 두 사람의 친구 네트워크에 몇 명이 있는지 구하는 프로그램을 작성하시오.

 


접근 방법

처음에는 unordered_map으로 풀었으나 시간 초과가 났다. 매 입력마다 value의 값을 변경하는 부분에서 시간이 많이 소요된 것 같다. 그래서 union and find 방법으로 해결하였다. (참고 : https://chanhuiseok.github.io/posts/baek-21/)

 

 

//
//  Map_BOj4195_친구네트워크.cpp
//  Coding_Test_Practice
//
//  Created by 김난영 on 2021/06/30.
//  Copyright © 2021 KimNanyoung. All rights reserved.
//

#include <iostream>
#include <unordered_map>

using namespace std;

int sizeArr[200000];
int parent[200000];

int getParent(int x){
    if(x==parent[x]) return x;
    else return parent[x] = getParent(parent[x]);
}

int main(){
    
    cin.tie(NULL);
    ios_base::sync_with_stdio(false);
    
    int test_case = 0;
    cin >> test_case;
    for(int t=1; t<=test_case; t++){
        
        unordered_map<string,int> umap;
        
        //F : 0~99,999 -> 친구 수 : 0 ~ 199,998 -> idx = 1~199,999 -> arr크기 = 200,000

        for(int i = 0; i<200000; i++){
            parent[i] = i;
            sizeArr[i] = 1;
        }
        
        int nodeNum = 0;
        
        int F; cin >> F;
        for(int i = 1; i<=F; i++){
            
            string name1, name2;
            cin >> name1 >> name2;
            
            if(umap.find(name1) == umap.end()){
                umap[name1]  = nodeNum++;
                
            }
            if(umap.find(name2) == umap.end()){
                umap[name2] = nodeNum++;
            }
            
            //find
            int n1 = umap[name1];
            int n2 = umap[name2];
            int p1 = getParent(n1);
            int p2 = getParent(n2);
            
            //union
            if(p1!=p2) {
                if(sizeArr[p1] < sizeArr[p2]) swap(p1, p2);
                sizeArr[p1] += sizeArr[p2];
                parent[p2] = p1;
            }
        
            cout << sizeArr[p1] << "\n";
            
        }
        
    }
    
    
    return 0;
}

 

 

(시간초과)

//
//  Map_BOj4195_친구네트워크.cpp
//  Coding_Test_Practice
//
//  Created by 김난영 on 2021/06/30.
//  Copyright © 2021 KimNanyoung. All rights reserved.
//

#include <iostream>
#include <unordered_map>

using namespace std;


int main(){
    
    cin.tie(NULL);
    ios_base::sync_with_stdio(false);
    
    int test_case = 0;
    cin >> test_case;
    for(int t=1; t<=test_case; t++){
        
        unordered_map<string,int> umap;
        
        int F; cin >> F;
        for(int i = 0; i<F; i++){
            string name1, name2;
            cin >> name1 >> name2;
            
            unordered_map<string, int> :: iterator it;
            
            if(umap.find(name1)==umap.end() && umap.find(name2)==umap.end()){   //둘다 없으면
                umap[name1] = 2;
                umap[name2] = 2;
                cout << umap[name2] << "\n";
            }
            else if(umap.find(name1) != umap.end() && umap.find(name2) == umap.end()){  //1만 있음
                int cnt = umap[name1];
                for(it = umap.begin(); it!=umap.end(); it++){
                    it->second = cnt + 1;
                }
                umap[name2] = cnt+1;
                
                cout << umap[name2] << "\n";
            }
            else if(umap.find(name1) == umap.end() && umap.find(name2) != umap.end()){  //2만 있음
                int cnt = umap[name2];
                for(it = umap.begin(); it!=umap.end(); it++){
                    it->second = cnt + 1;
                }
                umap[name1] = cnt + 1;
                
                cout << umap[name1] << "\n";
            }
            else{   //둘다 있음
                int cnt1 = umap[name1];
                int cnt2 = umap[name2];
                for(it=umap.begin(); it!= umap.end(); it++){
                    it->second = cnt1 + cnt2;
                }
                cout << umap[name1] << "\n";
            }
        }
        
    }
    
    return 0;
}

728x90
728x90

+ Recent posts