• 【POJ 3398】Perfect Service


    Perfect Service
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 1922   Accepted: 921

    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

    [Submit]   [Go Back]   [Status]   [Discuss]

    题解:来不及写啦,copy来的,老贾原谅我嘤嘤嘤昨晚找bug找了好长时间

    #include<cstdio>
    #include<iostream>
    #include<cmath>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    typedef long long ll;
    using namespace std;
    const int maxn=11000;
    const int INF=1e6;
     
    struct Edge
    {
        int to,next;
    }edge[maxn*2];
     
    int Adj[maxn],Size;
     
    int dp[maxn][3],n;
     
    void init()
    {
        Size=0; 
        for(int i=0;i<=n+10;i++)
        {
            Adj[i]=-1;
            dp[i][0]=1; dp[i][1]=0; dp[i][2]=INF;
        }
    }
     
    void add_edge(int u,int v)
    {
        edge[Size].to=v;
        edge[Size].next=Adj[u];
        Adj[u]=Size++;
    }
     
    void dfs(int u,int fa)
    {
        for(int i=Adj[u];~i;i=edge[i].next)
        {
            int v=edge[i].to;
            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]=min(dp[u][2],dp[v][0]-dp[v][2]);
        }
        dp[u][2]+=dp[u][1];
    }
     
    int main()
    {
    
        while(scanf("%d",&n)!=EOF)
        {
            if(n==-1) break;
            else if(n==0) continue;
            init();
            for(int i=0;i<n-1;i++)
            {
                int a,b;
                scanf("%d%d",&a,&b);
                add_edge(a,b); add_edge(b,a);
            }
            dfs(1,0);
            printf("%d
    ",min(dp[1][0],dp[1][2]));
        }
        
        return 0;
    } 
  • 相关阅读:
    八皇后 c++
    筛法求素数
    3月13号周练——2015 Multi-University Training Contest 9
    Mac搭建Git服务器—开启SSH
    push自定义动画
    学习:二维码、QR码、J4L-QRCode、java
    Java注解Annotation详解
    IOS 基于APNS消息推送原理与实现(JAVA后台)
    IOS学习笔记—苹果推送机制APNs
    linux yum命令详解
  • 原文地址:https://www.cnblogs.com/wuhu-JJJ/p/11243094.html
Copyright © 2020-2023  润新知