ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2050E - Three Strings
Language: C++17 (GCC 7-32)

Previous submission (verdict: Memory limit exceeded on test 14):
#include<bits/stdc++.h>
using namespace std;

vector<vector<vector<int>>> dp;

int solve(string a, string b, string c, int st1, int st2, int st3) {
    if (st1 == a.size() && st2 == b.size()) {
        return 0;
    }

    if (dp[st1][st2][st3] != -1) {
        return dp[st1][st2][st3];
    }

    int take_a = INT_MAX, take_b = INT_MAX;

    if (st3 < c.size() && st1 < a.size() && c[st3] == a[st1]) {
        take_a = solve(a, b, c, st1 + 1, st2, st3 + 1);
    }

    if (st3 < c.size() && st2 < b.size() && c[st3] == b[st2]) {
        take_b = solve(a, b, c, st1, st2 + 1, st3 + 1);
    }

    if (st3 < c.size()) {
        if (st1 < a.size()) {
            take_a = min(take_a, 1 + solve(a, b, c, st1 + 1, st2, st3 + 1));
        }
        if (st2 < b.size()) {
            take_b = min(take_b, 1 + solve(a, b, c, st1, st2 + 1, st3 + 1));
        }
    }

    if (take_a == INT_MAX && take_b == INT_MAX) {
        dp[st1][st2][st3] = INT_MAX;
    } else {
        dp[st1][st2][st3] = min(take_a, take_b);
    }

    return dp[st1][st2][st3];
}

int main() {
    int t;
    cin >> t;
    while(t--) {
        string a, b, c;
        cin >> a >> b >> c;

        dp = vector<vector<vector<int>>>(a.size() + 1, vector<vector<int>>(b.size() + 1, vector<int>(c.size() + 1, -1)));

        int ans = solve(a, b, c, 0, 0, 0);

        if (ans == INT_MAX) {
            cout << "-1" << endl;
        } else {
            cout << ans << endl;
        }
    }

    return 0;
}


Submission (verdict: Time limit exceeded on test 16):
#include<bits/stdc++.h>
using namespace std;

vector<vector<int>> dp;

int solve(string a, string b, string c, int st1, int st2, int st3) {
    if (st1 == a.size() && st2 == b.size()) {
        return 0;
    }

    if (dp[st1][st2] != -1) {
        return dp[st1][st2];
    }

    int take_a = INT_MAX, take_b = INT_MAX;

    if (st3 < c.size() && st1 < a.size() && c[st3] == a[st1]) {
        take_a = solve(a, b, c, st1 + 1, st2, st3 + 1);
    }

    if (st3 < c.size() && st2 < b.size() && c[st3] == b[st2]) {
        take_b = solve(a, b, c, st1, st2 + 1, st3 + 1);
    }

    if (st3 < c.size()) {
        if (st1 < a.size()) {
            take_a = min(take_a, 1 + solve(a, b, c, st1 + 1, st2, st3 + 1));
        }
        if (st2 < b.size()) {
            take_b = min(take_b, 1 + solve(a, b, c, st1, st2 + 1, st3 + 1));
        }
    }

    if (take_a == INT_MAX && take_b == INT_MAX) {
        dp[st1][st2] = INT_MAX;
    } else {
        dp[st1][st2] = min(take_a, take_b);
    }

    return dp[st1][st2];
}

int main() {
    int t;
    cin >> t;
    while(t--) {
        string a, b, c;
        cin >> a >> b >> c;

        dp = vector<vector<int>>(a.size() + 1, vector<int>(b.size() + 1, -1));

        int ans = solve(a, b, c, 0, 0, 0);

        if (ans == INT_MAX) {
            cout << "-1" << endl;
        } else {
            cout << ans << endl;
        }
    }

    return 0;
}
Problem: 2050E - Three Strings
Language: C++17 (GCC 7-32)

Previous submission (verdict: Time limit exceeded on test 16):
#include<bits/stdc++.h>
using namespace std;

vector<vector<int>> dp;

int solve(string a, string b, string c, int st1, int st2, int st3) {
    if (st1 == a.size() && st2 == b.size()) {
        return 0;
    }

    if (dp[st1][st2] != -1) {
        return dp[st1][st2];
    }

    int take_a = INT_MAX, take_b = INT_MAX;

    if (st3 < c.size() && st1 < a.size() && c[st3] == a[st1]) {
        take_a = solve(a, b, c, st1 + 1, st2, st3 + 1);
    }

    if (st3 < c.size() && st2 < b.size() && c[st3] == b[st2]) {
        take_b = solve(a, b, c, st1, st2 + 1, st3 + 1);
    }

    if (st3 < c.size()) {
        if (st1 < a.size()) {
            take_a = min(take_a, 1 + solve(a, b, c, st1 + 1, st2, st3 + 1));
        }
        if (st2 < b.size()) {
            take_b = min(take_b, 1 + solve(a, b, c, st1, st2 + 1, st3 + 1));
        }
    }

    if (take_a == INT_MAX && take_b == INT_MAX) {
        dp[st1][st2] = INT_MAX;
    } else {
        dp[st1][st2] = min(take_a, take_b);
    }

    return dp[st1][st2];
}

int main() {
    int t;
    cin >> t;
    while(t--) {
        string a, b, c;
        cin >> a >> b >> c;

        dp = vector<vector<int>>(a.size() + 1, vector<int>(b.size() + 1, -1));

        int ans = solve(a, b, c, 0, 0, 0);

        if (ans == INT_MAX) {
            cout << "-1" << endl;
        } else {
            cout << ans << endl;
        }
    }

    return 0;
}


Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;

#define FAST_IO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

vector<vector<int>> dp;

int solve(string &a, string &b, string &c, int st1, int st2, int st3) {
    if (st1 == a.size() && st2 == b.size()) {
        return 0;
    }

    if (dp[st1][st2] != -1) {
        return dp[st1][st2];
    }

    int take_a = INT_MAX, take_b = INT_MAX;

    if (st3 < c.size() && st1 < a.size() && c[st3] == a[st1]) {
        take_a = solve(a, b, c, st1 + 1, st2, st3 + 1);
    }

    if (st3 < c.size() && st2 < b.size() && c[st3] == b[st2]) {
        take_b = solve(a, b, c, st1, st2 + 1, st3 + 1);
    }

    if (st3 < c.size()) {
        if (st1 < a.size()) {
            take_a = min(take_a, 1 + solve(a, b, c, st1 + 1, st2, st3 + 1));
        }
        if (st2 < b.size()) {
            take_b = min(take_b, 1 + solve(a, b, c, st1, st2 + 1, st3 + 1));
        }
    }

    dp[st1][st2] = min(take_a, take_b);
    return dp[st1][st2];
}

int main() {
    FAST_IO  

    int t;
    cin >> t;
    while (t--) {
        string a, b, c;
        cin >> a >> b >> c;

        dp = vector<vector<int>>(a.size() + 1, vector<int>(b.size() + 1, -1));

        int ans = solve(a, b, c, 0, 0, 0);

        if (ans == INT_MAX) {
            cout << "-1\n";
        } else {
            cout << ans << '\n';
        }
    }

    return 0;
}
Problem: 2050E - Three Strings
Language: C++17 (GCC 7-32)


Submission (verdict: Memory limit exceeded on test 14):
#include<bits/stdc++.h>
using namespace std;

vector<vector<vector<int>>> dp;

int solve(string a, string b, string c, int st1, int st2, int st3) {
    if (st1 == a.size() && st2 == b.size()) {
        return 0;
    }

    if (dp[st1][st2][st3] != -1) {
        return dp[st1][st2][st3];
    }

    int take_a = INT_MAX, take_b = INT_MAX;

    if (st3 < c.size() && st1 < a.size() && c[st3] == a[st1]) {
        take_a = solve(a, b, c, st1 + 1, st2, st3 + 1);
    }

    if (st3 < c.size() && st2 < b.size() && c[st3] == b[st2]) {
        take_b = solve(a, b, c, st1, st2 + 1, st3 + 1);
    }

    if (st3 < c.size()) {
        if (st1 < a.size()) {
            take_a = min(take_a, 1 + solve(a, b, c, st1 + 1, st2, st3 + 1));
        }
        if (st2 < b.size()) {
            take_b = min(take_b, 1 + solve(a, b, c, st1, st2 + 1, st3 + 1));
        }
    }

    if (take_a == INT_MAX && take_b == INT_MAX) {
        dp[st1][st2][st3] = INT_MAX;
    } else {
        dp[st1][st2][st3] = min(take_a, take_b);
    }

    return dp[st1][st2][st3];
}

int main() {
    int t;
    cin >> t;
    while(t--) {
        string a, b, c;
        cin >> a >> b >> c;

        dp = vector<vector<vector<int>>>(a.size() + 1, vector<vector<int>>(b.size() + 1, vector<int>(c.size() + 1, -1)));

        int ans = solve(a, b, c, 0, 0, 0);

        if (ans == INT_MAX) {
            cout << "-1" << endl;
        } else {
            cout << ans << endl;
        }
    }

    return 0;
}
Problem: 2063B - Subsequence Update
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
 
void solve(){
    int n,l,r;
    cin>>n>>l>>r;
    vector<int>v;
    for(int i=0;i<n;i++){
        int a;
        cin>>a;
        v.push_back(a);
    }
    vector<int>lrr,rrr;
    lrr=v;
    rrr=v;
    sort(lrr.begin(),lrr.begin()+r-1);
    sort(rrr.begin()+l-1,rrr.end());
    int ans1=0,ans2=0;
    for(int i=l;i<=r;i++){
        ans1+=lrr[i-l];
        ans2+=rrr[i-1];
    }
    cout<<min(ans1,ans2)<<endl;
}
signed main() {
  ios_base::sync_with_stdio(false); cin.tie(NULL);
  int tc;
  cin>>tc;
  for(int i=0;i<tc;i++)solve();
  return 0;
}


Submission (verdict: Accepted):
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
 
void solve(){
    int n,l,r;
    cin>>n>>l>>r;
    vector<int>v;
    for(int i=0;i<n;i++){
        int a;
        cin>>a;
        v.push_back(a);
    }
    vector<int>lrr,rrr;
    lrr=v;
    rrr=v;
    sort(lrr.begin(),lrr.begin()+r);
    sort(rrr.begin()+l-1,rrr.end());
    int ans1=0,ans2=0;
    for(int i=l;i<=r;i++){
        ans1+=lrr[i-l];
        ans2+=rrr[i-1];
    }
    cout<<min(ans1,ans2)<<endl;
}
signed main() {
  ios_base::sync_with_stdio(false); cin.tie(NULL);
  int tc;
  cin>>tc;
  for(int i=0;i<tc;i++)solve();
  return 0;
}
Problem: 2063B - Subsequence Update
Language: C++17 (GCC 7-32)


Submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
 
void solve(){
    int n,l,r;
    cin>>n>>l>>r;
    vector<int>v;
    for(int i=0;i<n;i++){
        int a;
        cin>>a;
        v.push_back(a);
    }
    sort(v.begin(),v.end());
    int ans=0;
    for(int i=0;i<r-l+1;i++){
        ans+=v[i];
    }
    cout<<ans<<endl;
}
signed main() {
  ios_base::sync_with_stdio(false); cin.tie(NULL);
  int tc;
  cin>>tc;
  for(int i=0;i<tc;i++)solve();
  return 0;
}
Problem: 2063B - Subsequence Update
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
 
void solve(){
    int n,l,r;
    cin>>n>>l>>r;
    vector<int>v;
    for(int i=0;i<n;i++){
        int a;
        cin>>a;
        v.push_back(a);
    }
    sort(v.begin(),v.end());
    int ans=0;
    for(int i=0;i<r-l+1;i++){
        ans+=v[i];
    }
    cout<<ans<<endl;
}
signed main() {
  ios_base::sync_with_stdio(false); cin.tie(NULL);
  int tc;
  cin>>tc;
  for(int i=0;i<tc;i++)solve();
  return 0;
}


Submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
 
void solve(){
    int n,l,r;
    cin>>n>>l>>r;
    vector<int>v;
    for(int i=0;i<n;i++){
        int a;
        cin>>a;
        v.push_back(a);
    }
    vector<int>lrr,rrr;
    lrr=v;
    rrr=v;
    sort(lrr.begin(),lrr.begin()+r-1);
    sort(rrr.begin()+l-1,rrr.end());
    int ans1=0,ans2=0;
    for(int i=l;i<=r;i++){
        ans1+=lrr[i-l];
        ans2+=rrr[i-1];
    }
    cout<<min(ans1,ans2)<<endl;
}
signed main() {
  ios_base::sync_with_stdio(false); cin.tie(NULL);
  int tc;
  cin>>tc;
  for(int i=0;i<tc;i++)solve();
  return 0;
}
Problem: 2050C - Uninteresting Number
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--) {
        string n;
        cin >> n;
        ll twos = 0, threes = 0, c = 0;
        for(auto u : n) {
            if(u == '2') twos++;
            if(u == '3') threes++;
            c += (ll)u - '0';
        }
        if(c % 9 == 0) cout << "YES" << endl;
        else {
            ll rem = (9 - (c % 9)) % 9;
            bool found = false;
            for(ll i = 0; i <= twos && !found; i++) {
                if((rem - (i * 2)) % 6 == 0 && (rem - (i * 2)) / 6 <= threes) {
                    found = true;
                }
            }
            if(found) cout << "YES" << endl;
            else cout << "NO" << endl;
        }
    }
}


Submission (verdict: Time limit exceeded on test 5):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;


int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--){
        string n;
        cin >> n;
        ll twos = 0;
        ll threes = 0;
        ll c = 0;
        for(auto u:n){
            if(u == '2'){
                twos++;
            }
            if(u == '3'){
                    threes++;
            }
            c += (ll)u-'0';

        }
        if(c%9 == 0) cout << "YES"<<endl;
        else{
            bool found = false;
            for(ll i=twos;i>=0;i--){
                for(ll j=threes;j>=0;j--){
                    ll res = (ll)c+i*2+j*6;
                    if(res%9 == 0){
                        found = true;
                        break;
                    }
                }
            }
            if(found)cout<<"YES"<<endl;
            else cout<<"NO"<<endl;

        }






        }




    }
Problem: 2050C - Uninteresting Number
Language: C++17 (GCC 7-32)

Previous submission (verdict: Time limit exceeded on test 5):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;


int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--){
        string n;
        cin >> n;
        ll twos = 0;
        ll threes = 0;
        ll c = 0;
        for(auto u:n){
            if(u == '2'){
                twos++;
            }
            if(u == '3'){
                    threes++;
            }
            c += (ll)u-'0';

        }
        if(c%9 == 0) cout << "YES"<<endl;
        else{
            bool found = false;
            for(ll i=0;i<=twos;i++){
                for(ll j=0;j<=threes;j++){
                    ll res = (ll)c+i*2+j*6;
                    if(res%9 == 0){
                        found = true;
                        break;
                    }
                }
            }
            if(found)cout<<"YES"<<endl;
            else cout<<"NO"<<endl;

        }






        }




    }




Submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--) {
        string n;
        cin >> n;
        ll twos = 0, threes = 0, c = 0;
        for(auto u : n) {
            if(u == '2') twos++;
            if(u == '3') threes++;
            c += (ll)u - '0';
        }
        if(c % 9 == 0) cout << "YES" << endl;
        else {
            ll rem = (9 - (c % 9)) % 9;
            bool found = false;
            for(ll i = 0; i <= twos && !found; i++) {
                if((rem - (i * 2)) % 6 == 0 && (rem - (i * 2)) / 6 <= threes) {
                    found = true;
                }
            }
            if(found) cout << "YES" << endl;
            else cout << "NO" << endl;
        }
    }
}
Problem: 2050C - Uninteresting Number
Language: C++17 (GCC 7-32)


Submission (verdict: Time limit exceeded on test 5):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;


int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--){
        string n;
        cin >> n;
        ll twos = 0;
        ll threes = 0;
        ll c = 0;
        for(auto u:n){
            if(u == '2'){
                twos++;
            }
            if(u == '3'){
                    threes++;
            }
            c += (ll)u-'0';

        }
        if(c%9 == 0) cout << "YES"<<endl;
        else{
            bool found = false;
            for(ll i=0;i<=twos;i++){
                for(ll j=0;j<=threes;j++){
                    ll res = (ll)c+i*2+j*6;
                    if(res%9 == 0){
                        found = true;
                        break;
                    }
                }
            }
            if(found)cout<<"YES"<<endl;
            else cout<<"NO"<<endl;

        }






        }




    }
Problem: 2050C - Uninteresting Number
Language: C++17 (GCC 7-32)

Previous submission (verdict: Time limit exceeded on test 5):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;


int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--){
        string n;
        cin >> n;
        ll twos = 0;
        ll threes = 0;
        ll c = 0;
        for(auto u:n){
            if(u == '2'){
                twos++;
            }
            if(u == '3'){
                    threes++;
            }
            c += (ll)u-'0';

        }
        if(c%9 == 0) cout << "YES"<<endl;
        else{
            bool found = false;
            for(ll i=twos;i>=0;i--){
                for(ll j=threes;j>=0;j--){
                    ll res = (ll)c+i*2+j*6;
                    if(res%9 == 0){
                        found = true;
                        break;
                    }
                }
            }
            if(found)cout<<"YES"<<endl;
            else cout<<"NO"<<endl;

        }






        }




    }




Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int t;
    cin >> t;
    while(t--) {
        string n;
        cin >> n;
        ll digitSum = 0;
        int count2 = 0, count3 = 0;
        for(char digit : n) {
            digitSum += digit - '0';
            if(digit == '2') count2++;
            if(digit == '3') count3++;
        }
        if(digitSum % 9 == 0) {
            cout << "YES\n";
            continue;
        }

        bool found = false;
        int rem = digitSum % 9;

       
        for(int i = 0; i <= min(count2, 8); i++) {
            for(int j = 0; j <= min(count3, 8); j++) {
                int newRem = (rem + (i * 2 + j * 6) % 9) % 9;
                if(newRem == 0) {
                    found = true;
                    goto end;  
                }
            }
        }

        end:
        cout << (found ? "YES\n" : "NO\n");
    }
    return 0;
}
Problem: 2063C - Remove Exactly Two
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
#define ll long long
 
using namespace std;

int n, u, v, cnt;
map<int, int> x;
unordered_map<int, vector<int>> q;
bool out;

void dfs(int start, int t1, int t2, int parent, vector<bool>& vis)
{
	int c = 0;
	vis[start] = true;
	for(auto& i : q[start]){
		if(i != t1 && i != t2 && i != parent){
			dfs(i, t1, t2, start, vis);
			c++;
		}
	}
	
	if(c == 0){
		cnt++;
	}
}

bool comp(pair<int, int>& a, pair<int, int>& b){
	return a.second > b.second;
}

void solve()
{
	cin >> n;
	for(int i = 1; i <= n-1; i++){
		cin >> u >> v;
		x[u]++;
		x[v]++;
		q[u].push_back(v);
		q[v].push_back(u);
	}
	vector<pair<int, int>> mp(x.begin(), x.end());
	sort(mp.begin(), mp.end(), comp);
	
	if(mp[0].second == mp[1].second && mp[1].second == mp[2].second){
		if(distance(q[mp[0].first].begin(), find(q[mp[0].first].begin(), q[mp[0].first].end(), mp[1].first)) < q[mp[0].first].size() &&  distance(q[mp[0].first].begin(), find(q[mp[0].first].begin(), q[mp[0].first].end(), mp[2].first)) < q[mp[0].first].size()){
			swap(mp[0], mp[1]);
			swap(mp[1], mp[2]);
		}
	}
	
	cnt = 0;
	out = false;
	vector<bool> vis(n);
	for(int i = 2; i <= n-1; i++){
		if(vis[mp[i].first]) continue;
		dfs(mp[i].first, mp[0].first, mp[1].first, 0, vis);
	}
	cout << cnt << "\n";
}
 
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	int t; cin >> t;
	while(t--){
		solve();
		x.clear();
		q.clear();
	}
	
	
}

Submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
#define ll long long
 
using namespace std;

int n, u, v, cnt;
map<int, int> x;
unordered_map<int, vector<int>> q;
bool out;

void dfs(int start, int t1, int t2, int parent, vector<bool>& vis)
{
	int c = 0;
	vis[start] = true;
	for(auto& i : q[start]){
		if(i != t1 && i != t2 && i != parent){
			dfs(i, t1, t2, start, vis);
			c++;
		}
	}
	
	if(c == 0){
		cnt++;
	}
}

bool comp(pair<int, int>& a, pair<int, int>& b){
	return a.second > b.second;
}

void solve()
{
	cin >> n;
	for(int i = 1; i <= n-1; i++){
		cin >> u >> v;
		x[u]++;
		x[v]++;
		q[u].push_back(v);
		q[v].push_back(u);
	}
	vector<pair<int, int>> mp(x.begin(), x.end());
	sort(mp.begin(), mp.end(), comp);
	
	if(mp[0].second == mp[1].second && mp[1].second == mp[2].second){
		cout << mp[0].second * 2 - 1 << "\n";
		return;
	}
	
	cnt = 0;
	out = false;
	vector<bool> vis(n);
	for(int i = 2; i <= n-1; i++){
		if(vis[mp[i].first]) continue;
		dfs(mp[i].first, mp[0].first, mp[1].first, 0, vis);
	}
	cout << cnt << "\n";
}
 
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	int t; cin >> t;
	while(t--){
		solve();
		x.clear();
		q.clear();
	}
	
	
}
Problem: 2063C - Remove Exactly Two
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
#define ll long long
 
using namespace std;

int n, u, v, cnt;
map<int, int> x;
unordered_map<int, vector<int>> q;
bool out;

void dfs(int start, int t1, int t2, int parent, vector<bool>& vis)
{
	int c = 0;
	vis[start] = true;
	for(auto& i : q[start]){
		if(i != t1 && i != t2 && i != parent){
			dfs(i, t1, t2, start, vis);
			c++;
		}
	}
	
	if(c == 0){
		cnt++;
	}
}

bool comp(pair<int, int>& a, pair<int, int>& b){
	return a.second > b.second;
}

void solve()
{
	cin >> n;
	for(int i = 1; i <= n-1; i++){
		cin >> u >> v;
		x[u]++;
		x[v]++;
		q[u].push_back(v);
		q[v].push_back(u);
	}
	vector<pair<int, int>> mp(x.begin(), x.end());
	sort(mp.begin(), mp.end(), comp);
	cnt = 0;
	out = false;
	vector<bool> vis(n);
	for(int i = 2; i <= n-1; i++){
		if(vis[mp[i].first]) continue;
		dfs(mp[i].first, mp[0].first, mp[1].first, 0, vis);
	}
	cout << cnt << "\n";
}
 
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	int t; cin >> t;
	while(t--){
		solve();
		x.clear();
		q.clear();
	}
	
	
}

Submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
#define ll long long
 
using namespace std;

int n, u, v, cnt;
map<int, int> x;
unordered_map<int, vector<int>> q;
bool out;

void dfs(int start, int t1, int t2, int parent, vector<bool>& vis)
{
	int c = 0;
	vis[start] = true;
	for(auto& i : q[start]){
		if(i != t1 && i != t2 && i != parent){
			dfs(i, t1, t2, start, vis);
			c++;
		}
	}
	
	if(c == 0){
		cnt++;
	}
}

bool comp(pair<int, int>& a, pair<int, int>& b){
	return a.second > b.second;
}

void solve()
{
	cin >> n;
	for(int i = 1; i <= n-1; i++){
		cin >> u >> v;
		x[u]++;
		x[v]++;
		q[u].push_back(v);
		q[v].push_back(u);
	}
	vector<pair<int, int>> mp(x.begin(), x.end());
	sort(mp.begin(), mp.end(), comp);
	
	if(mp[0].second == mp[1].second && mp[1].second == mp[2].second){
		if(distance(q[mp[0].first].begin(), find(q[mp[0].first].begin(), q[mp[0].first].end(), mp[1].first)) < q[mp[0].first].size() &&  distance(q[mp[0].first].begin(), find(q[mp[0].first].begin(), q[mp[0].first].end(), mp[2].first)) < q[mp[0].first].size()){
			swap(mp[0], mp[1]);
			swap(mp[1], mp[2]);
		}
	}
	
	cnt = 0;
	out = false;
	vector<bool> vis(n);
	for(int i = 2; i <= n-1; i++){
		if(vis[mp[i].first]) continue;
		dfs(mp[i].first, mp[0].first, mp[1].first, 0, vis);
	}
	cout << cnt << "\n";
}
 
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	int t; cin >> t;
	while(t--){
		solve();
		x.clear();
		q.clear();
	}
	
	
}