Gym - 100187J J - Deck Shuffling —— dfs
题目链接: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的更多相关文章
- codeforces Gym 100187J J. Deck Shuffling dfs
J. Deck Shuffling Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...
- 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 ...
- 【codeforces.com/gym/100240 J】
http://codeforces.com/gym/100240 J [分析] 这题我搞了好久才搞出样例的11.76....[期望没学好 然后好不容易弄成分数形式.然后我‘+’没打..[于是爆0... ...
- Codeforces GYM 100876 J - Buying roads 题解
Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...
- CF Gym 100187J Deck Shuffling (dfs判连通)
题意:给你一堆牌,和一些洗牌机,可以改变牌的顺序,问你能不能通过洗牌机把数字为x的牌洗到第一个位置. 题解:反向建边,dfs判断连通性 #include<cstdio> #include& ...
- CodeForces Gym 100500A A. Poetry Challenge DFS
Problem A. Poetry Challenge Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...
- codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径
题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...
- codeforces GYM 100114 J. Computer Network tarjan 树的直径 缩点
J. Computer Network Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Des ...
- Gym - 100989 L / M 【dfs / dp】
题目链接:http://codeforces.com/gym/100989/problem/L / http://codeforces.com/gym/100989/problem/M 题目大意:给定 ...
随机推荐
- 【APIO2015】Jakarta Skyscrapers
题目描述 印尼首都雅加达市有 $N$ 座摩天楼,它们排列成一条直线,我们从左到右依次将它们编号为 $0$ 到 $N − 1$.除了这 $N$ 座摩天楼外,雅加达市没有其他摩天楼. 有 $M$ 只叫做 ...
- windows XP 下的DTRACE 跟踪 学习
https://github.com/prash-wghats/DTrace-win32 1. dtrace_loader.exe -l //to load dtrace drivers 2. C:\ ...
- Java使用QRCode.jar生成与解析二维码
原文V:http://www.cnblogs.com/bigroc/p/7496995.html#3797682 正题:Java使用QRCode.jar生成与解析二维码demo 欢迎新手共勉,大神监督 ...
- Could not change executable permissions on the application
I could solve it erasing an application that I had previously uploaded using the same Bundle Identif ...
- 邁向IT專家成功之路的三十則鐵律 鐵律三:IT人長久之道–站對邊
這一回來談談IT人對於技術的學習.對於一位專業的IT人來說,在自己有興趣的技術領域之中,究竟要如何來正確選擇學習的方向呢?關於這個問題的答案,筆者個人深深體會到這確實會成為一位專業IT人士的長久經營之 ...
- 【Linxu】CentOS7下安装程序报错:
进入root用户,然后编辑 vi /usr/libexec/urlgrabber-ext-down 将首行换成 #!/usr/bin/python2.
- python设置utf-8为默认编码
当使用Python编程时,编码问题一直很让人头疼,程序中经常会碰到如下错误提示: UnicodeDecodeError: 'ascii' codec can't decode byte 0x?? in ...
- C#中通过反射获取类中非公有成员
public class NGlbGlobeXComm { public static T GetPrivateField<T>(object instance, string field ...
- Java中字符串转为16进制表示
Java中字符串转为16进制表示 String str = "鲸"; char[] chars = "0123456789ABCDEF".toCharArray ...
- vue2.0 + vux 项目搭建
1.快速搭建项目模板 因为项目使用vux,所以推荐使用vux官网的airyland/vux2 模板,vue-cli工具是vue项目的搭建脚手架 默认为 webpack2 模板,如果你需要使用webpa ...