-
Notifications
You must be signed in to change notification settings - Fork 0
/
16432_떡장수와 호랑이.cpp
73 lines (68 loc) · 1.09 KB
/
16432_떡장수와 호랑이.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <vector>
#include <string.h>
using namespace std;
const int MAX_N = 1000 + 2;
int N;
vector <int> arr[MAX_N];
int answer[MAX_N];
int visited[MAX_N][10] = { 0, };
bool doit = false;
void dfs(int days, int lastuse)
{
if (days == N)
{
for (int i = 0; i < N; i++)
cout << answer[i] << "\n";
doit = true;
exit(0);
}
for (int i = 0; i < arr[days].size(); i++)
{
if (days == 0)
{
answer[days] = arr[days][i];
visited[days][answer[days]] = 1;
dfs(days + 1, answer[days]);
}
else if (arr[days][i] != answer[days - 1] && !visited[days][arr[days][i]])
{
visited[days][arr[days][i]] = 1;
answer[days] = arr[days][i];
dfs(days + 1, answer[days]);
}
}
}
int main()
{
cin >> N;
for (int i = 0; i < N; i++)
{
int n;
cin >> n;
while (n--)
{
int tmp;
cin >> tmp;
arr[i].push_back(tmp);
}
if (i != 0)
{
bool flag = false;
for (auto j : arr[i - 1])
{
for (auto k : arr[i])
if (j != k)
flag = true;
}
if (!flag)
{
cout << "-1";
return 0;
}
}
}
dfs(0, 0);
if (!doit)
cout << "-1";
}