72682024-01-05 18:56:42czitaElágazás nélküli úton levő települések (50 pont)csharpAccepted 50/50116ms38880 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;
                        if(V[kovtel].Count == 1)
                        {
                            break;
                        }
                    }
                    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
base50/50
1Accepted0/043ms24620 KiB
2Accepted0/0116ms35888 KiB
3Accepted2/243ms25256 KiB
4Accepted2/243ms25188 KiB
5Accepted2/239ms25396 KiB
6Accepted2/243ms25472 KiB
7Accepted2/243ms25400 KiB
8Accepted2/248ms28084 KiB
9Accepted2/256ms29964 KiB
10Accepted2/263ms32420 KiB
11Accepted2/279ms35484 KiB
12Accepted2/282ms36012 KiB
13Accepted3/346ms28452 KiB
14Accepted3/350ms30296 KiB
15Accepted3/354ms30876 KiB
16Accepted3/356ms31508 KiB
17Accepted3/375ms37124 KiB
18Accepted3/376ms37328 KiB
19Accepted3/387ms37724 KiB
20Accepted3/3112ms38580 KiB
21Accepted3/3116ms38820 KiB
22Accepted3/3116ms38880 KiB