[BZOJ 4436][Cerc2015]Kernel Knights
[Cerc2015]Kernel Knights
Time Limit: 2 Sec Memory Limit: 512 MB
Submit: 5 Solved: 4
[Submit][Status][Discuss]
Description
“Jousting”是一种让骑士在高速骑行中用木制长矛相互攻击对方的中世纪竞技游戏。现在,一共有2n个骑士进入一场“Jousting”锦标赛。骑士们被平均分配到2个house。竞赛开始时,所有骑士都会对另一个house的骑士之一发起挑战。
一组解被定义为一个集合S满足:
• S中的骑士不存在相互挑战。
• 所有不在S中的骑士都被S中的骑士挑战。
现给出官方公布的挑战场次,找出一组解。
数据保证有解。
Input
第一行包括一个整数n(1<=n<=100000)——每个house的骑士数。第一个house的骑士编号为1~n,第二个house的骑士编号为n+1~2n。
接下来一行包含n个整数f1,f2,…,fn——fk指第k名骑士发起的挑战。
最后一行包含n个整数s1,s2,…,sn——sk指第n+k名骑士发起的挑战。
(1<=fk,sk<=n)
Output
在一行中输出S中骑士的编号。
如果数据存在多组解,你只需任意输出一组。
Sample Input
4
5 6 7 7
1 3 2 3
Sample Output
1 2 4 8
HINT 请不要提交,期待SPJ
因为原图是二分图,所以所有的环都是偶环
因为每个点只有一个出度,所以所有的环都是简单环。
那么这个图可以想象成一个内向基环森林
每次给度为0的节点染色。每遍历到一个点度--
如果将要到达的点度为0或者它的颜色为不可选(因为此点不可选所以不能随便染色),那么继续遍历(博主语死早。。大家自行画图吧。。)
然后把所有没有标记的环搞一遍就可以了
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#define maxn 500010
using namespace std;
int n;
int to[maxn], h[maxn], cnt, deg[maxn], col[maxn];
void add(int u, int v){to[u] = v, deg[v] ++;} void BFS(int u){
col[u] = 2;
while(true){
if(col[to[u]])break;
deg[to[u]] --;
if(deg[to[u]] == 0 || col[u] == 2){
col[to[u]] = col[u] ^ 1;
u = to[u];
}
else break;
}
} void Col(int u){
col[u] = 2;
while(true){
if(col[to[u]])break;
col[to[u]] = col[u] ^ 1;
u = to[u];
}
} int main(){
scanf("%d", &n);
int v;
for(int i = 1; i <= n; i ++)
scanf("%d", &v), add(i, v); for(int i = n + 1; i <= n * 2; i ++)
scanf("%d", &v), add(i, v);
n <<= 1;
for(int i = 1; i <= n; i ++)
if(!deg[i] && !col[i])BFS(i);
for(int i = 1; i <= n; i ++)
if(!col[i])Col(i);
for(int i = 1; i <= n; i ++)
if(col[i] == 2)printf("%d ", i);
return 0;
}
[BZOJ 4436][Cerc2015]Kernel Knights的更多相关文章
- UVALive 7334 Kernel Knights (dfs)
Kernel Knights 题目链接: http://acm.hust.edu.cn/vjudge/contest/127407#problem/K Description Jousting is ...
- 【UVALive 7334】Kernel Knights
题 题意 有两个队的骑士1到n和n+1到2n,每个骑士只能互相攻击对手队的一个骑士.kernel的意思是在这个kernel里的骑士不会互相攻击,在kernel外的骑士被kernel里的骑士攻击. 现在 ...
- BZOJ 4421: [Cerc2015] Digit Division
4421: [Cerc2015] Digit Division Time Limit: 1 Sec Memory Limit: 512 MBSubmit: 348 Solved: 202[Subm ...
- bzoj 4451 : [Cerc2015]Frightful Formula FFT
4451: [Cerc2015]Frightful Formula Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 177 Solved: 57[Sub ...
- BZOJ 4421: [Cerc2015] Digit Division 排列组合
4421: [Cerc2015] Digit Division 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4421 Descripti ...
- BZOJ 1671: [Usaco2005 Dec]Knights of Ni 骑士 (bfs)
题目: https://www.lydsy.com/JudgeOnline/problem.php?id=1671 题解: 按题意分别从贝茜和骑士bfs然后meet_in_middle.. 把一个逗号 ...
- BZOJ 4435 [Cerc2015]Juice Junctions 分治最小割+hash
分治最小割的题目,要求n2. 之前用的n3的方法自然不能用了. 于是用hash,设hash[i][j]表示在最小割为i的时候,j是否与S联通. 看懂这个需要理解一下最小割树的构造. 这种题建议用EK写 ...
- bzoj 1671: [Usaco2005 Dec]Knights of Ni 骑士【bfs】
bfs预处理出每个点s和t的距离d1和d2(无法到达标为inf),然后在若干灌木丛格子(x,y)里取min(d1[x][y]+d2[x][y]) /* 0:贝茜可以通过的空地 1:由于各种原因而不可通 ...
- Kernel Knights (Gym - 101480K)
题目链接 #include <bits/stdc++.h> using namespace std; typedef long long ll; int a[200005]; //存放原始 ...
随机推荐
- Linux jstack命令详解
jstack用于打印出给定的java进程ID或core file或远程调试服务的Java堆栈信息. 如果是在64位机器上,需要指定选项"-J-d64",Windows的jstack ...
- 使用twisted.web实现代理服务器
简单的实现谷歌的代理: 架构就是下面这么简单. ================= my server outside GFW | <----------------------> ...
- android获取手机信息大全
IMEI号,IESI号,手机型号: private void getInfo() { TelephonyManager mTm = (TelephonyManager) getSystemServic ...
- ReverseString
[本文链接] http://www.cnblogs.com/hellogiser/p/reverse-string.html reverse string [代码] C++ Code 123456 ...
- VC++遇到的错误汇集
1.在程序头添加#include "stdafx.h" 和#include <afx.h>时会出现以下错误 在Win32Project下使用,出现“error C118 ...
- Java动态加载类在功能模块开发中的作用
Java中我们一般会使用new关键字实例化对象然后调用该对象所属类提供的方法来实现相应的功能,比如我们现在有个主类叫Web类这个类中能实现各种方法,比如用户注册.发送邮件等功能,代码如下: /* * ...
- Java for LeetCode 030 Substring with Concatenation of All Words【HARD】
You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...
- codeforces 478B Random Teams 解题报告
题目链接:http://codeforces.com/problemset/problem/478/B 题目意思:有 n 个人,需要将这班人分成 m 个 组,每个组至少含有一个人,同一个组里的人两两可 ...
- [Linux] 孤儿进程与僵尸进程[总结]
转载: http://www.cnblogs.com/Anker/p/3271773.html 1.前言 之前在看<unix环境高级编程>第八章进程时候,提到孤儿进程和僵尸进程,一直对这两 ...
- dfs常见的配置文件中的value与description
照抄于网络: name value description dfs.namenode.logging.level info The logging level for dfs namenode. Ot ...