最小支配集--pojPerfect Service

来源:互联网 发布:au是什么软件 编辑:程序博客网 时间:2024/06/03 02:26

Perfect Service
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 1739 Accepted: 844
Description

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
Source

Kaohsiung 2006

/*    最小支配集 ,即在一棵树中,选择覆盖一个点,则该点的邻居也会被覆盖    整棵树中,选择最少的点,使得所有的点被覆盖    poj 3398 最小支配集裸题 */#include<iostream>#include<stdio.h>#include<algorithm>#include<string.h>using namespace std;const int MAXN = 20010;struct EdgeNode{    int to;    int next;}Edges[MAXN];int Head[MAXN];//前向星建图 int father[MAXN];//记录i的父节点 int Pos[MAXN];//记录深搜时第i个访问到的节点 bool vis[MAXN];//标记节点是否访问过 bool S[MAXN];//true 代表第i个点被覆盖了 bool Set[MAXN];//true 代表第i个点属于支配集 int N,M,cnt;void DFS(int x){    Pos[cnt++] = x;    for(int k = Head[x]; k != -1; k = Edges[k].next)    {        if(!vis[Edges[k].to])        {            vis[Edges[k].to] = true;            father[Edges[k].to] = x;            DFS(Edges[k].to);        }    }}int Greedy()//贪心求最小支配集{    memset(S,0,sizeof(S));    memset(Set,0,sizeof(Set));    int ans = 0;    for(int i = N-1; i >= 0; i--)//反向序列检查    {        int t = Pos[i];        if(!S[t])//当前点未被覆盖,也就是当前点既不属于支配集,也不与支配集中的点相连        {            if(!Set[father[t]])//当前点的父亲结点不属于支配集,            {                Set[father[t]] = true;  //将父节点加入支配集                ans++;                  //顶点个数加1            }            //执行到这里,t的父节点已经属于支配集了            //所以要讲t,t的父节点,t的父节点的父节点覆盖掉             S[t] = true;            S[father[t]] = true;            S[father[father[t]]] = true;        }    }    return ans;}int main(){//  ios::sync_with_stdio(false);//  freopen("in.txt","r",stdin);    int u,v,nxt=0;    while(1)    {        if(nxt == -1) break;        scanf("%d",&N);         //初始化        memset(Edges,0,sizeof(Edges));        memset(Head,-1,sizeof(Head));        memset(father,0,sizeof(father));        memset(vis,false,sizeof(vis));        memset(Pos,0,sizeof(Pos));        int id = 0;        for(int i = 0; i < N-1; ++i)        {            scanf("%d%d",&u,&v);            Edges[id].to = v;            Edges[id].next = Head[u];            Head[u] = id++;            Edges[id].to = u;            Edges[id].next = Head[v];            Head[v] = id++;        }        cnt = 0;        vis[1] = true;        father[1] = 1;        DFS(1);        printf("%d\n",Greedy());        cin >> nxt;    }    return 0;}
原创粉丝点击