hdu 6178 Monkeys
题意:给你一颗树,删除最多的边,使得k个猴子能落在n个结点上(每个猴子所在的节点周围至少连着一个有猴子的节点)
思路:2个猴子一条边,不够的话一只猴子一条边
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N=1E5+10; 4 5 vector<int >a[N]; 6 int vis[N]; 7 int ans; 8 9 namespace IO { 10 const int MX = 4e7; //1e7占用内存11000kb 11 char buf[MX]; int c, sz; 12 void begin() { 13 c = 0; 14 sz = fread(buf, 1, MX, stdin); 15 } 16 inline bool read(int &t) { 17 while(c < sz && buf[c] != '-' && (buf[c] < '0' || buf[c] > '9')) c++; 18 if(c >= sz) return false; 19 bool flag = 0; if(buf[c] == '-') flag = 1, c++; 20 for(t = 0; c < sz && '0' <= buf[c] && buf[c] <= '9'; c++) t = t * 10 + buf[c] - '0'; 21 if(flag) t = -t; 22 return true; 23 } 24 } 25 26 void dfs(int u){ 27 for(int i=0;i<a[u].size();i++){ 28 dfs(a[u][i]); 29 if(!vis[u]&&!vis[a[u][i]]){ 30 ans++; 31 vis[u]=1; 32 } 33 } 34 } 35 int main(){ 36 int t; 37 IO::begin(); 38 IO::read(t); 39 while(t--){ 40 int n,k; 41 IO::read(n); 42 IO::read(k); 43 int x; 44 for(int i=0;i<=n;i++) a[i].clear(),vis[i]=0; 45 for(int i=1;i<n;i++){ 46 IO::read(x); 47 a[x].push_back(i+1); 48 } 49 ans=0; 50 dfs(1); 51 if(k<=ans*2){ 52 printf("%d\n",(k+1)/2); 53 } 54 else printf("%d\n",ans+(k-ans*2)); 55 } 56 return 0; 57 }