using System; using System.Collections.Generic; using System.Text; using System.Linq; class Program { public void Proc() { Reader.IsDebug = false; int[] inpt = Reader.GetInt(); int matiCount = inpt[0]; int roadCount = inpt[1]; int queryCount = inpt[2]; List roadList = new List(); for (int i = 0; i < roadCount; i++) { inpt = Reader.GetInt(); Road rd = new Road(inpt[0], inpt[1], inpt[2]); roadList.Add(rd); } int[] history = Reader.GetInt(); List target = new List(); roadList.FindAll((a) => { return (a.Cost == history[0]); }).ForEach((a) => { if (!target.Contains(a.Mati1)) { target.Add(a.Mati1); } if (!target.Contains(a.Mati2)) { target.Add(a.Mati2); } }); for (int i = 1; i < history.Length; i++) { List nextTarget = new List(); ; target.ForEach((a) => { roadList.FindAll((b) => { return (b.Mati1 == a || b.Mati2 == a) && b.Cost == history[i]; }).ForEach((c) => { if (c.Mati1 != a && (!nextTarget.Contains(c.Mati1))) { nextTarget.Add(c.Mati1); } else if(c.Mati2 != a && (!nextTarget.Contains(c.Mati2))) { nextTarget.Add(c.Mati2); } }); }); target = nextTarget; if (nextTarget.Count == 0) { break; } } target.Sort(); Console.WriteLine(target.Count); Console.WriteLine(string.Join(" ", target)); } public class Road { public int Mati1; public int Mati2; public int Cost; public Road(int m1, int m2, int c) { this.Mati1 = m1; this.Mati2 = m2; this.Cost = c; } } public class Reader { public static bool IsDebug = true; private static String PlainInput = @" 3 2 2 1 2 10 1 2 20 10 20 "; private static System.IO.StringReader Sr = null; public static string ReadLine() { if (IsDebug) { if (Sr == null) { Sr = new System.IO.StringReader(PlainInput.Trim()); } return Sr.ReadLine(); } else { return Console.ReadLine(); } } public static int[] GetInt(char delimiter = ' ', bool trim = false) { string inptStr = ReadLine(); if (trim) { inptStr = inptStr.Trim(); } string[] inpt = inptStr.Split(delimiter); int[] ret = new int[inpt.Length]; for (int i = 0; i < inpt.Length; i++) { ret[i] = int.Parse(inpt[i]); } return ret; } } static void Main() { Program prg = new Program(); prg.Proc(); } }