洛谷P2899 [USACO08JAN]手机网络Cell Phone Network

#include
#define N 100100
using namespace std;
const int INF=1008610086;
int A[N],B[N],C[N];
int beg[N],to[N],nex[N];
int n,len;
inline void Add(int a,int b){
    nex[++len]=beg[a],beg[a]=len,to[len]=b;
    nex[++len]=beg[b],beg[b]=len,to[len]=a;
}
void dfs(int p,int fa){
    A[p]=B[p]=C[p]=0;
	int mib=1;
	int f=0;
	for(int i=beg[p];i;i=nex[i]){
		if(to[i]==fa) continue;
		dfs(to[i],p);
		if(A[to[i]]<=B[to[i]]){
			A[p]+=A[to[i]];
			mib=min(mib,B[to[i]]-A[to[i]]);
		}
		else A[p]+=B[to[i]],f=1;
		C[p]+=min(B[to[i]],A[to[i]]);
		B[p]+=min(C[to[i]],min(B[to[i]],A[to[i]]));
	}
	B[p]++;
	if(!f) A[p]+=mib;
	if(!A[p]) A[p]=1;
}
int main(){
    scanf("%d",&n);
    for(int i=1;i

你可能感兴趣的:(dp,树,洛谷,dp,树,洛谷)