-
Notifications
You must be signed in to change notification settings - Fork 5
/
root_the_tree.cpp
67 lines (61 loc) · 1.36 KB
/
root_the_tree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include<bits/stdc++.h>
#include<stdio.h>
#include<stdlib.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> new_data_set;
#define endl "\n"
#define FILEIO freopen("input.txt", "r", stdin);freopen("output1.txt", "w", stdout);
#define LL long long
#define mod 1000000007LL
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
#define pp pop_back
//#define p push
#define I insert
#define bg begin()
#define ed end()
#define sz size()
#define all(v) v.begin(),v.end()
#define ALL(v) v.rbegin(),v.rend()
typedef vector<LL> vi;
typedef pair<LL,LL> pii;
typedef vector<pii> vii;
typedef map<LL ,LL > mii;
typedef priority_queue<int> pq;
typedef queue<int>que;
typedef stack<int>stk;
typedef pair<int, pair<int , int>> ppi;
#define Inf 100000005LL
#define f first
#define s second
void solve(){
int n;
cin>>n;
vi vect[10001];
for (int i=0;i<n-1;i++){
LL a , b;
cin>>a>>b;
vect[a].pb(b);
}
LL ans=0;
vi indegree(n+1);
for(int i=1;i<=n;i++){
for(auto x:vect[i])
indegree[x]++;
}
for(int i=1;i<=n;i++)
ans+=max(indegree[i]-1 , (LL)0);
cout<<ans<<endl;
}
int main()
{
fast
int t;
cin>>t;
while(t--)
solve();
}