Problem: 2063C - Remove Exactly Two
Language: C# 10

Previous submission (verdict: Wrong answer on test 1):
namespace ConsoleApp1._1000;

public class C
{
    public static void Main()
    {
        var t = int.Parse(Console.ReadLine());

         for (var i = 0; i < t; i++)
         {
             Solve();
         }
    }

    public static void Solve()
    {
        var n = int.Parse(Console.ReadLine());
        var nodes = new Node[n];
        Init(nodes);

        var (isEnd, result) = SolveStep(nodes);
        if (!isEnd)
        {
            (_, result) = SolveStep(nodes, result);
        }

        Console.WriteLine(result);
    }

    public static (bool isEnd, int result) SolveStep(ICollection<Node> nodes, int result = 1)
    {
        var maxNode = nodes.Where(node => !node.IsDeleted).MaxBy(node => node.ComponentsCount)!;
        var maxNodes = nodes.Where(node => node.ComponentsCount == maxNode.ComponentsCount).ToHashSet();

        if (maxNodes.Count >= 3)
        {
            return (true, 2 * maxNode.ComponentsCount - 1);
        }

        maxNode.IsDeleted = true;

        maxNode.Parent?.Kids.Remove(maxNode);
        foreach (var kid in maxNode.Kids)
        {
            kid.Parent = null;
        }
        return (false, result + maxNode.ComponentsCount - 1);
    }

    public static void Init(Node[] nodes)
    {
        for (var i = 0; i < nodes.Length; i++)
        {
            nodes[i] = new Node(i);
        }

        for (var i = 0; i < nodes.Length - 1; i++)
        {
            var pair = Console.ReadLine().Split().Select(int.Parse).ToArray();
            var (p, k) = (pair[0] - 1, pair[1] - 1);
            if (nodes[k].Parent == null)
            {
                nodes[p].Kids.Add(nodes[k]);
                nodes[k].Parent = nodes[p];
            }
            else
            {
                nodes[k].Kids.Add(nodes[p]);
                nodes[p].Parent = nodes[k];
            }
        }
    }
}

public class Node
{
    public int Value;
    public bool IsDeleted;
    public HashSet<Node> Kids = new();
    public Node? Parent;

    public int ComponentsCount => Kids.Count + (Parent != null ? 1 : 0);

    public Node(int value)
    {
        Value = value;
    }
}

Submission (verdict: Wrong answer on test 3):
namespace ConsoleApp1._1000;

public class C
{
    public static void Main()
    {
        var t = int.Parse(Console.ReadLine());

        for (var i = 0; i < t; i++)
        {
            Solve();
        }
    }

    public static void Solve()
    {
        var result = 1;
        var n = int.Parse(Console.ReadLine());
        var nodes = new Node[n];
        Init(nodes);

        var maxNode = nodes.MaxBy(node => node.ComponentsCount);
        var maxNodes = nodes.Where(node => node.ComponentsCount == maxNode.ComponentsCount).ToHashSet();

        if (maxNodes.Count >= 3)
        {
            Console.WriteLine(2 * maxNode.ComponentsCount - 1);
            return;
        }

        result += maxNode.ComponentsCount - 1;

        maxNode.Parent?.Kids.Remove(maxNode);
        foreach (var kid in maxNode.Kids)
        {
            kid.Parent = null;
        }

        var nextMaxNode = nodes.Where(node => node.Value != maxNode.Value).MaxBy(node => node.ComponentsCount);

        result += nextMaxNode.ComponentsCount - 1;

        Console.WriteLine(result);
    }

    public static void Init(Node[] nodes)
    {
        for (var i = 0; i < nodes.Length; i++)
        {
            nodes[i] = new Node(i);
        }

        for (var i = 0; i < nodes.Length - 1; i++)
        {
            var pair = Console.ReadLine().Split().Select(int.Parse).ToArray();
            var (p, k) = (pair[0] - 1, pair[1] - 1);
            if (nodes[k].Parent == null)
            {
                nodes[p].Kids.Add(nodes[k]);
                nodes[k].Parent = nodes[p];
            }
            else
            {
                nodes[k].Kids.Add(nodes[p]);
                nodes[p].Parent = nodes[k];
            }
        }
    }
}

public class Node
{
    public int Value;
    public HashSet<Node> Kids = new();
    public Node? Parent;

    public int ComponentsCount => Kids.Count + (Parent != null ? 1 : 0);

    public Node(int value)
    {
        Value = value;
    }

    public override int GetHashCode() => Value;
}