-
Notifications
You must be signed in to change notification settings - Fork 0
/
762 - We Ship Cheap.cpp
107 lines (103 loc) · 2.35 KB
/
762 - We Ship Cheap.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include<bits/stdc++.h>
using namespace std;
#define asgnarr(a,n) for(int i=0;i<n;i++)cin>>a[i]
#define fast ios_base::sync_with_stdio(false)
#define loop(a,b) for(int i=a;i<b;i++)
#define mem(a,x) memset(a,x,sizeof(a))
#define pii pair<int,int>
typedef long long lli;
typedef long li;
typedef unsigned long long ulli;
typedef unsigned long int uli;
typedef unsigned int ui;
typedef long double ld;
inline int iseven(int x){return x&1?0:1;}
int main()
{
fast;
int n;
bool f=false;
while(cin>>n)
{
if(f)
cout<<endl;
f=true;
vector<int>graph[2*n+1];
map<string,int>si;
map<int,string>is;
string s,d;
int a,b,j=1;
loop(0,n)
{
cin>>s>>d;
if(si[s]==0)
{
is[j]=s;
si[s]=j;
j++;
}
a=si[s];
if(si[d]==0)
{
is[j]=d;
si[d]=j;
j++;
}
b=si[d];
graph[a].push_back(b);
graph[b].push_back(a);
}
cin>>s>>d;
a=si[s],b=si[d];
if(a==0 || b==0)
{
cout<<"No route"<<endl;
continue;
}
if(s==d)
{
cout<<s<<' '<<d<<endl;
continue;
}
int level[2*n+10];
mem(level,-1);
level[a]=0;
map<int,int> parent;
parent[a]=0;
queue<int>q;
q.push(a);
while(!q.empty())
{
int c=q.front();
q.pop();
for(int x:graph[c])
{
if(level[x]==-1)
{
int m;
m=level[x]=level[c]+1;
parent[x]=c;
q.push(x);
}
}
}
if(level[b]==-1)
cout<<"No route"<<endl;
else
{
vector<int> v;
while(b!=a && b!=0)
{
v.push_back(b);
b=parent[b];
v.push_back(b);
}
for(int it=v.size()-1;it>=0;it--)
{
cout<<is[v[it]]<<' ';
it--;
cout<<is[v[it]]<<endl;
}
}
}
}