72672024-01-05 18:50:26czitaElágazás nélküli úton levő települések (50 pont)csharpTime limit exceeded 33/50300ms39860 KiB
using System;
using System.Collections.Generic;
using System.Linq;


namespace lenelktel
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] st = Console.ReadLine().Split().Select(int.Parse).ToArray();

            int N = st[0];
            int M = st[1];
            List<List<int>> V = new List<List<int>>();
            for (int i = 0; i < N + 1; i++)
            {
                V.Add(new List<int>());
            }
            for (int i = 0; i < M; i++)
            {
                st = Console.ReadLine().Split().Select(int.Parse).ToArray();
                V[st[0]].Add(st[1]);
                V[st[1]].Add(st[0]);
            }
            HashSet<int> zsakfalu = new HashSet<int>();
            List<int> jotel = new List<int>();
            HashSet<int> megold = new HashSet<int>();
            bool[] volt = new bool[N + 1];
            for (int i = 0; i < V.Count; i++)
            {
                if (V[i].Count == 1)
                {
                    zsakfalu.Add(i);
                    jotel.Add(V[i][0]);
                    megold.Add(V[i][0]);
                }
            }
            foreach (var item in zsakfalu)
            {
                int tel = item, kovtel,elozotel=0;
                while (V[tel].Count <= 2)
                {
                    if (V[tel].Count==1)
                    {
                        megold.Add(V[tel][0]);
                        kovtel = V[tel][0];
                      
                        elozotel = tel;  
                        tel = kovtel;

                    }
                    else
                    {
                        if (V[tel][0]!=elozotel)
                        {
                            kovtel = V[tel][0];
                        }
                        else
                        {
                            kovtel = V[tel][1];
                        }
                        megold.Add(kovtel);
                        
                        elozotel = tel;
                        tel = kovtel;
                        if (V[kovtel].Count == 1)
                        {
                            break;
                        }
                    }
                }
            }
            Console.WriteLine(megold.Count);
            Console.WriteLine(string.Join(" ", new SortedSet<int>(megold)));
            Console.ReadKey();
        }
    }
}
SubtaskSumTestVerdictTimeMemory
base33/50
1Accepted0/043ms24892 KiB
2Accepted0/0114ms36188 KiB
3Accepted2/241ms25740 KiB
4Accepted2/241ms25876 KiB
5Accepted2/239ms25712 KiB
6Time limit exceeded0/2273ms6392 KiB
7Accepted2/243ms26752 KiB
8Accepted2/248ms28344 KiB
9Accepted2/256ms30736 KiB
10Accepted2/261ms33472 KiB
11Accepted2/276ms36272 KiB
12Accepted2/282ms36980 KiB
13Accepted3/346ms29232 KiB
14Time limit exceeded0/3261ms9700 KiB
15Time limit exceeded0/3268ms9968 KiB
16Time limit exceeded0/3280ms10120 KiB
17Time limit exceeded0/3300ms13916 KiB
18Time limit exceeded0/3268ms14268 KiB
19Accepted3/385ms38596 KiB
20Accepted3/3112ms39624 KiB
21Accepted3/3114ms39860 KiB
22Accepted3/3114ms39752 KiB