daily pastebin goal
66%
SHARE
TWEET

Untitled

a guest Mar 13th, 2018 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define PB push_back
  4. #define ZERO (1e-10)
  5. #define INF int(1e9+1)
  6. #define CL(A,I) (memset(A,I,sizeof(A)))
  7. #define DEB printf("DEB!\n");
  8. #define D(X) cout<<"  "<<#X": "<<X<<endl;
  9. #define EQ(A,B) (A+ZERO>B&&A-ZERO<B)
  10. typedef long long ll;
  11. typedef pair<ll,ll> pll;
  12. typedef vector<int> vi;
  13. typedef pair<int,int> ii;
  14. typedef vector<ii> vii;
  15. #define IN(n) int n;scanf("%d",&n);
  16. #define FOR(i, m, n) for (int i(m); i < n; i++)
  17. #define F(n) FOR(i,0,n)
  18. #define FF(n) FOR(j,0,n)
  19. #define FT(m, n) FOR(k, m, n)
  20. #define aa first
  21. #define bb second
  22. void ga(int N,int *A){F(N)scanf("%d",A+i);}
  23. #define MX (10004)
  24. vi g[MX];
  25. int P[MX],S[MX],N,a,b,e,q[MX],T,H[MX],W[MX],p;
  26. void dfs(int u,int p){
  27.     P[u]=p;
  28.     for(auto&h:g[u])if(h^p)++S[u],dfs(h,u);
  29. }
  30. void bfs(){
  31.     if(N==2){T=1;return;}
  32.     e=0;
  33.     F(N)if(!S[i])q[e++]=i;
  34.     F(e){
  35.         T+=H[a=q[i]]/2,p=P[a];
  36.         if(p^a){
  37.             if(H[a]&1||(W[a]&&!H[a]))++H[p];
  38.             else if(!H[a])++W[p];
  39.             if(!--S[p])q[e++]=p;
  40.         }
  41.     }
  42. //    T+=(!*H&&*W>1)||(*H&1);
  43.     T+=(!*H&&*W)||(*H&1);
  44.     assert(e==N);
  45. }
  46. int main(void){
  47.     IN(_)F(_){
  48.         for(auto&h:g)h.clear();
  49.         scanf("%d",&N),CL(S,0),CL(H,0),CL(W,0),T=0;
  50.         F(N-1)scanf("%d%d",&a,&b),g[a].PB(b),g[b].PB(a);
  51.         dfs(0,0),bfs(),printf("%d\n",T);
  52.     }
  53.     return 0;
  54. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top