P1101 Infectious disease prevention and control Accepted Tags: search and pruning Noip raise group 2003
Description
The study shows that the spread of this kind of infectious disease has two very special properties;
The first is that it is transmitted in tree form, and a person X may be infected only by a certain person, as long as Y does not
disease, or if the transmission pathway between XY is cut off, X will not get sick.
Second, the spread of the disease is cyclical, and within a cycle of disease transmission, infectious diseases will only infect a
Instead of being transmitted to the next generation.
These properties have greatly reduced the pressure on disease Control and Prevention in Penglai, and they have got some susceptible people in the country
Map of potential routes of transmission (a tree). But the trouble is not over yet. Due to the inadequate staffing of the Peng Lai state CDC and the lack of strong technology, they can only try to cut off one route of transmission during a disease transmission cycle, and uncontrolled transmission routes will cause more susceptible groups to be infected (that is, linked to the current infected person's transmission path, And the connection path is not cut off by the crowd). When there is no possibility of a healthy person being infected, the disease stops spreading. Therefore, the Penglai state CDC to develop a cut off the route of transmission in order to make as few people are infected. Your program should target a given tree and find out the proper cut-off sequence. format Input Format
The first line of the input format is two integers n (1≤n≤300) and P. Next P line, each row has two integers i
and J, which means that the nodes I and J are connected by a side (meaning that there is a transmission pathway between the person I and J). Where the node
1 are patients who have already been infected. output Format
Only one row, output the total number of infected people. Example 1 sample input 1[copy]
7 6
1 2
1 3
2 4
2 5
3 6
3 7
sample Output 1[copy]
3
Limit
1s source per test point
The fourth question of NOIP2003
Parse: Search, for a tree, the root node is infected, enumerate the severed subtrees, and then update the current infected node.
It is important to note that, each time the number of words cut directly select the most nodes, this greedy idea is wrong, such as the following situation:
Code:
#include <cstdio> #include <cstring> #include <algorithm> #define MAXN using namespace std;
int N,M,ANS=MAXN;
int f[maxn+20][maxn+20],sum[maxn+20],q[maxn+20];
BOOL FLAG[MAXN+20];
void Build_tree (int s) {int i,j=0,k;
Sum[s]=1,flag[s]=1;
for (i=1;i<=f[s][0];i++) {k=f[s][i];
if (!flag[k]) {f[s][++j]=k;
Build_tree (k);
SUM[S]+=SUM[K];
}} f[s][0]=j;
} void Dfs (int l,int r,int s) {if (S>=ans) return;
int i,j,k;
for (k=r,i=l;i<=k;i++) if (!flag[q[i]]) for (j=1;j<=f[q[i]][0];j++) q[++r]=f[q[i]][j];
if (r-k-1<=0) {ans=min (ans,s);
Return
} s+=r-k-1;
for (i=k+1;i<=r;i++) {flag[q[i]]=1;
DFS (K+1,R,S);
flag[q[i]]=0;
}} int main () {//freopen ("1.in", "R", stdin);
int i,j,k,l,r,x,y,p;
scanf ("%d%d", &n,&m);
for (i=1;i<=m;i++) {scanf ("%d%d", &j,&k);
F[j][++f[j][0]]=k; f[K][++f[k][0]]=j;
} build_tree (1);
Q[0]=1,q[1]=1;
memset (flag,0,sizeof (flag));
DFS (1,1,1);
printf ("%d\n", ans);
return 0; }