Kernel Knights

题目链接:

http://acm.hust.edu.cn/vjudge/contest/127407#problem/K

Description


Jousting is a medieval contest that involves people on horseback trying to strike each other with wooden
lances while riding at high speed. A total of 2n knights have entered a jousting tournament — n knights
from each of the two great rival houses. Upon arrival, each knight has challenged a single knight from
the other house to a duel.
A kernel is defined as some subset S of knights with the following two properties:
• No knight in S was challenged by another knight in S.
• Every knight not in S was challenged by some knight in S.
Given the set of the challenges issued, find one kernel. It is guaranteed that a kernel always exists.

Input


The input file contains several test cases, each of them as described below.
The first line contains an integer n (1 ≤ n ≤ 100000) — the number of knights of each house. The
knights from the first house are denoted with integers 1 through n, knights from the second house with
integers n + 1 through 2n.
The following line contains integers f1, f2, . . ., fn — the k-th integer fk is the index of the knight
challenged by knight k (n + 1 ≤ fk ≤ 2n).
The following line contains integers s1, s2 , . . ., sn — the k-th integer sk is the index of the knight
challenged by knight n + k (1 ≤ sk ≤ n).

Output


For each case, output the indices of the knights in the kernel on a single line. If there is more than one
solution, you may output any one.

Sample Input


4
5 6 7 7
1 3 2 3

Sample Output


1 2 4 8


##题意:

有两队骑士各n人,每位骑士会挑战对方队伍的某一个位骑士. (可能相同)
要求找出一个集合S,使得:(任意满足条件即可)
集合S中的骑士不会互相挑战.
每个集合外的骑士必定会被集合S内的某个骑士挑战.


##题解:

一开始看题有点懵比,题目的两层要求绕得有点糊涂.
在模拟样例的过程中发现,有些点是必须在S中的,而有些必须在S外,有些是不固定的.
首先,如果某个骑士没有被人挑战,那么他一定要位于S中. (反之他在集合外的话,就违背了条件2).
然后,如果某个骑士被确定在S中时,那么他的挑战对象一定要在S外. (反之违背条件1).
若某个骑士i被多个人挑战,那么要先对这些挑战者逐一进行上述判断,若某个挑战者被确定在S外,那么说明能使骑士i满足条件2的挑战者少了一个(等同于少了一个挑战者). 若所有挑战者都在S外,那么i一定在S内.

一开始觉得上述条件不够充分,特别是存在多个挑战者时,考虑会不会存在某个挑战者无法确定而导致i确定不了.
考虑无法确定的情况:
首先一定是成对出现,若只出现一个,那么由上述判据一定能够确定它.
比如样例中的15(互相挑战),上述判据就无法确定. 这时候可以推断1和5只要任意一个在集合S内都满足情况.

直接用dfs或bfs搜状态即可,从入度为0的点开始,若某点的父结点被确定在S外,则将该点的入度减少1.


##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define eps 1e-8
#define maxn 201000
#define mod 100000007
#define inf 0x3f3f3f3f
#define mid(a,b) ((a+b)>>1)
#define IN freopen("in.txt","r",stdin);
using namespace std;

int reach[maxn];

int mp[maxn];

bool vis[maxn];

int ans[maxn];

void dfs(int cur) {

vis[cur] = 1;

if(vis[mp[cur]]) return;

if(ans[cur] == 1) { // in;

ans[mp[cur]] = -1;

dfs(mp[cur]);

return;

}

reach[mp[cur]]--; // out;
if(!reach[mp[cur]]) {
ans[mp[cur]] = 1;
dfs(mp[cur]);
}

}

int main(int argc, char const *argv[])

{

//IN;

int n;
while(scanf("%d", &n) != EOF)
{
memset(reach, 0, sizeof(reach));
for(int i=1; i<=2*n; i++) {
int x; scanf("%d", &x);
mp[i] = x;
reach[x]++;
} memset(vis, 0, sizeof(vis));
memset(ans, 0, sizeof(ans));
for(int i=1; i<=2*n; i++) {
if(!vis[i] && !reach[i]) {
ans[i] = 1;
dfs(i);
}
} vector<int> p; p.clear();
for(int i=1; i<=2*n; i++) {
if(ans[i] == -1) continue;
if(ans[i] == 1) p.push_back(i);
else if(i <= n) p.push_back(i);
}
int sz = p.size();
for(int i=0; i<sz; i++) {
printf("%d%c", p[i], i==sz-1?'\n':' ');
}
} return 0;

}

UVALive 7334 Kernel Knights (dfs)的更多相关文章

  1. LeetCode Subsets II (DFS)

    题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...

  2. LeetCode Subsets (DFS)

    题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...

  3. HDU 2553 N皇后问题(dfs)

    N皇后问题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 在 ...

  4. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  5. 【算法导论】图的深度优先搜索遍历(DFS)

    关于图的存储在上一篇文章中已经讲述,在这里不在赘述.下面我们介绍图的深度优先搜索遍历(DFS). 深度优先搜索遍历实在访问了顶点vi后,访问vi的一个邻接点vj:访问vj之后,又访问vj的一个邻接点, ...

  6. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现

    1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...

  7. 深度优先搜索(DFS)和广度优先搜索(BFS)

    深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...

  8. 图的 储存 深度优先(DFS)广度优先(BFS)遍历

    图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...

  9. 搜索——深度优先搜索(DFS)

    设想我们现在身处一个巨大的迷宫中,我们只能自己想办法走出去,下面是一种看上去很盲目但实际上会很有效的方法. 以当前所在位置为起点,沿着一条路向前走,当碰到岔道口时,选择其中一个岔路前进.如果选择的这个 ...

随机推荐

  1. UVa 10562 (特殊的输入处理方式) Undraw the Trees

    题意: 给出一个二维字符数组,它代表了一棵树.然后将这棵树转化为括号表示法(以递归的形式). 分析: 这道题最大的特色就是对数据的处理方式,里面用到了一个 fgets() 函数,这个函数的功能有点像c ...

  2. 建立tracert路由列表的方法

    建立tracert路由列表的方法:电脑屏幕左下方 选择开始选项运行 输入 CMD在DOS命令行下输入:tracert (你的网站域名)   运行结果中如出现了“*     *     *    req ...

  3. 利用ffmpeg解码h264流的代码

    这里也直接给出代码: h264dec.h: #pragma once #include "tdll.h" #include "avcodec.h" #inclu ...

  4. IIS应用程序池回收图文详解

    转:http://blog.sina.com.cn/s/blog_8677fcaa010138uf.html 什么是应用程序池呢?这是微软的一个全新概念:应用程序池是将一个或多个应用程序链接到一个或多 ...

  5. MyBatis学习 之 一、MyBatis简介与配置MyBatis+Spring+MySql

    目录(?)[-] 一MyBatis简介与配置MyBatisSpringMySql MyBatis简介 MyBatisSpringMySql简单配置 搭建Spring环境 建立MySql数据库 搭建My ...

  6. PHP经验集锦

    最近刚刚完成手中的项目,比较闲.来这儿转转,把积累的一些技巧分享给大家!1.关于PHP重定向 方法一:header("Location: index.php"); 方法二:echo ...

  7. 【转】堆栈跟踪中收到一个UnhandledExceptionFilter调用时,如何查找问题异常堆栈

    定义没有异常处理程序来处理引发的异常时调用UnhandledExceptionFilter函数.函数通常将异常传递到捕获并处理它所尝试的 Ntdll.dll 文件. 在某些情况下,在其中存在的进程内存 ...

  8. 在stm32上移植wpa_supplicant(一)

    wifi芯片为88w8686,已经写好了驱动,用的是SPI方式,接下来准备移植wpa_supplicant.参考的资料为一篇论文----<基于微控制器的WPA技术研究与应用>. wpa_s ...

  9. POJ 2136 Vertical Histogram

    题意:按样例那样模拟…… 解法:模拟…… 代码: #include<stdio.h> #include<iostream> #include<algorithm> ...

  10. Kafka 设计与原理详解

    一.Kafka简介 本文综合了我之前写的kafka相关文章,可作为一个全面了解学习kafka的培训学习资料. 转载请注明出处 : 本文链接 1.1 背景历史 当今社会各种应用系统诸如商业.社交.搜索. ...