ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2063C - Remove Exactly Two
Language: python

Solution 1 (runtime: 718 ms):
import sys, math, collections, bisect, heapq, functools, itertools
sys.setrecursionlimit(10 ** 5)
inp = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = 10 ** 20
MX = 10 ** 5 + 10
orda = ord('a')
fmax = lambda x, y: (x if x > y else y)
fmin = lambda x, y: (x if x < y else y)

def solve():
    N = int(inp())
    degree = [0 for _ in range(N + 1)]
    adj = [[] for _ in range(N + 1)]

    for _ in range(N - 1):
        u, v = list(map(int, inp().split()))
        adj[u].append(v)
        adj[v].append(u)
        degree[u] += 1
        degree[v] += 1

    tmp = []

    for node in range(1, N + 1):
        tmp.append((degree[node], node))

    tmp.sort(reverse=True)
    mx = tmp[0][0]
    best = i = 0

    def get_mx_branch(a, d, node):
        a = a[:]
        d = d[:]
        current, current_mx = d[node], 0

        d[node] = 0
        for u in a[node]:
            d[u] -= 1

        for i in range(1, N + 1):
            current_mx = fmax(current_mx, d[i])

        return current + current_mx - 1

    while i < N and tmp[i][0] == mx:
        if i > 1: break
        best = fmax(best, get_mx_branch(adj, degree, tmp[i][1]))
        i += 1

    return best
    
def main():
    for _ in range(int(inp())):
        print(solve())

if __name__ == '__main__':
    main()

Solution 2 (runtime: 1374 ms):
from collections import defaultdict

t = int(input())

def find_max_edges(graph):
    max_us = []
    max_n = -1
    for u in graph:
        if len(graph[u]) > max_n:
            max_us, max_n = [u], len(graph[u])
        elif len(graph[u]) == max_n:
            max_us.append(u)
    return max_us, max_n

for _ in range(t):
    g = defaultdict(list)
    g_lengths = defaultdict(int)
    n = int(input())
    for i in range(n - 1):
        u, v = input().split(' ')
        g[u].append(v)
        g[v].append(u)
        g_lengths[u] += 1
        g_lengths[v] += 1

    us, n = find_max_edges(g)
    max_2nd_edges = 0
    for u in us:
        u_adj = set(g[u])
        for node in g_lengths:
            if node == u:
                continue
            max_2nd_edges = max(g_lengths[node] - (1 if node in u_adj else 0), max_2nd_edges)
        if max_2nd_edges == n:
            break

    components = 1
    components += n - 1
    components += max_2nd_edges - 1
    print(components)
Problem: 2063C - Remove Exactly Two
Language: python

Solution 1 (runtime: 718 ms):
import sys, math, collections, bisect, heapq, functools, itertools
sys.setrecursionlimit(10 ** 5)
inp = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = 10 ** 20
MX = 10 ** 5 + 10
orda = ord('a')
fmax = lambda x, y: (x if x > y else y)
fmin = lambda x, y: (x if x < y else y)

def solve():
    N = int(inp())
    degree = [0 for _ in range(N + 1)]
    adj = [[] for _ in range(N + 1)]

    for _ in range(N - 1):
        u, v = list(map(int, inp().split()))
        adj[u].append(v)
        adj[v].append(u)
        degree[u] += 1
        degree[v] += 1

    tmp = []

    for node in range(1, N + 1):
        tmp.append((degree[node], node))

    tmp.sort(reverse=True)
    mx = tmp[0][0]
    best = i = 0

    def get_mx_branch(a, d, node):
        a = a[:]
        d = d[:]
        current, current_mx = d[node], 0

        d[node] = 0
        for u in a[node]:
            d[u] -= 1

        for i in range(1, N + 1):
            current_mx = fmax(current_mx, d[i])

        return current + current_mx - 1

    while i < N and tmp[i][0] == mx:
        if i > 1: break
        best = fmax(best, get_mx_branch(adj, degree, tmp[i][1]))
        i += 1

    return best
    
def main():
    for _ in range(int(inp())):
        print(solve())

if __name__ == '__main__':
    main()

Solution 2 (runtime: 765 ms):
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def solve646():
    n = int(input())
    e = []
    for _ in range(n-1):
        u, v = map(int, input().split())
        u, v = u-1, v-1
        e.append([u, v])
    final_res = 0
    for _ in range(2):
        adj = [[] for _ in range(n)]
        for u, v in e:
            adj[u].append(v)
            adj[v].append(u)
        rm = [False] * n
        d = [0] * n
        for i in range(n):
            d[i] = len(adj[i])
        for _ in range(2):
            mx = u = -1
            for i in range(n):
                if not rm[i] and mx < d[i]:
                    mx = d[i]
                    u = i
            rm[u] = True
            d[u] = 0
            for v in adj[u]:
                d[v] -= 1
        res = 0
        visited = rm[:]
        def dfs(u):
            visited[u] = True
            q = [u]
            while q:
                u = q.pop()
                for v in adj[u]:
                    if not visited[v]:
                        visited[v] = True
                        q.append(v)
        for i in range(n):
            if not visited[i]:
                dfs(i)
                res += 1
        final_res = max(final_res, res)
        for i, edge in enumerate(e):
            u, v = edge
            e[i] = [n-1-u, n-1-v]
    return final_res

res_test = []
for _ in range(int(input())):
    res_test.append(solve646())
    
sys.stdout.write("\n".join(map(str, res_test))) #output all test
Problem: 2063C - Remove Exactly Two
Language: python

Solution 1 (runtime: 718 ms):
import sys, math, collections, bisect, heapq, functools, itertools
sys.setrecursionlimit(10 ** 5)
inp = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = 10 ** 20
MX = 10 ** 5 + 10
orda = ord('a')
fmax = lambda x, y: (x if x > y else y)
fmin = lambda x, y: (x if x < y else y)

def solve():
    N = int(inp())
    degree = [0 for _ in range(N + 1)]
    adj = [[] for _ in range(N + 1)]

    for _ in range(N - 1):
        u, v = list(map(int, inp().split()))
        adj[u].append(v)
        adj[v].append(u)
        degree[u] += 1
        degree[v] += 1

    tmp = []

    for node in range(1, N + 1):
        tmp.append((degree[node], node))

    tmp.sort(reverse=True)
    mx = tmp[0][0]
    best = i = 0

    def get_mx_branch(a, d, node):
        a = a[:]
        d = d[:]
        current, current_mx = d[node], 0

        d[node] = 0
        for u in a[node]:
            d[u] -= 1

        for i in range(1, N + 1):
            current_mx = fmax(current_mx, d[i])

        return current + current_mx - 1

    while i < N and tmp[i][0] == mx:
        if i > 1: break
        best = fmax(best, get_mx_branch(adj, degree, tmp[i][1]))
        i += 1

    return best
    
def main():
    for _ in range(int(inp())):
        print(solve())

if __name__ == '__main__':
    main()

Solution 2 (runtime: 468 ms):
def max1(t) :
    indice=0
    nb=t[0]
    n=len(t)
    for i in range(1,n) :
        if t[i]>nb :
            indice=i
            nb=t[i]
    return nb,indice

def max2(t,j) :
    
    if j==0 :
        indice=1
        nb=t[1]
    
    else:
        nb=t[0]
        indice=0
    n=len(t)
    for i in range(1,n) :
        if i!=j :
            if t[i]>nb :
                indice=i
                nb=t[i]
    return nb,indice

for _ in range(int(input())):
    n=int(input())
    if n<=2 :
        for i in range(n-1):
            u,v=map(int,input().split())
        print(0)
    else:
        t=[0]*n
        t2=[0]*n
        l=[]
        for i in range(n-1):
            u,v=map(int,input().split())
            l.append((u,v))
            t[u-1]+=1
            t[v-1]+=1
            t2[u-1]+=1
            t2[v-1]+=1
        nb1,indice1=max1(t)
        nb3,indice3=max2(t2,indice1)
        t[indice1]=0
        t2[indice3]=0
        for i in l :
            if i[0]-1==indice1 :
                t[i[1]-1]-=1
            elif i[1]-1==indice1 :
                t[i[0]-1]-=1
        for i in l :
            if i[0]-1==indice3 :
                t2[i[1]-1]-=1
            elif i[1]-1==indice3 :
                t2[i[0]-1]-=1
        nb2,indice2=max1(t)
        nb4,indice4=max1(t2)
 
  
        print(max(nb1+nb2-1,nb3+nb4-1))
Problem: 2063C - Remove Exactly Two
Language: python

Solution 1 (runtime: 1374 ms):
from collections import defaultdict

t = int(input())

def find_max_edges(graph):
    max_us = []
    max_n = -1
    for u in graph:
        if len(graph[u]) > max_n:
            max_us, max_n = [u], len(graph[u])
        elif len(graph[u]) == max_n:
            max_us.append(u)
    return max_us, max_n

for _ in range(t):
    g = defaultdict(list)
    g_lengths = defaultdict(int)
    n = int(input())
    for i in range(n - 1):
        u, v = input().split(' ')
        g[u].append(v)
        g[v].append(u)
        g_lengths[u] += 1
        g_lengths[v] += 1

    us, n = find_max_edges(g)
    max_2nd_edges = 0
    for u in us:
        u_adj = set(g[u])
        for node in g_lengths:
            if node == u:
                continue
            max_2nd_edges = max(g_lengths[node] - (1 if node in u_adj else 0), max_2nd_edges)
        if max_2nd_edges == n:
            break

    components = 1
    components += n - 1
    components += max_2nd_edges - 1
    print(components)


Solution 2 (runtime: 765 ms):
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def solve646():
    n = int(input())
    e = []
    for _ in range(n-1):
        u, v = map(int, input().split())
        u, v = u-1, v-1
        e.append([u, v])
    final_res = 0
    for _ in range(2):
        adj = [[] for _ in range(n)]
        for u, v in e:
            adj[u].append(v)
            adj[v].append(u)
        rm = [False] * n
        d = [0] * n
        for i in range(n):
            d[i] = len(adj[i])
        for _ in range(2):
            mx = u = -1
            for i in range(n):
                if not rm[i] and mx < d[i]:
                    mx = d[i]
                    u = i
            rm[u] = True
            d[u] = 0
            for v in adj[u]:
                d[v] -= 1
        res = 0
        visited = rm[:]
        def dfs(u):
            visited[u] = True
            q = [u]
            while q:
                u = q.pop()
                for v in adj[u]:
                    if not visited[v]:
                        visited[v] = True
                        q.append(v)
        for i in range(n):
            if not visited[i]:
                dfs(i)
                res += 1
        final_res = max(final_res, res)
        for i, edge in enumerate(e):
            u, v = edge
            e[i] = [n-1-u, n-1-v]
    return final_res

res_test = []
for _ in range(int(input())):
    res_test.append(solve646())
    
sys.stdout.write("\n".join(map(str, res_test))) #output all test
Problem: 2063C - Remove Exactly Two
Language: python

Solution 1 (runtime: 1374 ms):
from collections import defaultdict

t = int(input())

def find_max_edges(graph):
    max_us = []
    max_n = -1
    for u in graph:
        if len(graph[u]) > max_n:
            max_us, max_n = [u], len(graph[u])
        elif len(graph[u]) == max_n:
            max_us.append(u)
    return max_us, max_n

for _ in range(t):
    g = defaultdict(list)
    g_lengths = defaultdict(int)
    n = int(input())
    for i in range(n - 1):
        u, v = input().split(' ')
        g[u].append(v)
        g[v].append(u)
        g_lengths[u] += 1
        g_lengths[v] += 1

    us, n = find_max_edges(g)
    max_2nd_edges = 0
    for u in us:
        u_adj = set(g[u])
        for node in g_lengths:
            if node == u:
                continue
            max_2nd_edges = max(g_lengths[node] - (1 if node in u_adj else 0), max_2nd_edges)
        if max_2nd_edges == n:
            break

    components = 1
    components += n - 1
    components += max_2nd_edges - 1
    print(components)


Solution 2 (runtime: 468 ms):
def max1(t) :
    indice=0
    nb=t[0]
    n=len(t)
    for i in range(1,n) :
        if t[i]>nb :
            indice=i
            nb=t[i]
    return nb,indice

def max2(t,j) :
    
    if j==0 :
        indice=1
        nb=t[1]
    
    else:
        nb=t[0]
        indice=0
    n=len(t)
    for i in range(1,n) :
        if i!=j :
            if t[i]>nb :
                indice=i
                nb=t[i]
    return nb,indice

for _ in range(int(input())):
    n=int(input())
    if n<=2 :
        for i in range(n-1):
            u,v=map(int,input().split())
        print(0)
    else:
        t=[0]*n
        t2=[0]*n
        l=[]
        for i in range(n-1):
            u,v=map(int,input().split())
            l.append((u,v))
            t[u-1]+=1
            t[v-1]+=1
            t2[u-1]+=1
            t2[v-1]+=1
        nb1,indice1=max1(t)
        nb3,indice3=max2(t2,indice1)
        t[indice1]=0
        t2[indice3]=0
        for i in l :
            if i[0]-1==indice1 :
                t[i[1]-1]-=1
            elif i[1]-1==indice1 :
                t[i[0]-1]-=1
        for i in l :
            if i[0]-1==indice3 :
                t2[i[1]-1]-=1
            elif i[1]-1==indice3 :
                t2[i[0]-1]-=1
        nb2,indice2=max1(t)
        nb4,indice4=max1(t2)
 
  
        print(max(nb1+nb2-1,nb3+nb4-1))
Problem: 2063C - Remove Exactly Two
Language: python

Solution 1 (runtime: 765 ms):
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def solve646():
    n = int(input())
    e = []
    for _ in range(n-1):
        u, v = map(int, input().split())
        u, v = u-1, v-1
        e.append([u, v])
    final_res = 0
    for _ in range(2):
        adj = [[] for _ in range(n)]
        for u, v in e:
            adj[u].append(v)
            adj[v].append(u)
        rm = [False] * n
        d = [0] * n
        for i in range(n):
            d[i] = len(adj[i])
        for _ in range(2):
            mx = u = -1
            for i in range(n):
                if not rm[i] and mx < d[i]:
                    mx = d[i]
                    u = i
            rm[u] = True
            d[u] = 0
            for v in adj[u]:
                d[v] -= 1
        res = 0
        visited = rm[:]
        def dfs(u):
            visited[u] = True
            q = [u]
            while q:
                u = q.pop()
                for v in adj[u]:
                    if not visited[v]:
                        visited[v] = True
                        q.append(v)
        for i in range(n):
            if not visited[i]:
                dfs(i)
                res += 1
        final_res = max(final_res, res)
        for i, edge in enumerate(e):
            u, v = edge
            e[i] = [n-1-u, n-1-v]
    return final_res

res_test = []
for _ in range(int(input())):
    res_test.append(solve646())
    
sys.stdout.write("\n".join(map(str, res_test))) #output all test

Solution 2 (runtime: 468 ms):
def max1(t) :
    indice=0
    nb=t[0]
    n=len(t)
    for i in range(1,n) :
        if t[i]>nb :
            indice=i
            nb=t[i]
    return nb,indice

def max2(t,j) :
    
    if j==0 :
        indice=1
        nb=t[1]
    
    else:
        nb=t[0]
        indice=0
    n=len(t)
    for i in range(1,n) :
        if i!=j :
            if t[i]>nb :
                indice=i
                nb=t[i]
    return nb,indice

for _ in range(int(input())):
    n=int(input())
    if n<=2 :
        for i in range(n-1):
            u,v=map(int,input().split())
        print(0)
    else:
        t=[0]*n
        t2=[0]*n
        l=[]
        for i in range(n-1):
            u,v=map(int,input().split())
            l.append((u,v))
            t[u-1]+=1
            t[v-1]+=1
            t2[u-1]+=1
            t2[v-1]+=1
        nb1,indice1=max1(t)
        nb3,indice3=max2(t2,indice1)
        t[indice1]=0
        t2[indice3]=0
        for i in l :
            if i[0]-1==indice1 :
                t[i[1]-1]-=1
            elif i[1]-1==indice1 :
                t[i[0]-1]-=1
        for i in l :
            if i[0]-1==indice3 :
                t2[i[1]-1]-=1
            elif i[1]-1==indice3 :
                t2[i[0]-1]-=1
        nb2,indice2=max1(t)
        nb4,indice4=max1(t2)
 
  
        print(max(nb1+nb2-1,nb3+nb4-1))
Problem: 2050B - Transfusion
Language: c++17 

Solution 1 (runtime: 78 ms):
#include <bits/stdc++.h>
#define int long long
using namespace std;
int testcase;
signed main()
{
    cin.tie(0) -> ios_base::sync_with_stdio(0);
    cin >> testcase;
    while (testcase--)
    {
        int n,a[200009];
        cin >> n;
        int odd=0,even=0;
        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
            if (i % 2 == 0) even+=a[i];
            else odd+=a[i];
        }
        int oda = n/2,eva = n/2;
        if (n % 2 != 0) eva++;
        if (odd % oda != 0 || even % eva != 0 || odd/oda != even/eva)
        {
            cout << "nO\n";
        }
        else cout << "YeS\n";
    }
    return 0;
}


Solution 2 (runtime: 327 ms):
#include <fstream>
#include <iostream>
using namespace std;



int main() {
    istream& fin = cin;
    ostream& fout = cout;

    int t;
    fin >> t;
    int n;
    long long a1, a2;
    int temp;
    for (int i=0; i<t; i++) {
        fin >> n;
        a1 = 0; a2 = 0;
        for (int j=0; j<n; j++) {
            fin >> temp;
            if (j % 2 == 0) a1 += temp;
            else a2 += temp;
        }
        bool possible = (a2 % (n / 2) == 0 && a1 % (n - n/2) == 0 && (a2 / (n/2) == a1 / (n - n/2)));
        fout << (possible ? "YES" : "NO") << "\n";
    }
}
Problem: 2050C - Uninteresting Number
Language: c++17 

Solution 1 (runtime: 61 ms):
#include <bits/stdc++.h>

#define TEST
// #define IOFILE
#define iamspeed ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
#define vi vector<int>
#define vvi vector<vector<int>>
#define pii pair<int, int>
#define vii vector<pair<int, int>>
#define fr(a, x) for(int a=0; a<=x; a++)
#define ll long long
#define endl '\n'

using namespace std;

void solve();

int main(){
	
	iamspeed

	#ifdef IOFILE
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	#endif

	int t=1;
	
	#ifdef TEST
		cin>>t;
	#endif

	while (t--)
		solve();

    return 0;
}

void solve(){
	// Here goes my code
    // 2 4 6 8 [1 - 10] 3-12 5-14 7-16
    int sum = 0, two = 0, six = 0;
    // cin>>n;
    string s;
    cin>>s;
    fr(i, s.size()-1){
        char a=s[i];
        sum += a-'0';
        // cout<<sum<<endl;
        sum %= 9;
        if ((a-'0')==2){
            two++;
        }
        else if ((a-'0')==3){
            six++;
        }
    }
    fr(i, min(two, 8)){
        fr(j, min(six, 2)){
            if ((sum + i * 2 + j * 6) % 9 == 0){
                // cout<<sum<<" "<<i<<" "<<j<<endl;
                cout<<"YES\n";
                return;
            }
        }
    }
    cout<<"NO\n";
}

Solution 2 (runtime: 77 ms):
#include<bits/stdc++.h>
using namespace std;

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        string s;
        cin >> s;

        int sum = 0, n = 0, m = 0;
        for (char c : s)
        {
            sum += c - '0';
            if (c == '2') n++;
            if (c == '3') m++;
        }


        vector<bool> dp(9, false);
        dp[sum % 9] = true;

 
        for (int i = 0; i < n; i++)
        {
            vector<bool> new_dp(9, false);
            for (int rem = 0; rem < 9; rem++)
            {
                if (dp[rem])
                {
                    new_dp[rem] = true;
                    new_dp[(rem + 2) % 9] = true;
                }
            }
            dp = new_dp;
        }

        for (int i = 0; i < m; i++)
        {
            vector<bool> new_dp(9, false);
            for (int rem = 0; rem < 9; rem++)
            {
                if (dp[rem])
                {
                    new_dp[rem] = true;
                    new_dp[(rem + 6) % 9] = true;
                }
            }
            dp = new_dp;
        }

        cout << (dp[0] ? "YES" : "NO") << endl;
    }
}
Problem: 2050C - Uninteresting Number
Language: c++17 

Solution 1 (runtime: 62 ms):
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <limits>
#include <unordered_set>

int main()
{
    int t;
    std::cin >> t;

    while (t-- > 0)
    {
        std::string n;
        std::cin >> n;

        int s = 0, a = 0, b = 0;
        for (char c : n)
        {
            s += (c - '0');
            if (c == '2')
            {
                ++a;
            }
            else if (c == '3')
            {
                ++b;
            }
        }

        int q = s % 9;
        if (!q)
        {
            std::cout << "YES\n";
            continue;
        }

        auto f = [](bool yes)
        {
            std::cout << (yes ? "YES" : "NO") << '\n';
        };

        switch (9 - q)
        {
        case 1:
            f(a >= 5 || (a >= 2 && b >= 1));
            break;
        case 2:
            f(a >= 1);
            break;
        case 3:
            f(a >= 6 || b >= 2 || (a >= 3 && b >= 1));
            break;
        case 4:
            f(a >= 2);
            break;
        case 5:
            f(a >= 7 || (a >= 4 && b >= 1) || (a >= 1 && b >= 2));
            break;
        case 6:
            f(a >= 3 || b >= 1);
            break;
        case 7:
            f(a >= 8 || (a >= 5 && b >= 1) || (a >= 2 && b >= 2));
            break;
        case 8:
            f(a >= 4 || (a >= 1 && b >= 1));
            break;
        }
    }
}


Solution 2 (runtime: 77 ms):

#include <bits/stdc++.h>
using namespace std;
#define int long long 
int min(int x,int y){
    if(x<=y)return x;
    return y;
}

bool fn(int count2,int count3,int sum){
    // cout<<"ggg"<<count2<<count3<<sum<<endl;
    for(int i=0;i<=min(100,count2);i++){
           for(int j=0;j<=min(100,count3);j++){
               int sum1=sum+i*2+j*6;
            //   if(i==0&&j==2)cout<<sum1<<"ggdd"<<endl;
               if(sum1%9==0){
                   return true;
               }
           }
       }
       return false;
}
int32_t main()
{
    int test;
    cin>>test;
    while(test--){
       string n;
       cin>>n;
       int count2=0;
       int count3=0;
       int  sum=0;
    //   while(n!=0){
    //       if(n%10==2)count2++;
    //       if(n%10==3)count3++;
    //       sum+=n%10;
    //       n=n/10;
    //   
        
    for(int i=0;i<n.size();i++){
        if(n[i]=='2')count2++;
        if(n[i]=='3')count3++;
        sum+=(n[i]-'0');
    }
       
      if(fn(count2,count3,sum))cout<<"yes"<<endl;
      else cout<<"no"<<endl;
       
       
        
    }

    return 0;
}