hdu 6228 Tree ###K ###K ###K //K
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6228
题意:给一棵树 给k种颜色染色, 每种颜色连接起来 走过的边算一个集合, 问所有颜色的交集最大为多少
思路:考虑每条边能够产生贡献和不能产生贡献,对每一条边来说 上节点的子树和下节点的子树 都大于k 即肯定可以产生贡献
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 #define pb push_back 5 const int maxn =2e5+10; 6 const int mod=1e9+7; 7 int n,k; 8 vector<int>E[maxn]; 9 int cnt[maxn]; 10 int ans; 11 12 13 void dfs(int u,int fa) 14 { 15 cnt[u]=1; 16 for(auto &v:E[u]) 17 { 18 if(v==fa) 19 continue; 20 dfs(v,u); 21 cnt[u]+=cnt[v]; 22 } 23 } 24 void dfs222(int u,int fa) 25 { 26 for(auto &v:E[u]) 27 { 28 if(v==fa) 29 continue; 30 int t=n-cnt[v]; 31 if(t>=k&&cnt[v]>=k) 32 ans++; 33 dfs222(v,u); 34 } 35 } 36 37 38 int main() 39 { 40 ios::sync_with_stdio(false); 41 cin.tie(0); 42 int t; 43 cin>>t; 44 while(t--) 45 { 46 cin>>n>>k; 47 for(int i=1;i<=n;i++) 48 E[i].clear(); 49 ans=0; 50 for(int i=1;i<n;i++) 51 { 52 int u,v; 53 cin>>u>>v; 54 E[u].pb(v); 55 E[v].pb(u); 56 } 57 dfs(1,0); 58 dfs222(1,0); 59 cout<<ans<<'\n'; 60 61 } 62 63 64 65 }

浙公网安备 33010602011771号