UVA 1218-perfect Service

Source: Internet
Author: User

Summary: There is a tree-like structure of the network, in some nodes to install the server so that the server is not a node around exactly a server, ask the minimum number of servers.

First turn to have a root tree (when enumerating the child nodes ignore their father), and then Dp[u][s] represents the minimum number of child tree installation Server, the impact of the decision is whether you are the server and the state of its parent node, so the value of S is U is the server (0), U is not the server U father is the server (1), U is not the father of the server U nor the server (2).

#include <bits/stdc++.h>using namespacestd;Const intMAXN = 1e4+4;intN;vector<int>G[MAXN];#definePB push_backintdp[maxn][3];voidDfsintUintFA) {dp[u][1] =0; dp[u][0] =1; intSZ =g[u].size (); if(SZ = =0) {dp[u][2] =-MAXN;//illegal        return; }     for(inti =0; I < sz; i++){        intv =G[u][i]; if(v = = FA)Continue;        DFS (V,U); dp[u][0] + = min (dp[v][0],dp[v][1]); dp[u][1] + = dp[v][2]; } dp[u][2] =MAXN; int&AMP;D2 = dp[u][2];  for(inti =0; I < sz; i++){        intv =G[u][i]; if(v = = FA)Continue; D2= Min (d2,dp[u][1]-dp[v][2]+dp[v][0]); }}voidinit () {scanf ("%d",&N);  for(inti =1; I <= N; i++) g[i].clear ();  for(inti =1; I < n;i++){        intU,v; scanf"%d%d",&u,&v); G[u].        PB (v); G[V].    PB (U); }}intMain () {//freopen ("In.txt", "R", stdin);    intFlag;  Do{init (); DFS (1,-1); intans = min (dp[1][0],dp[1][2]); printf ("%d\n", ans); scanf ("%d",&flag); } while(Flag = =0); return 0;}

UVA 1218-perfect Service

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.