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

Previous 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;
}

Submission (verdict: Accepted):
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.KidsCount = 0;
        foreach (var kid in maxNode.Kids)
        {
            kid.KidsCount -= 1;
        }

        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);

            nodes[p].Kids.Add(nodes[k]);
            nodes[k].Kids.Add(nodes[p]);
        }

        for (var i = 0; i < nodes.Length; i++)
        {
            nodes[i].KidsCount = nodes[i].Kids.Count;
        }
    }
}

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

    public int ComponentsCount => KidsCount;

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

    public override int GetHashCode() => Value;
}