[HDU6288]Tree

topic

answer

First, read the question is very problematic. . . . English supposed na! ! !
Direct consider a little complex, direct analysis of whether each edge was elected to the final answer. For this edge, see his \ (size [v] \) and \ (n-size [v] \) if K is greater than equal to the line, if possible, it is selected by the magic point system constructed a variety of topics so-called staining protocol.
The code is very simple

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long LL;
typedef pair<int,int> PII;
inline int read()
{
   int x=0,f=1;char c=getchar();
   while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
   while(isdigit(c)){x=x*10+c-'0';c=getchar();}
   return x*f;
}
const int maxn=200010; 
struct Edge
{
   int u,v,next;
   Edge() {}
   Edge(int _1,int _2,int _3):u(_1),v(_2),next(_3) {}
}e[maxn];
int n,T,ce,K,a,b,first[maxn],size[maxn],cnt;
void addEdge(int a,int b)
{
   e[++ce]=Edge(a,b,first[a]);first[a]=ce;
   e[++ce]=Edge(b,a,first[b]);first[b]=ce;
} 
void dfs(int now,int fa)
{
   size[now]=1;
   for(int i=first[now];i!=-1;i=e[i].next)
       if(e[i].v!=fa)
       {
           dfs(e[i].v,now);
           size[now]+=size[e[i].v];
           if(size[e[i].v]>=K && n-size[e[i].v]>=K)cnt++;
       }
}
int main()
{
   T=read();
   while(T--)
   {
       mem(first,-1);mem(size,0);ce=-1;cnt=0;
       n=read();K=read();
       for(int i=1;i<n;i++)a=read(),b=read(),addEdge(a,b);
       dfs(1,-1);
       printf("%d\n",cnt);
   }
   return 0;
}

Guess you like

Origin www.cnblogs.com/FYH-SSGSS/p/12075141.html