-
Notifications
You must be signed in to change notification settings - Fork 6
/
toi09_disaster.cpp
45 lines (38 loc) · 1.02 KB
/
toi09_disaster.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
#include <bits/stdc++.h>
using namespace std;
int m;
vector<vector<int> > adj(26);
bool visited[26][26];
bool solve(int p,int lv)
{
if(lv==m){ cout << (char)(p+'A') << ' '; return true; }
for(int i = 0;i < adj[p].size();i++)
{
if(visited[p][adj[p][i]]) continue;
visited[p][adj[p][i]] = true;
visited[adj[p][i]][p] = true;
if(solve(adj[p][i],lv+1)){ cout << (char)(p+'A') << ' '; return true; }
visited[p][adj[p][i]] = false;
visited[adj[p][i]][p] = false;
}
return false;
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> m;
for(int i = 0;i < m;i++)
{
string s;
cin >> s;
adj[s[0]-'A'].push_back(s[1]-'A');
adj[s[1]-'A'].push_back(s[0]-'A');
}
for(int i = 0;i < 26;i++)
{
if(adj[i].size()%2==0) continue;
for(int j = 0;j < 26;j++) for(int k = 0;k < 26;k++) visited[j][k] = false;
if(solve(i,0)) return 0;
}
for(int i = 0;i < 26;i++) if(solve(i,0)) return 0;
}