Gym - 101480K_K - Kernel Knights (DFS)

题意:有两队骑士各n人,每位骑士会挑战对方队伍的某一个位骑士. (可能相同)
要求找以一个区间s:
集合S中的骑士不会互相挑战.
每个集合外的骑士必定会被集合S内的某个骑士挑战.
题解:讲真被题目绕懵比了,一直不知道题目在要求找啥。
骑士可以分为三类:必定在s中,必定不再s中,不确定的。
如果一个骑士的被挑战人数为0的话,那么他一定在s中。(否则就违背了2)
如果一个骑士挑战了确定在s内的骑士,那么他一定在圈外。
若某个骑士i被多个人挑战,那么要先对这些挑战者逐一进行上述判断,若某个挑战者被确定在S外,那么说明能使骑士i满足条件2的挑战者少了一个(等同于少了一个挑战者). 若所有挑战者都在S外,那么i一定在S内。
最后会有一种情况,就是该骑士跟挑战者都不能确定在圈外还是圈内,如样例的1、5,这时候让他们任意一人在圈内就可以了。
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 200050;
int ans[maxn],f[maxn],head[maxn],vis[maxn];
void dfs(int x)
{
vis[x] = 1;
if(vis[head[x]]==1)
return;
if(f[x]==1)
{
f[head[x]] = -1;
dfs(head[x]);
return;
}
ans[head[x]] --;
if(!ans[head[x]])
{
f[head[x]] = 1;
dfs(head[x]);
return;
}
}
int main()
{
int n,i,x,ff;
cin>>n;
memset(ans,0,sizeof(ans));
memset(f,0,sizeof(ans));
memset(head,0,sizeof(ans));
memset(vis,0,sizeof(ans));
for(i=1;i<=2*n;i++)
{
scanf("%d",&x);
ans[x] ++;
head[i] = x;
}
for(i=1;i<=2*n;i++)
if(!ans[i]&&!vis[i])
{
f[i] = 1;
dfs(i);
}
ff = 0;
for(i=1;i<=2*n;i++)
{
if(f[i]==-1)
continue;
if(f[i]==1)
{
if(!ff)
{
printf("%d",i);
ff = 1;
}
else
printf(" %d",i);
continue;
}
if(i<=n)
{
if(!ff)
{
printf("%d",i);
ff = 1;
}
else
printf(" %d",i);
}
}
printf("\n");
return 0;
}
Gym - 101480K_K - Kernel Knights (DFS)的更多相关文章
- UVALive 7334 Kernel Knights (dfs)
Kernel Knights 题目链接: http://acm.hust.edu.cn/vjudge/contest/127407#problem/K Description Jousting is ...
- [BZOJ 4436][Cerc2015]Kernel Knights
[Cerc2015]Kernel Knights Time Limit: 2 Sec Memory Limit: 512 MBSubmit: 5 Solved: 4[Submit][Status][D ...
- Kernel Knights (Gym - 101480K)
题目链接 #include <bits/stdc++.h> using namespace std; typedef long long ll; int a[200005]; //存放原始 ...
- ACM: Gym 100935G Board Game - DFS暴力搜索
Board Game Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Gym 100 ...
- 【UVALive 7334】Kernel Knights
题 题意 有两个队的骑士1到n和n+1到2n,每个骑士只能互相攻击对手队的一个骑士.kernel的意思是在这个kernel里的骑士不会互相攻击,在kernel外的骑士被kernel里的骑士攻击. 现在 ...
- K. Random Numbers(Gym 101466K + 线段树 + dfs序 + 快速幂 + 唯一分解)
题目链接:http://codeforces.com/gym/101466/problem/K 题目: 题意: 给你一棵有n个节点的树,根节点始终为0,有两种操作: 1.RAND:查询以u为根节点的子 ...
- POJ2735/Gym 100650E Reliable Nets dfs
Problem E: Reliable NetsYou’re in charge of designing a campus network between buildings and are ver ...
- Gym 100650H Two Ends DFS+记忆化搜索
Problem H: Two EndsIn the two-player game “Two Ends”, an even number of cards is laid out in a row. ...
- Codeforces Gym 100650D Queens, Knights and Pawns 暴力
Problem D: Queens, Knights and PawnsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu ...
随机推荐
- textarea限定字符输入及提示
html <textarea type="text" name="goodsDesc" data-varify='goods' placeholder=& ...
- JPA实体
Java类可以很容易地转换成实体. 对于实体转换,基本要求是 - 无参数构造函数 注解 @Entity和@Id注解. @Entity - 这是一个标记注释,表明这个类是一个实体.这个注释必须放在类名称 ...
- Linux学习(一):软链接和硬链接
今天起,决定开始自学Linux命令及Shell脚本,并用Linux学习(命令行,Shell及其他知识点)这一系列记录下自己的心路历程,内容不分先后,只记录自己觉得有必要的,简单的就不记了! 第一个知识 ...
- https方式nginx 代理tomcat访问不带www的域名301重定向跳转到www的域名帮助seo集中权重
比如我要把 http://gucanhui.com http://www.gucanhui.com 跳转到https://www.gucanhui.com 用F12的network可以看到状态码301 ...
- 前端--HTML简介
软件开发架构: c/s架构 客户端 服务端 b/s架构 浏览器 服务端 本质:b/s架构也是c/s架构 HTTP协议 超文本传输协议:规定了客户端与服务端之间消息传输的格式 四个特性: 1.基于TCP ...
- python基础--魔法方法、迭代器、上下文管理
isinstance:判断一个对象是否是某个类的实例 参数一:要判断的对象 参数二:要判断的类型 issubclass:判断一个类是否是另一个类的子类 参数一:是待判断的子类 参数二:待判断的父类 _ ...
- JQMObile 优势
1.跨平台 目前大部分的移动设备浏览器都支持HTML5标准,jQuery Mobile以HTML5标记配置网页,所以可以跨不同的移动设备,如Apple iOS,Android,BlackBerry, ...
- Leetcode77. Combinations组合
给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合. 示例: 输入: n = 4, k = 2 输出: [ [2,4], [3,4], [2,3], [1,2], [1,3] ...
- 直白介绍卷积神经网络(CNN)【转】
英文地址:https://ujjwalkarn.me/2016/08/11/intuitive-explanation-convnets/ 中文译文:http://mp.weixin.qq.com/s ...
- Ajax--art-template + 调用天气接口
一.实现原理: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...