UVALive 6432 Influence 搜索 剪枝大法好

有n个人,有k个人可以选作传播疾病的母体,和病人直接接触的未被感染者会被感染,求出选择k个人中的哪个可以取得最多的病人数目,有相同的取编号小的那个。

简单搜索,剪枝是如果一个同为母体的可以被其他母体直接或间接传染,这个母体就肯定不会是最多的那个,只会是一条分支。

#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|1

struct 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;
}
时间: 2024-08-09 02:06:38

UVALive 6432 Influence 搜索 剪枝大法好的相关文章

【搜索剪枝】HDU 5469 Antonidas

通道 题意:给出1字母树,询问一字符串是否出现在该树中 思路:直接搜索剪枝,有人点分治?写了几发都T了..有人会了教我? 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; struct Edge { int v, nxt; Edge () { } Edge (int _v, int _n) { v = _v, nxt = _n; } }; const in

poj 1054 The Troublesome Frog (暴力搜索 + 剪枝优化)

题目链接 看到分类里是dp,结果想了半天,也没想出来,搜了一下题解,全是暴力! 不过剪枝很重要,下面我的代码 266ms. 题意: 在一个矩阵方格里面,青蛙在里面跳,但是青蛙每一步都是等长的跳, 从一个边界外,跳到了另一边的边界外,每跳一次对那个点进行标记. 现在给你很多青蛙跳过后的所标记的所有点,那请你从这些点里面找出 一条可能的路径里面出现过的标记点最多. 分析:先排序(目的是方便剪枝,break),然后枚举两个点,这两个 点代表这条路径的起始的两个点.然后是三个剪枝,下面有. 开始遍历时,

USACO/fence8 迭代加深搜索+剪枝

题目链接 迭代加深搜索思想. 枚举答案K,考虑到能否切出K个木头,那么我们当然选最小的K个来切. 1.对于原材料,我们是首选最大的还是最小的?显然,首选大的能够更容易切出,也更容易得到答案. 2.对于目标木头,我们是优先得到最大的还是最小的?显然,由于K个木头我们都要得到,那么当然先把最大的(最难得到的)先得到,这种搜索策略更优. 3.假设总原材料为all,前K个木头总和为sum,那么all-sum就是这一次切割过程中能[浪费]的最大数目.对于一个切剩下的原材料,若它比最小的目标木头还要小,则它

hdu 5887 搜索+剪枝

Herbs Gathering Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 687    Accepted Submission(s): 145 Problem Description Collecting one's own plants for use as herbal medicines is perhaps one of t

poj 2531 搜索剪枝

Network Saboteur Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Java class name: Main [Submit] [Status] [Discuss] Description A university network is composed of N computers. System administrators gathered informat

【迭代博弈+搜索+剪枝】poj-1568--Find the Winning Move

poj  1568:Find the Winning Move   [迭代博弈+搜索+剪枝] 题面省略... Input The input contains one or more test cases, followed by a line beginning with a dollar sign that signals the end of the file. Each test case begins with a line containing a question mark and

『生日蛋糕 搜索剪枝』

Description 7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为N×π的M层生日蛋糕,每层都是一个圆柱体. 设从下往上数第i(1 <= i <= M)层蛋糕是半径为Ri, 高度为Hi的圆柱.当i < M时,要求Ri > Ri+1且Hi > Hi+1. 由于要在蛋糕上抹奶油,为尽可能节约经费,我们希望蛋糕外表面(最下一层的下底面除外)的面积Q最小. 令Q = S×π请编程对给出的N和M,找出蛋糕的制作方案(适当的Ri和Hi的值),使S最小.(除Q外,以上所

poj 1198 hdu 1401 搜索+剪枝 Solitaire

写到一半才发现可以用双向搜索4层来写,但已经不愿意改了,干脆暴搜+剪枝水过去算了. 想到一个很水的剪枝,h函数为  当前点到终点4个点的最短距离加起来除以2,因为最多一步走2格,然后在HDU上T了,又发现再搜索过程中,这个估价函数应该是递减的(贪心),再加上这个剪枝就过了. #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<list> #

HDU 1010 Tempter of the Bone 骨头诱惑(AC代码)DFS搜索+剪枝法

参考了别人的思路:将迷宫外围四面都筑墙‘X’.方便减少代码量. 1 #include <iostream> 2 #include <vector> 3 #include <string> 4 #include <math.h> 5 using namespace std; 6 vector<string> v; 7 int n,m; 8 int x_1,y_1,x_2,y_2; 9 bool maze(int x,int y,int t) //目