【每日一题】旅游
旅游
https://ac.nowcoder.com/acm/problem/15748
题意:
思路:
#include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int N = 5e5 + 10; struct Node{ int to,nex; }e[N << 1]; int n,s,head[N],idx; int dp[N][2]; void add_edge(int u,int v){ e[idx].to = v; e[idx].nex = head[u]; head[u] = idx++; } void dfs(int u,int fa){ dp[u][1] = 1; for(int i = head[u];~i;i = e[i].nex){ int v = e[i].to; if(v == fa) continue; dfs(v,u); dp[u][0] += max(dp[v][1],dp[v][0]); dp[u][1] += dp[v][0]; } } int main(){ memset(head,-1,sizeof(head)); scanf("%d%d",&n,&s); for(int i = 1,u,v;i < n;i++){ scanf("%d%d",&u,&v); add_edge(u,v); add_edge(v,u); } dfs(s,-1); printf("%d\n",dp[s][1]); return 0; }