#include #include #include #include using namespace std; int main() { int n, m, k; cin >> n >> m >> k; vector a, b; vector c; a.resize(m); b.resize(m); c.resize(m); for (int i = 0; i < m; i++){ cin >> a[i] >> b[i] >> c[i]; } vector waru; waru.resize(k); vector kouho; cin >> waru[0]; for (int i = 0; i < m; i++){ if (c[i] == waru[0]){ kouho.push_back(a[i]); kouho.push_back(b[i]); } } for (int j = 1; j> waru[j]; for (int i = 0; i < m; i++){ if (c[i] == waru[j]){ int cnt = 0; for (int h = 0; h < kouho.size(); h++){ if (kouho[h] == a[i]){ if (!count(kouho.begin(), kouho.end(), b[i])){ kouho.erase(kouho.begin() + h); kouho.push_back(b[i]); cnt++; } } else if (kouho[h] == b[i]){ if (!count(kouho.begin(), kouho.end(), a[i])){ kouho.erase(kouho.begin() + h); kouho.push_back(a[i]); cnt++; } } } if (cnt){ kouho.erase(kouho.begin()); } } } sort(kouho.begin(), kouho.end()); unique(kouho.begin(), kouho.end()); } cout << kouho.size() << endl; for (int i = 0; i < kouho.size(); i++){ if (i) cout << " "; cout << kouho[i]; } cout << endl; return 0; }