POJ 3659 Cell Phone Network (tree dp)

Source: Internet
Author: User

Title Link: http://poj.org/problem?id=3659

Give you a tree chart, a dot can cover the points around him, so that you cover all points with the fewest points.

Dp[i][0] means overwrite with I point, dp[i][1] means overwrite with child node, dp[i][2] means overwrite with parent node

(1) dp[i][0] = min (dp[i.son][0], dp[i.son][1], dp[i.son][2])

(2) dp[i][1] = min (dp[i.son][0], dp[i.son][1])//Special award

(3) dp[i][2] = min (dp[i.son][0], dp[i.son][1])

Note (2) that because I needs to be overwritten, dp[i.son][0] needs to take at least one.

1 //#pragma COMMENT (linker, "/stack:102400000, 102400000")2#include <algorithm>3#include <iostream>4#include <cstdlib>5#include <cstring>6#include <cstdio>7#include <vector>8#include <cmath>9#include <ctime>Ten#include <list> One#include <Set> A#include <map> - using namespacestd; -typedefLong LongLL; thetypedef pair <int,int>P; - Const intN = 1e4 +5; - intdp[n][3]; - structEdge { +     intnext, to; -}edge[n <<1]; + intHead[n], CNT; A  atInlinevoidAddintUintv) { -Edge[cnt].next =Head[u]; -Edge[cnt].to =v; -Head[u] = cnt++; - } -  in voidDfsintUintp) { -dp[u][0] =1, dp[u][1] = dp[u][2] =0; to     BOOLFlag =false; +     intMin =10000; -      for(inti = Head[u]; ~i; i =Edge[i].next) { the         intv =edge[i].to; *         if(v = =p) $             Continue;Panax Notoginseng Dfs (v, u); -dp[u][0] + = min (dp[v][0], Min (dp[v][1], dp[v][2])); thedp[u][2] + = min (dp[v][1], dp[u][0]); +         if(dp[v][0] <= dp[v][1]) { AFlag =true; thedp[u][1] + = dp[v][0]; +}Else { -dp[u][1] + = dp[v][1]; $min = min (dp[v][0]-dp[v][1], Min); $         } -     } -     if(!flag)//If you don't get dp[v][0] thedp[u][1] +=Min; - }Wuyi  the intMain () - { Wu     intN, u, v; -      while(~SCANF ("%d", &N)) { AboutMemset (Head,-1,sizeof(head)); $CNT =0; -          for(inti =1; I < n; ++i) { -scanf"%d%d", &u, &v); - Add (U, v); A Add (V, u); +         } theDfs1, -1); -printf"%d\n", Min (dp[1][0], dp[1][1])); $     } the     return 0; the}

POJ 3659 Cell Phone Network (tree dp)

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.