PS
BOJ 31423 : 신촌 통폐합 계획
lickelon
2024. 2. 21. 14:33
- 문제 링크 : boj.kr/31423
- 난이도 : G5
- 태그 : 링크드 리스트
31423번: 신촌 통폐합 계획
첫 번째 줄에 대학교의 개수 $N$이 주어진다. $(2 \leq N \leq 500 \, 000)$ 다음 $N$개의 줄의 $i$번째 줄에 대학교 이름을 의미하는 알파벳 소문자로 이루어진 문자열 $s_i$가 주어진다. 주어지는 대학교
www.acmicpc.net
코드
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define INF 0x7FFFFFFF
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll, ll>;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n;
cin >> n;
vector<string> arr(n);
for(int i = 0; i < n; i++) {
cin >> arr[i];
}
vector<int> head(n);
vector<int> tail(n);
for(int i = 0; i < n; i++) {
head[i] = i;
tail[i] = i;
}
vector<int> next(n, -1);
for(int i = 0; i < n-1; i++) {
int a, b;
cin >> a >> b;
next[tail[a-1]] = head[b-1];
head[b-1] = head[a-1];
tail[a-1] = tail[b-1];
}
int start = head[0];
while(head[start] != start) {
start = head[start];
}
while(start != -1) {
cout << arr[start];
start = next[start];
}
return 0;
}
풀이
여러가지 풀이가 있을 수 있겠지만, 링크드 리스트를 응용해서 풀 수도 있다.
728x90