Bzoj1596: [usaco2008 Jan] Telephone Network

Source: Internet
Author: User

 

1596: [usaco 162 Jan] Telephone Network Time Limit: 10 sec memory limit: MB
Submit: 513 solved: 232
[Submit] [Status] Description

Farmer John decided to equip all his cows with cell phones to encourage them to communicate with each other. However, for this reason, FJ must select some radio communication towers in the N (1 <= n <= 10,000) lawn where the cows live to ensure that there is a mobile phone signal between any two lawns. All n lawns are numbered by 1 .. n sequentially. Only N-1 pairs in All lawns are adjacent, but for any two lawns A and B (1 <= A <= N; 1 <= B <= N;! = B), you can find a lawn Sequence starting with a and ending with B, and the adjacent numbers in the sequence represent adjacent lawns. A radio communication tower can only be built on the lawn. A tower serves the lawn where it is located and all the lawns adjacent to the lawn. Please help FJ calculate the minimum number of radio communication towers it will need to build a communication system that can cover all lawns.

Input

* Row 1st: 1 integer, n

* Row 2nd. N: two integers, A and B, separated by spaces, are the numbers of the two adjacent lawns.

Output

* Row 1st: output an integer, that is, the minimum number of radio communication towers created by FJ.

Sample input5
1 3
5 2
4 3
3 5

Input description:

Farmer John's farm has five lawns: grass 1 and grass 3 adjacent, grass 5 and grass 2, grass
4 and 3, 3 and 5. The relationship between the positions of lawns is as follows:
(Or other similar shapes)
4 2
|
1--3--5


Sample output2

Output description:

FJ can choose to build a communication tower on lawn 2 and lawn 3, or lawn 3 and lawn 5.
Hintsource

Gold

Question:

DP of water and water, handling question:

Minimum decision set

This is a classic problem. Just use the tree-like DP.

F [I] [0]: the minimum number of sub-trees with I as the root, all of which are covered and no signal tower is required on the lawn I (I is covered by my son)

F [I] [1]: the minimum number of towers required to cover all the sub-trees with I as the root and to have a signal tower on the lawn I

F [I] [2]: the minimum number of towers required for all vertices except I in the subtree rooted in I

Transfer Method:

F [I] [1] = sigma (min (F [I. Son] [0 .. 2]) (obviously)

F [I] [2] = sigma (F [I. Son] [0]) (obviously)

Make sum = sigma (min (F [I. Son] [0. 1])

F [I] [0] = min (F [I. son] [1] + sum-min (F [I. son] [0 .. (1]) (this is more complicated. because I must be covered by my son, I must have a son on the tower, and the rest of the sons are taken at the minimum value)

Note that when I is a leaf node, F [I] [0] is obviously invalid, so it should be set to infinity.

Code:

  1 #include<cstdio>  2   3 #include<cstdlib>  4   5 #include<cmath>  6   7 #include<cstring>  8   9 #include<algorithm> 10  11 #include<iostream> 12  13 #include<vector> 14  15 #include<map> 16  17 #include<set> 18  19 #include<queue> 20  21 #include<string> 22  23 #define inf 1000000000 24  25 #define maxn 100000+1000 26  27 #define maxm 500+100 28  29 #define eps 1e-10 30  31 #define ll long long 32  33 #define pa pair<int,int> 34  35 #define for0(i,n) for(int i=0;i<=(n);i++) 36  37 #define for1(i,n) for(int i=1;i<=(n);i++) 38  39 #define for2(i,x,y) for(int i=(x);i<=(y);i++) 40  41 #define for3(i,x,y) for(int i=(x);i>=(y);i--) 42  43 #define mod 1000000007 44  45 using namespace std; 46  47 inline int read() 48  49 { 50  51     int x=0,f=1;char ch=getchar(); 52  53     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} 54  55     while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();} 56  57     return x*f; 58  59 } 60 struct edge{int go,next;}e[2*maxn]; 61 int n,tot,head[maxn],f[maxn][3]; 62 bool v[maxn]; 63 inline void insert(int x,int y) 64 { 65     e[++tot].go=y;e[tot].next=head[x];head[x]=tot; 66     e[++tot].go=x;e[tot].next=head[y];head[y]=tot; 67 } 68 void dfs(int x) 69 { 70     int tmp=n;bool flag=0; 71     v[x]=1;f[x][0]=1; 72     for(int i=head[x],y;i;i=e[i].next) 73         if(!v[y=e[i].go]) 74         { 75             flag=1; 76             dfs(y); 77             f[x][0]+=min(f[y][0],min(f[y][1],f[y][2])); 78             f[x][1]+=min(f[y][0],f[y][1]);tmp=min(tmp,f[y][0]-f[y][1]); 79             f[x][2]+=f[y][1]; 80         } 81     if(tmp>0)f[x][1]+=tmp;     82     if(!flag)f[x][1]=n; 83 }     84  85 int main() 86  87 { 88  89     freopen("input.txt","r",stdin); 90  91     freopen("output.txt","w",stdout); 92  93     n=read(); 94     for1(i,n-1)insert(read(),read()); 95     dfs(1); 96     printf("%d\n",min(f[1][0],f[1][1])); 97  98     return 0; 99 100 }
View code

 

Bzoj1596: [usaco2008 Jan] Telephone Network

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.