#include <string>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
using namespace std;
const int MAX = 100000001;
vector<string> temp;
int visited[MAX];
bool dfs(string str,int count, vector<vector<string>> t) {
if (count == t.size())
return true;
for (int i = 0; i < t.size(); i++) {
if (t[i][0] == str && visited[i] == -1) {
temp.push_back(t[i][1]);
visited[i] = 1;
if (dfs(t[i][1], count + 1, t))
return true;
visited[i] = -1;
temp.pop_back();
}
}
return false;
}
vector<string> solution(vector<vector<string>> tickets) {
vector<string> answer;
sort(tickets.begin(), tickets.end());
memset(visited, -1, sizeof(visited));
for (int i = 0; i < tickets.size(); i++) {
if (tickets[i][0] == "ICN") {
visited[i] = 1;
temp.clear();
temp.push_back(tickets[i][0]);
temp.push_back(tickets[i][1]);
if (dfs(tickets[i][1],1,tickets)) {
answer = temp;
return answer;
}
visited[i] = -1;
}
}
return answer;
}
프로그래머스