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 ...
随机推荐
- 洛谷 2890 [USACO07OPEN]便宜的回文Cheapest Palindrome
传送门 一道最简单的区间dp,然而我还是抄了题解. //Twenty #include<algorithm> #include<iostream> #include<cs ...
- myql 配置项
提高数据插入速度方法 bulk_insert_buffer_size 默认:8M (8*1024*1024) 参考网址:https://stackoverflow.com/questions/2030 ...
- jodatime 计算时间差_统计程序运行耗时
https://blog.csdn.net/De_Moivre/article/details/79775661 记录开始执行的时间 DateTime startDateTime=new DateTi ...
- vmstat-虚拟内存查看实例
虚拟内存运行原理 在系统中运行的每个进程都需要使用到内存,但不是每个进程都需要每时每刻使用系统分配的内存空间.当系统运行所需内存超过实际的物理内存,内核会释放某些进程所占用但未使用的部分或所有物理内存 ...
- 洛谷P1084 [NOIP2012提高组Day2T3]疫情控制
P1084 疫情控制 题目描述 H 国有 n 个城市,这 n 个城市用 n-1 条双向道路相互连通构成一棵树,1 号城市是首都,也是树中的根节点. H 国的首都爆发了一种危害性极高的传染病.当局为了控 ...
- Linux系统下实现远程连接MySQL数据库的方法教程
1.在服务器端开启远程访问首先进入mysql数据库,然后输入下面两个命令: grant all privileges on *.* to 'root'@'%' identified by 'passw ...
- Liferay 7:如何在Liferay Portlet中使用Angular, React, Vue.js等前端框架
https://web.liferay.com/zh/web/ivan.zaera/blog/-/blogs/modern-frontend-workflows-in-liferay-portal L ...
- 如何制作可以在 MaxCompute 上使用的 crcmod
之前我们介绍过在 PyODPS DataFrame 中使用三方包.对于二进制包而言,MaxCompute 要求使用包名包含 cp27-cp27m 的 Wheel 包.但对于部分长时间未更新的包,例如 ...
- agc015F Kenus the Ancient Greek
题意: 有$Q$次询问,每次给定$X_i$和$Y_i$,求对于$1\leq x \leq X_i , 1 \leq y \leq Y_i$,$(x,y)$进行辗转相除法的步数的最大值以及取到最大值的方 ...
- 批处理启动应用程序(win)
@echo off net session >nul 2>&1 " ( echo Oops: This tools must run with administrator ...