Luogu P1041 Infectious Disease Control

The original title

Topic background

Recently, a new epidemic raging around the world. Penglai country also found sporadic infection, the disease is prevalent in a wide range of Penglai country in order to prevent the Government decided at all costs to control the spread of infectious diseases. Unfortunately, because people have not yet fully aware of the epidemic, it is difficult to accurately determine carriers of the virus, and no vaccine developed to protect vulnerable populations. So, Penglai States Centers for Disease Control decided to cut off the transmission method of controlling the spread of disease. Through the efforts of WHO (World Health Organization) as well as countries around the world of scientific research departments, transmission and control method of this emerging infectious diseases research has been clear, the remaining task is to assist you in Penglai States Center for Disease Control to develop an effective control measures.

Title Description

Studies have shown that the spread of the epidemic has two very special nature;

The first is its means of transmission is a tree, a man the X- the X- could only have been a particular person Y Y infection, as long as Y Y not sick, or XY * transmission between * X ** Y is cut off , then the X- the X- would not get sick.

The second is the spread of the disease is cyclical, within a period of spread of the disease, patients with infectious diseases would only infect a generation, and will not spread to the next generation.

These properties greatly reduce the pressure on the country Penglai disease prevention and control, and they've got a potential transmission method diagram (a tree) domestic part of the susceptible population. However, the trouble is not over yet. As the country Penglai CDC staff shortage, but also the lack of strong technology, so that they are in a cycle of disease transmission, only managed to cut off a route of transmission, and the transmission is not controlled it will cause more susceptible populations are infection (that is, with the current people have already been infected with connected transmission, and the connection is cut off approach is not the crowd). When you can not have healthy people are infected, the disease spread to abort. So, country Penglai CDC to work out a cut off the transmission of the order, so that as few people have been infected.

Your program for a given tree, find the right cut order.

Input Format

The first line is two integers n and P .
Next p rows, each row has 22 integers i and j , represent the nodes i and j have inter-connected side. (Which means, the i and the first j has a transmission connected between humans). Node 1 is where the patient has been infected.

Output Format

, Total number of people infected Line 1.

Sample input and output

Entry

7 6
1 2
1 3
2 4
2 5
3 6
3 7

Export

3

Description / Tips

To 100% of the data,. 1 \ (\ Leq \) n- \ (\ Leq \) 300.

answer

(Very bored at home during the solution to a problem and write a virus related topics to increase their resistance (?))

First consider greedy, select a time value for the maximum size of the nodes excised, but greedy there is a problem, that is, if the child was a tree of a chain can not be at their best

Secondly, if you go to look at the original title of, you will find this question proved to be polynomial time algorithm can not find the standard of complexity to solve (P class is not a problem), so we can only consider the search. (N <= 300 is to give you the search)

Although randomization should be able to do before, but here I was given only aMore prudentThe search violence, interested dalao can try random what of

First deal out of this tree size, fa and dep with dfs1

Followed by depth index node to add ly inside

Finally dfs2 search violence, pay attention to the depth of experience> maximum depth or the entire layer circumstances have been blocked off to immediately update the value and return ans

AC Code:

#include <bits/stdc++.h>
using namespace std;
const int maxn=1926;
vector<int> gpe[maxn],ly[maxn];
int dep[maxn],size[maxn],fa[maxn],n,p;
int maxdep=-1,ans=0;
void dfs1(int u,int f,int d){
    size[u]=1;
    dep[u]=d;
    fa[u]=f;
    for(int i=0;i<gpe[u].size();i++){
        int v=gpe[u][i];
        if(v==f) continue;
        dfs1(v,u,d+1);
        size[u]+=size[v];
    }
}
int vis[maxn];
void dfs2(int d,int now){
    if(d>maxdep){
        ans=min(ans,now);
        return;
    }
    for(int i=0;i<ly[d].size();i++){
        if(vis[fa[ly[d][i]]]){
            vis[ly[d][i]]=1;
        }else{
            vis[ly[d][i]]=0;
        }
    }
    int flag=1;
    for(int i=0;i<ly[d].size();i++){
        if(!vis[ly[d][i]]) flag=0;
    }
    if(flag){
        ans=min(ans,now);
        return;
    }
    for(int i=0;i<ly[d].size();i++){
        if(vis[ly[d][i]]) continue;
        vis[ly[d][i]]=1;
        dfs2(d+1,now-size[ly[d][i]]);
        vis[ly[d][i]]=0;
    }
}
int main(void){
    scanf("%d %d",&n,&p);
    for(int i=1;i<=p;i++){
        int u,v;
        scanf("%d %d",&u,&v);
        gpe[v].push_back(u);
        gpe[u].push_back(v);
    }
    dfs1(1,1,0);
    for(int i=1;i<n;i++){
        ly[dep[i]].push_back(i);
        maxdep=max(maxdep,dep[i]);
    }
    ans=n;
    dfs2(1,n);
    printf("%d",ans);
}

Guess you like

Origin www.cnblogs.com/jrdxy/p/12398610.html