UVa1218 Perfect service[树规]

来源:互联网 发布:双休日算法定节假日吗 编辑:程序博客网 时间:2024/06/05 05:33

A network is composed of N computers connected by N - 1 communication links such that any two computers can be communicated via a unique route. Two computers are said to be adjacent if there is a communication link between them. The neighbors of a computer is the set of computers which are adjacent to it. In order to quickly access and retrieve large amounts of information, we need to select some computers acting as servers to provide resources to their neighbors. Note that a server can serve all its neighbors. A set of servers in the network forms a perfect service if every client (non-server) is served by exactly one server. The problem is to find a minimum number of servers which forms a perfect service, and we call this number perfect service number.
We assume that N (10000) is a positive integer and these N computers are numbered from 1 to N . For example, Figure 1 illustrates a network comprised of six computers, where black nodes represent servers and white nodes represent clients. In Figure 1(a), servers 3 and 5 do not form a perfect service because client 4 is adjacent to both servers 3 and 5 and thus it is served by two servers which contradicts the assumption. Conversely, servers 3 and 4 form a perfect service as shown in Figure 1(b). This set also has the minimum cardinality. Therefore, the perfect service number of this example equals two.

Your task is to write a program to compute the perfect service number.
Input
The input consists of a number of test cases. The format of each test case is as follows: The first line contains one positive integer, N , which represents the number of computers in the network. The next N - 1 lines contain all of the communication links and one line for each link. Each line is represented by two positive integers separated by a single space. Finally, a 0' at the (N + 1) -th line indicates the end of the first test case.
The next test case starts after the previous ending symbol
0’. A `-1’ indicates the end of the whole inputs.
Output
The output contains one line for each test case. Each line contains a positive integer, which is the perfect service number.
Sample Input
6
1 3
2 3
3 4
4 5
4 6
0
2
1 2
-1
Sample Output
2
1

题意:
对一棵给定的树,求所有使得未被选择的点相连的点里都有且只有一个点被选择时,最小选择的点数。
分析:
第二维表示三种情况,被儿子覆盖,被父亲覆盖,自己覆盖自己。这道题的转移方程和最小点覆盖稍有不同,因为相连的点里只能有一个点被选。感觉这道题对于理清思路很有用。
有坑:说好的每个数据一行,但如果输入节点是0的树不换行,被WA了好几次,简直。

#include<cstdio>#include<iostream>#include<cmath>#include<cstdlib>#include<cstring>#include<string>#include<algorithm>#include<queue>#include<set>#include<map>#include<stack>#include<vector>#include<ctime>#define ll long long #define inf 2e8#define modd 1e9+7#define clr(x) memset(x,0,sizeof(x))#define maxen(x) memset(x,127,sizeof(x))#define maxer(x) memset(x,31,sizeof(x))#define each(i,n) for(int i=1;i<=n;i++)#define minn(a,b,c) min(a,min(b,c))#define maxx(a,b,c) max(a,max(b,c))#ifdef WIN32#define lld "%I64d"#else#define lld "%lld"#endif#define PROC "UVa1218"//for(int i=1;i<=n;i++)//(double) (ll) LL (int)//(double)clock()/CLOCKS_PER_SECusing namespace std;const int Maxn=1e4+5;int sta,fin;int n,f[Maxn][3],fr[Maxn],tov[2*Maxn],des[2*Maxn];int read(){    int x=0,f=1;char ch=getchar();    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}    return x*f;}void addedge(int cur){    sta=read();    fin=read();    int tmp1=2*cur-1;    int tmp2=2*cur;    tov[tmp1]=fr[sta];tov[tmp2]=fr[fin];    fr[sta]=tmp1;fr[fin]=tmp2;    des[tmp1]=fin;des[tmp2]=sta;}void init(){    int tmp;    n=read();    clr(fr);    each(i,n-1)        addedge(i);    clr(f);}void dfs(int cur,int fa){    int mini=Maxn;    for(int i=fr[cur];i;i=tov[i])    if(des[i]!=fa){        dfs(des[i],cur);        f[cur][0]+=f[des[i]][0];        f[cur][1]+=min(f[des[i]][1],f[des[i]][2]);        f[cur][2]+=f[des[i]][0];        mini=min(mini,f[des[i]][1]-f[des[i]][0]);    }    if(mini!=Maxn)f[cur][0]+=mini;    if(!f[cur][0])f[cur][0]=Maxn;    f[cur][1]++;}void work(){    if(n==0)return;    if(n==-1)return;    dfs(1,1);    printf("%d\n",min(f[1][0],f[1][1]));}void debug(){    //}int main(){    freopen(PROC".in","r",stdin);    freopen(PROC".out","w",stdout);    while(n!=-1){    init();    work();    scanf("%d",&n);    }    //debug();    return 0;}
0 0
原创粉丝点击