-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1075.cpp
123 lines (86 loc) · 1.75 KB
/
1075.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define PI (acos(-1))
#define ct cout<<
#define cn cin>>
#define spc <<" "<<
#define nl puts("")
#define _fl(x) puts("FLAG " #x);
#define _(x) cout<< #x <<" is "<< x <<endl;
#define fs first
#define sc second
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define _109 (1000000000)
#define _0(arr) memset(arr,0,sizeof ( arr ) )
#define _1(arr) memset(arr,-1,sizeof ( arr ) )
#define _ninp(n,arr) for(int i=0; i<n;i++)cin>>arr[i];
#define _nout(n,arr) for(int i=0; i<n;i++)cout<<arr[i]<<" \n"[i==n-1];
double _eps=1e-6;
long long gcd(long long a, long long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
map<int,string>is;
map<string ,int>si;
vector<int> adj[500];
vector<int> order;
bool vis[500];
void dfs(int v){
vis[v]=1;
// _(v);
for(int x:adj[v])if(!vis[x])dfs(x);
order.pb(v);
}
int main() {
int T;
cn T;
for(int caseno=1; caseno<=T ;caseno++){
is.clear();
si.clear();
order.clear();
int n;
cn n;
int cnt=1;
for (int i = 0; i < n-1; ++i)
{
string a,b;
cn a>>b;
int u,v;
if(si.count(a)){
u=si[a];
}
else{
u=cnt++;
si[a]=u;
is[u]=a;
}
if(si.count(b)){
v=si[b];
}
else{
v=cnt++;
si[b]=v;
is[v]=b;
}
adj[u].pb(v);
}
_0(vis);
for(int i=1;i<cnt;i++){
if(!vis[i])dfs(i);
}
reverse(all(order));
printf("Case %d:\n",caseno);
for(int x:order){
ct is[x];nl;
}nl;
for (int i = 1; i <cnt; ++i)
{
adj[i].clear();
}
}
return 0;
}