#include<iostream>
#include<cstring>
#include<string>
#include<queue>
#include<map>
#include<algorithm>
using namespace std;
int forts[27], vis[27];
vector<int> mp[27];
int n, k;
int bfs(int ori) {
vis[ori] = 1;
queue<int> q;
q.push(ori);
int sz, cur, coun, len, i;
coun = 0;
while (!q.empty()) {
sz = q.size();
coun++;
while (sz--) {
cur = q.front();
q.pop();
vector<int> to = mp[cur];
len = to.size();
for (i = 0; i < len; i++) {
if (vis[to[i]])continue;
if (forts[to[i]]) {
return coun;
} else {
vis[to[i]] = 1;
q.push(to[i]);
}
}
}
}
}
int main() {
char ch1, ch2, first;
vector<int> ans;
memset(forts, 0, sizeof(forts));
cin >> n >> first >> k;
forts[int(first - 'A' + 1)] = 1;
ans.push_back(int(first - 'A' + 1));
while (cin >> ch1 >> ch2) {
mp[int(ch1 - 'A' + 1)].push_back(int(ch2 - 'A' + 1));
mp[int(ch2 - 'A' + 1)].push_back(int(ch1 - 'A' + 1));
}
int i, j;
int num, maxd, vuner[27];
vector<int> v;
for (i = 1; i < k; i++) {
maxd = -1;
memset(vuner, 0, sizeof(vuner));
v.clear();
for (j = 1; j <= n; j++) {
if (!forts[j]) {
memset(vis, 0, sizeof(vis));
num = bfs(j);
vuner[j] = num;
maxd = max(num, maxd);
}
}
for (j = 1; j <= n; j++) {
if (!forts[j] && maxd == vuner[j]) {
v.push_back(j);
}
}
sort(v.begin(), v.end());
forts[v[0]] = 1;
ans.push_back(v[0]);
}
int len = ans.size();
printf("Program 8 by team X\n");
printf("%c", char(ans[0] + 'A' - 1));
for (i = 1; i < len; i++) {
printf(" %c", char(ans[i] + 'A' - 1));
}
printf("\nEnd of program 8 by team X");
}
10-28
212