hyelie
hyelie
Hyeil Jeong
       
글쓰기    관리    수식입력
  • 전체보기 (495)
    • PS (283)
      • Algorithm (28)
      • PS Log (244)
      • Contest (6)
      • Tips (5)
    • Development (52)
      • Java (14)
      • Spring (23)
      • SQL (2)
      • Node.js (2)
      • Socket.io (3)
      • Study (4)
      • Utils (4)
    • DevOps (36)
      • Git (5)
      • Docker (4)
      • Kubernetes (2)
      • GCP (3)
      • Environment Set Up (8)
      • Tutorial (12)
      • Figma (2)
    • CS (74)
      • OOP (7)
      • OS (24)
      • DB (2)
      • Network (24)
      • Architecture (0)
      • Security (2)
      • Software Design (0)
      • Parallel Computing (15)
    • Project (15)
      • Project N2T (5)
      • Project ASG (0)
      • Project Meerkat (1)
      • Model Checking (7)
      • Ideas (2)
    • 내가 하고싶은 것! (34)
      • Plan (16)
      • Software Maestro (10)
      • 취준 (8)
hELLO · Designed By 정상우.
hyelie

hyelie

PS/PS Log

23.08.09. 풀었던 문제들 복기

BOJ 2224. 명제 증명, 30분

 floyd warshall로 푸는 문제. floyd-warshall은 예전에 포스팅 한 적이 있다. floyd warshall의 특징은 모든 vertex에서 모든 vertex의 path weight를 최단거리로 만들고, 이를 위해 kij 순서로 for loop를 돌린다.

typedef pair<char, char> pcc;

////////////////////// write your code below

// 모든 vertex에서 모든 vertex로의 path 여부를 알아야 하므로, floyd-warshall
// directed임.

int V = 52;

int char2int(char c){
    if(isupper(c)) return c - 'A';
    if(islower(c)) return c - 'a' + 26;
}

char int2char(int i){
    if(0 <= i && i <= 25) return i + 'A';
    else return i + 'a' - 26;
}

void solve(){
	int N; cin>>N;
    vector<vector<bool>> edges(52, vector<bool>(V, false)); // edges[i][j] : i부터 j까지 path 존재
    // 0 - 25까지는 대문자, 26 - 51까지는 소문자

    char from, to;
    string s;
    for(int i = 0; i<N; i++){
        cin>>from>>s>>to;
        edges[char2int(from)][char2int(to)] = true;
    }

    for(int k = 0; k<V; k++){ // k : 중간 path
        for(int i = 0; i<V; i++){
            for(int j = 0; j<V; j++){
                if(!edges[i][j] && edges[i][k] && edges[k][j]) edges[i][j] = true;
            }
        }
    }

    int cnt = 0;
    vector<pcc> answers;
    for(int i = 0; i<V; i++){
        for(int j = 0; j<V; j++){
            if(i != j && edges[i][j]){
                cnt++;
                answers.push_back({int2char(i), int2char(j)});
            }
        }
    }

    cout<<cnt<<'\n';
    for(pcc answer : answers){
        cout<<answer.first<<" => "<<answer.second<<'\n';
    }
}

 

시간복잡도

 floyd-warshall의 시간복잡도는 vertex 개수가 V일 때 O(V$^3$). 여기서는 vertex가 최대 52개이므로 (알파벳 대/소문자) 충분히 풀 수 있다.

 

공간복잡도

 floyd-warshall의 공간복잡도는 배열을 만들기 위해 O(V$^2$)가 필요하다.

 

 

 

 

 

저작자표시 (새창열림)

'PS > PS Log' 카테고리의 다른 글

23.08.11. 풀었던 문제들 복기  (0) 2023.08.11
23.08.10. 풀었던 문제들 복기  (0) 2023.08.11
23.08.08. 풀었던 문제들 복기  (1) 2023.08.09
23.08.07. 풀었던 문제들 복기  (0) 2023.08.09
23.08.05. 풀었던 문제들 복기  (0) 2023.08.05
    hyelie
    hyelie

    티스토리툴바