75232024-01-09 13:25:53TortelliniJrElágazás nélküli úton levő települések (50 pont)csharpRuntime error 0/5037ms25380 KiB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StopElagazas
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] inp = Console.ReadLine().Split().Select(int.Parse).ToArray();
            int n = inp[0] + 1;
            int m = inp[1];
            int[] l = new int[2];
            List<int>[] cities = new List<int>[n + 1];
            List<int> bag = new List<int>();
            for (int i = 0; i < m; i++)
            {
                cities[i] = new List<int>();
            }
            for (int i = 0; i < m; i++)
            {
                l = Console.ReadLine().Split().Select(int.Parse).ToArray();
                cities[l[1]].Add(l[0]);
                cities[l[0]].Add(l[1]);
            }
            for (int i = 1; i < n; i++)
            {
                if (cities[i].Count == 1)
                {
                    bag.Add(i);
                }
            }
            int last = -1;
            int dis;
            List<int> sol = new List<int>();
            for (int i = 0; i < bag.Count; i++)
            {
                dis = bag[i];
                while (true)
                {
                    if (dis == bag[i])
                    {
                        last = dis;
                        dis = cities[bag[i]].First();
                        if (!sol.Contains(dis))
                        {
                            sol.Add(dis);
                        }
                    }
                    else if (cities[dis].Count == 2)
                    {
                        if (cities[dis][0] == last)
                        {
                            last = dis;
                            dis = cities[dis][1];
                        }
                        else
                        {
                            last = dis;
                            dis = cities[dis][0];
                        }
                        if (!sol.Contains(dis))
                        {
                            sol.Add(dis);
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }
            Console.WriteLine(sol.Count);
            for (int i = 0; i < sol.Count; i++)
            {
                Console.Write(sol[i] + " ");
            }
            Console.ReadLine();
        }
    }
}
SubtaskSumTestVerdictTimeMemory
base0/50
1Accepted0/032ms21968 KiB
2Runtime error0/035ms22988 KiB
3Runtime error0/234ms22520 KiB
4Runtime error0/232ms22400 KiB
5Runtime error0/232ms22520 KiB
6Runtime error0/234ms22760 KiB
7Runtime error0/234ms22952 KiB
8Runtime error0/235ms23220 KiB
9Runtime error0/234ms22960 KiB
10Runtime error0/235ms23872 KiB
11Runtime error0/235ms23968 KiB
12Runtime error0/235ms24428 KiB
13Runtime error0/335ms24168 KiB
14Runtime error0/335ms24084 KiB
15Runtime error0/334ms24232 KiB
16Runtime error0/337ms25172 KiB
17Runtime error0/335ms24668 KiB
18Runtime error0/334ms24808 KiB
19Runtime error0/335ms24868 KiB
20Runtime error0/335ms24764 KiB
21Runtime error0/335ms25140 KiB
22Runtime error0/335ms25380 KiB