URAL 1242 Werewolf(DFS)

本文介绍了一种通过家族关系寻找村庄中狼人的算法实现。利用递归遍历村民间的亲属关系,结合已知的受害者名单,可以有效缩小嫌疑人范围。

Werewolf

Time limit: 1.0 second
Memory limit: 64 MB
Knife. Moonlit night. Rotten stump with a short black-handled knife in it. Those who know will understand. Disaster in the village. Werewolf.
There are no so many residents in the village. Many of them are each other's relatives. Only this may help to find the werewolf. The werewolf is merciless, but his descendants never become his victims. The werewolf can drown the village in blood, but he never kills his ancestors.
It is known about all the villagers who is the child of whom. Also, the sad list of the werewolf's victims is known. Your program should help to determine the suspects. It would be a hard task, if a very special condition would not hold. Namely, citizens of the village are not used to leave it. If some ancestor of some citizen lives in the village, then also his immediate ancestor does. It means, that, for example, if the father of the mother of some citizen still lives in the village, than also his mother still lives.

Input

The first line contains an integer N, 1 < N ≤ 1000, which is the number of the villagers. The villagers are assigned numbers from 1 to N. Further is the description of the relation "child-parent": a sequence of lines, each of which contains two numbers separated with a space; the first number in each line is the number of a child and the second number is the number of the child's parent. The data is correct: for each of the residents there are no more than two parents, and there are no cycles. The list is followed by the word "BLOOD" written with capital letters in a separate line. After this word there is the list of the werewolf's victims, one number in each line.

Output

The output should contain the numbers of the residents who may be the werewolf. The numbers must be in the ascending order and separated with a space. If there are no suspects, the output should contain the only number 0.

Samples

inputoutput
8
1 3
3 6
4 5
6 2
4 6
8 1
BLOOD
3
8
4 5 7
6
1 2
3 2
1 4
3 4
2 6
5 2
5 4
BLOOD
2
5
0
Problem Author: Leonid Volkov
【分析】简单的递归。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define inf 0x3f3f3f3f
#define mod 10000
typedef long long ll;
using namespace std;
const int N=1005;
const int M=100005;
int n,m,k=0,a,b;
int vis[N];
char str[20];
bool flag=false;
vector<int>vec,parent[N],chil[N];
void dfs(int u,int f)
{//printf("!!!%d %d\n",u,f);
    vis[u]=1;
    if(!f){
        for(int i=0;i<parent[u].size();i++){
                //printf("@@@%d\n",parent[u][i]);
            if(!vis[parent[u][i]])dfs(parent[u][i],f);
        }
    }
    else {
        for(int i=0;i<chil[u].size();i++){
            if(!vis[chil[u][i]])dfs(chil[u][i],f);
        }
    }
}
void in(char *ch){
    int fff=0,aa=0,bb=0;
    char A[20],B[20];
    for(int i=0;i<strlen(ch);i++){
        if(ch[i]==' '){fff=1;continue;}
        if(!fff)A[aa++]=ch[i];
        else B[bb++]=ch[i];
    }
    A[aa]=0;B[bb]=0;
    a=atoi(A);
    b=atoi(B);
    return;
}
int main() {
    scanf("%d",&n);getchar();

    while(gets(str)!=NULL){k++;
        if(str[0]=='B'){flag=true;continue;}
        in(str);
        if(!flag)parent[a].push_back(b),chil[b].push_back(a);
        if(flag)vec.push_back(a);//if(k>=9)break;
    }
    for(int i=0;i<vec.size();i++){
        int v=vec[i];
        dfs(v,0);
        dfs(v,1);
    }
    flag=false;
    for(int i=1;i<=n;i++)if(!vis[i])printf("%d ",i),flag=true;
    if(!flag)printf("0");
    printf("\n");
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/jianrenfang/p/5875204.html

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值