题目链接:http://codeforces.com/gym/100187/problem/J

题目链接:问通过洗牌器,能否将编号为x的牌子转移到第一个位置?

根据 洗牌器,我们可以知道原本在第i位置的牌可以转移到第j个位置,且这种转移是单向的。如果要把在某个位置的牌转移到第一个位置,那么必须至少存在一条转换路径,其起点为这张牌的位置,起终点为第一个位置。所以就很自然想到建图,然后用dfs跑一遍。

代码如下:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
#include<string>
#include<set>
//#define LOCAL
using namespace std;
#define pb push_back
#define ms(a, b) memset(a,b,sizeof(a));
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int mod = ;
const int maxn = ; int a[];
vector<int>v[];
int vis[]; int dfs(int u)
{
if(u==)
return ; vis[u] = ;
int num = v[u].size();
for(int i = ; i<num; i++)
{
if(!vis[v[u][i]])
{
if(dfs(v[u][i])) return ;
}
}
return ;
} int main()
{
int n,k,x;
scanf("%d",&n);
for(int i = ; i<=n; i++)
{
scanf("%d",&x);//记录编号为x的牌在哪个位置
a[x] = i;
v[i].clear();
} scanf("%d",&k);
for(int i = ; i<k; i++)
for(int j = ; j<=n; j++)
{
scanf("%d",&x);
if(x==j) continue;
v[x].pb(j);//单向图
} scanf("%d",&x);
if(dfs(a[x]))//起点为:编号为x的牌所处的位置
puts("YES");
else
puts("NO"); }

Gym - 100187J J - Deck Shuffling —— dfs的更多相关文章

  1. codeforces Gym 100187J J. Deck Shuffling dfs

    J. Deck Shuffling Time Limit: 2   Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...

  2. Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】

     2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...

  3. 【codeforces.com/gym/100240 J】

    http://codeforces.com/gym/100240 J [分析] 这题我搞了好久才搞出样例的11.76....[期望没学好 然后好不容易弄成分数形式.然后我‘+’没打..[于是爆0... ...

  4. Codeforces GYM 100876 J - Buying roads 题解

    Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...

  5. CF Gym 100187J Deck Shuffling (dfs判连通)

    题意:给你一堆牌,和一些洗牌机,可以改变牌的顺序,问你能不能通过洗牌机把数字为x的牌洗到第一个位置. 题解:反向建边,dfs判断连通性 #include<cstdio> #include& ...

  6. CodeForces Gym 100500A A. Poetry Challenge DFS

    Problem A. Poetry Challenge Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...

  7. codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径

    题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...

  8. codeforces GYM 100114 J. Computer Network tarjan 树的直径 缩点

    J. Computer Network Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Des ...

  9. Gym - 100989 L / M 【dfs / dp】

    题目链接:http://codeforces.com/gym/100989/problem/L / http://codeforces.com/gym/100989/problem/M 题目大意:给定 ...

随机推荐

  1. guake使用

    1. 安装:sudo apt-get install guake 2. 在终端输入guake 3. f12:显示/隐藏 4. f11:全屏/正常屏切换 5. f2:重命名终端名 6. 还可以查看修改快 ...

  2. Usaco_Contest_2013_Open_Bovine Problem 1. Bovine Ballet

    Problem 1: Bovine Ballet [Brian Dean, 2013] In an attempt to challenge the stereotypical perception ...

  3. jQuery全选全不选

    $("[name='goods']").prop("checked", this.checked);

  4. 第四期coding_group笔记_用CRF实现分词-词性标注

    一.背景知识 1.1 什么是分词? NLP的基础任务分为三个部分,词法分析.句法分析和语义分析,其中词法分析中有一种方法叫Tokenization,对汉字以字为单位进行处理叫做分词. Example ...

  5. 第四章——SQLServer2008-2012资源及性能监控(1)专家

    http://blog.csdn.net/dba_huangzj/article/details/8614817

  6. 手机微硬盘读取速度>50MB/s eMMC技术浅析

    转载:http://mobile.zol.com.cn/296/2968659_all.html#p2968659 手机微硬盘读取速度>50MB/s 在开始今天的话题之前,请大家随笔者一起时光倒 ...

  7. 集合对象(NSSet,NSMutableSet,NSIndexSet)

    NSArray:有序的集合,NSSet:无序的集合,散列存储. 但是NSSet保证数据的唯一性.当插入相同的数据时,不会有任何效果.从内部实现来说是hash表.NSMutableSet是NSSet的子 ...

  8. 借助树莓派模拟Wimonitor并实现WiFi窃听和嗅探

    Wimonitor是一款非常优秀的黑客工具,它不仅可以帮渗透测试人员省去配置虚拟机和无线网卡等一系列麻烦事,而且它的Web接口配置起来也非常的方便.实际上,它就是一款TP-Link-MR3020路由器 ...

  9. [Algorithms] Sort an Array with a Nested for Loop using Insertion Sort in JavaScript

    nsertion sort is another sorting algorithm that closely resembles how we might sort items in the phy ...

  10. jsp网页在浏览器中不显示图片_eclipse环境下配置tomcat中jsp项目的虚拟路径

    遇到的问题是这种,在jsp网页中嵌入了本地的图片,由于会用到上传到服务器的图片,所以没有放到项目里面,而是把全部图片单独放到一个文件夹里,然后打算使用绝对路径把要显示的图片显示出来.比方是放在了E盘的 ...