import std.algorithm, std.array, std.container, std.range; import std.string, std.conv; import std.math, std.bigint, std.bitmanip, std.random; import std.stdio, std.typecons; alias Tuple!(int, "n1", int, "n2") side; void main() { auto rd1 = readln.split.map!(to!int); auto n = rd1[0], m = rd1[1], k = rd1[2]; side[][int] si; foreach (_; 0..m) { auto rd2 = readln.split.map!(to!int); si[rd2[2]] ~= side(rd2[0] - 1, rd2[1] - 1); } auto di = readln.split.map!(to!int); auto cands = BitArray([]); cands.length = n; foreach (s; si[di[0]]) { cands[s.n1] = true; cands[s.n2] = true; } foreach (i; 1..k) { auto next = BitArray([]); next.length = n; foreach (s; si[di[i]]) { if (cands[s.n1]) next[s.n2] = true; if (cands[s.n2]) next[s.n1] = true; } cands = next; } auto r = cands.bitsSet.array; writeln(r.length); writeln(r.map!(x => x + 1).map!(to!string).join(' ')); }