1. 程式人生 > >2017 ICPC/ACM 沈陽區域賽HDU6228

2017 ICPC/ACM 沈陽區域賽HDU6228

color mini bio log desc lease all amp fin

Tree

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 373 Accepted Submission(s): 254


Problem Description Consider a un-rooted tree T which is not the biological significance of tree or plant, but a tree as an undirected graph in graph theory with n nodes, labelled from 1 to n. If you cannot understand the concept of a tree here, please omit this problem.
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.

Input The first line of input contains an integer T (1 ≤ T ≤ 1000), indicating the total number of test cases.
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.

Output For each test case, output the maximum size of E1 ∩ E1 ... ∩ Ek.

Sample Input 3 4 2 1 2 2 3 3 4 4 2 1 2 1 3 1 4 6 3 1 2 2 3 3 4 3 5 6 2 Sample Output 1 0 1

題意 給樹節點染色

直接dfs搜一遍就好了

 1 #include <stdio.h>
 2 #include <math.h>
 3 #include <string.h>
 4 #include <stdlib.h>
 5
#include <iostream> 6 #include <sstream> 7 #include <algorithm> 8 #include <string> 9 #include <queue> 10 #include <ctime> 11 #include <vector> 12 using namespace std; 13 const int maxn= 1e5+5; 14 const int maxm= 1e6+5; 15 const int inf = 0x3f3f3f3f; 16 typedef long long ll; 17 vector<int> G[maxn]; 18 int n,k,ans; 19 int siz[maxn]; 20 void dfs(int x,int fa) 21 { 22 siz[x]=1; 23 for(int i=0;i<G[x].size();i++) 24 { 25 int w=G[x][i]; 26 if(w!=fa) 27 { 28 dfs(w,x); 29 siz[x]+=siz[w]; 30 } 31 } 32 if(siz[x]>=k&&n-siz[x]>=k) 33 ans++; 34 } 35 int main() 36 { 37 int t; 38 scanf("%d",&t); 39 while(t--) 40 { 41 scanf("%d%d",&n,&k); 42 int u,v; 43 for(int i=1;i<=n;i++) 44 G[i].clear(); 45 for(int i=1;i<n;i++) 46 { 47 scanf("%d%d",&u,&v); 48 G[u].push_back(v); 49 G[v].push_back(u); 50 } 51 ans=0; 52 dfs(1,0); 53 printf("%d\n",ans); 54 } 55 }

2017 ICPC/ACM 沈陽區域賽HDU6228