1. 程式人生 > >刷題總結——拆網線(noip模擬 貪心)

刷題總結——拆網線(noip模擬 貪心)

lib pen n) ems true 情況 for clu time

題目:

給定一顆樹··在保證有k個點與其它點連接的情況下問最少保留多少條邊····

樹的節點樹n和k均小於100000;

題解:

很容易看出來我們要盡量保留那種一條邊連兩個節點的情況····

然後考試的時候我以為這就完了··xjb貪完心後錯了一大半····

下次一定要寫對拍了,艹

貪心的時候我們要沿著葉子節點來貪心···這樣就能保證正確性了···證明的話就不細說了··不信的話打個對拍看看···

代碼:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<cctype>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e5+5;
int T,n,K,fst[N],nxt[N*2
],go[N*2],tot,cnt,keep; bool del[N]; inline int R() { char c;int f=0; for(c=getchar();c<0||c>9;c=getchar()); for(;c<=9&&c>=0;c=getchar()) f=(f<<3)+(f<<1)+c-0; return f; } inline void comb(int a,int b) { nxt[++tot]=fst[a],fst[a]=tot,go[tot]=b; nxt[
++tot]=fst[b],fst[b]=tot,go[tot]=a; } inline void pre() { cnt=tot=keep=0; memset(fst,0,sizeof(fst)); memset(del,false,sizeof(del)); } inline void dfs(int u,int fa) { for(int e=fst[u];e;e=nxt[e]) { int v=go[e];if(v==fa) continue; dfs(v,u); if(!del[v]&&!del[u]) { if(keep<K) { keep+=2;cnt++; del[v]=del[u]=true; } } } } int main() { //freopen("a.in","r",stdin); T=R();int a; while(T--) { n=R(),K=R();pre(); for(int i=1;i<n;i++) a=R(),comb(i+1,a); dfs(1,0); if(keep>=K) cout<<cnt<<"\n"; else cout<<cnt+(K-keep)<<"\n"; } return 0; }

刷題總結——拆網線(noip模擬 貪心)