LuoguP1922 女仆咖啡厅桌游吧 (树形动态规划)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int  a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int  a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long
#define ON_DEBUG

#ifdef ON_DEBUG

#define D_e_Line printf("\n\n----------\n\n")
#define D_e(x)  cout << #x << " = " << x << endl
#define Pause() system("pause")

#else

#define D_e_Line ;

#endif

struct ios{
    template<typename ATP>ios& operator >> (ATP &x){
        x = 0; int f = 1; char c;
        for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-')  f = -1;
        while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
        x*= f;
        return *this;
    }
}io;
using namespace std;

const int N = 100007;

struct Edge{
	int nxt, pre;
}e[N<<1];
int head[N], cntEdge;
inline void add(int u,int v){
	e[++cntEdge] = (Edge){head[u], v}, head[u] = cntEdge;
}

int ans;
inline int DFS(int u, int fa){
	int siz = 1;
	int flag = false;
	for(register int i = head[u]; i; i = e[i].nxt){
		int v = e[i].pre;
		if(v == fa) continue;
		siz += DFS(v, u);
		flag = true;
	}
	if(flag == true){
		ans += siz >> 1;	
		return 0;
	}
	else{
		return 1;
	}
}
int deg[N];
int main(){
	int n;
	io >> n;
	R(i,2,n){
		int u, v;
		io >> u >> v;
		add(u, v);
		add(v, u);
		++deg[u], ++deg[v];
	}
	
	int root;
	R(i,1,n){
		if(deg[i]){
			root = i;
			break;
		}
	}
	
	DFS(root, root);
	
	printf("%d", ans);
	
	return 0;
}

posted @ 2019-07-18 11:01  邱涵的秘密基地  阅读(182)  评论(0编辑  收藏  举报