fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define inf 1000000000
  5. #define pb push_back
  6. #define mp make_pair
  7. #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  8. #define endl "\n"
  9. #define yoi cout<<"yo"<<endl;
  10. #define debug(i) cout<<i<<endl;
  11. const int mod=1e9 +7;
  12. void dfs(vector<vector<int> >&v,int dprooted[],int i,int parent){
  13. int j;
  14. dprooted[i]=1;
  15. for(j=0;j<v[i].size();j++){
  16. if(v[i][j]!=parent){
  17. dfs(v,dprooted,v[i][j],i);
  18. dprooted[i]*=(dprooted[v[i][j]]+1);
  19. }
  20. }
  21. }
  22. #undef int
  23. int main(){
  24. #define int long long int
  25. IOS;
  26. int t;
  27. cin>>t;
  28. int h=t;
  29. while(t--){
  30. int n,x,y,i;
  31. cin>>n;
  32. vector<vector<int> > v(n+1);
  33. for(i=0;i<n-1;i++){
  34. cin>>x>>y;
  35. v[x].pb(y);
  36. v[y].pb(x);
  37. }
  38. int dprooted[n+1];
  39. for(i=0;i<=n;i++){
  40. dprooted[i]=1;
  41. }
  42. dfs(v,dprooted,1,0);
  43. int ans=0;
  44. for(i=1;i<=n;i++){
  45. ans+=dprooted[i];
  46. }
  47. cout<<"Case #"<<h-t<<": "<<ans-n<<endl;
  48. }
  49. }
  50.  
Success #stdin #stdout 0s 15240KB
stdin
2
3
1 2
1 3
9
9 4
4 3
1 3
7 4
1 6
5 7
2 4
6 8
stdout
Case #1: 3
Case #2: 66