hdu6228Tree
Now we decide to colour its nodes with k distinct colours, labelled from 1 to k. Then for each colour i = 1, 2, · · · , k, define Ei as the minimum subset of edges connecting all nodes coloured by i. If there is no node of the tree coloured by a specified colour i, Ei will be empty.
Try to decide a colour scheme to maximize the size of E1 ∩ E2 · · · ∩ Ek, and output its size.
For each case, the first line contains two positive integers n which is the size of the tree and k (k ≤ 500) which is the number of colours. Each of the following n - 1 lines contains two integers x and y describing an edge between them. We are sure that the given graph is a tree.
The summation of n in input is smaller than or equal to 200000.
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
const int maxn=200010;
vector<int >ve[maxn];
int plug[maxn];
int sum[maxn];
int number(int x){ //对于这里额使用的是一个数组来标记这个点是否访问过,其实也可以直接将上一个访问的点放进来进行判断就好,因为这个图也是一棵树
for(int i=0;i<ve[x].size();i++){
if(plug[ve[x][i]]) continue;
plug[ve[x][i]]=1;
sum[x]+=number(ve[x][i]);
}
return sum[x];
}
int main(){
int T,node,k,x,y;
cin>>T;
while(T--){
scanf("%d%d",&node,&k);
for(int i=0;i<=maxn+5;i++) ve[i].clear();
memset(plug,0,sizeof(plug));
for(int i=0;i<=node;i++) sum[i]=1;
for(int i=0;i<node-1;i++){
scanf("%d%d",&x,&y);
ve[x].push_back(y);
ve[y].push_back(x);
}
plug[1]=1;
number(1);
/*for(int i=1;i<node;i++) printf("%d ",sum[i]);
printf("%d\n",sum[node]);*/
int ans=0;
for(int i=1;i<=node;i++){
if(sum[i]>=k&&node-sum[i]>=k) ans++;
}
printf("%d\n",ans);
}
}
作者:孙建钊
出处:http://www.cnblogs.com/sunjianzhao/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。