There are n people, and K people can choose to be the mother of the disease, and the uninfected people who are in direct contact with the patient will be infected, find out which of the K people can obtain the most number of patients, there are the same ones with small numbers.
Simple search: pruning means that if the same mother can be directly or indirectly transmitted by other mothers, this mother will certainly not be the most, but will only be a branch.
#include<cstring>#include<string>#include<fstream>#include<iostream>#include<iomanip>#include<cstdio>#include<cctype>#include<algorithm>#include<queue>#include<map>#include<set>#include<vector>#include<stack>#include<ctime>#include<cstdlib>#include<functional>#include<cmath>using namespace std;#define PI acos(-1.0)#define MAXN 10100#define eps 1e-7#define INF 0x7FFFFFFF#define LLINF 0x7FFFFFFFFFFFFFFF#define seed 131#define MOD 1000000007#define ll long long#define ull unsigned ll#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1struct node{ int v,next;}edge[250000];int head[5010],a[5010],vis[5010];int num[5100];int cnt,ans,maxm,sum;void add_edge(int u,int v){ edge[cnt].v = v; edge[cnt].next = head[u]; head[u] = cnt++;}int dfs(int u){ int i,j; int sum=1; vis[u]=1; for(i=head[u];i!=-1;i=edge[i].next){int v=edge[i].v;if(num[v]){num[v]=0;} if(!vis[v]){sum+=dfs(v);} } return sum;}int main(){ char c; int n,i,j,x,t,m; while(scanf("%d%d",&n,&m)!=EOF){memset(num,0,sizeof(num)); memset(head,-1,sizeof(head)); cnt = 0; maxm=0; for(i=0;i<m;i++){ scanf("%d",&a[i]); num[a[i]]=1; } for(i=0;i<n;i++){ scanf("%d",&x); while(1){ c = getchar(); if(c=='\n') break; scanf("%d",&t); add_edge(x,t); } } for(i=0;i<m;i++){if(num[a[i]]){ for(j=1;j<=n;j++){ vis[j] = 0; } int sum=dfs(a[i]); if(sum>maxm){ maxm = sum; ans = a[i]; }} } printf("%d\n",ans); } return 0;}
The uvalive 6432 influence search pruning algorithm is good.