codeforces Gym 100187J J. Deck Shuffling dfs
2015-07-18 21:27
344 查看
J. Deck Shuffling
Time Limit: 20 SecMemory Limit: 256 MB
题目连接
http://codeforces.com/gym/100187/problem/JDescription
The world famous scientist Innokentiy continues his innovative experiments with decks of cards. Now he has a deck of n cards and k shuffle machines to shuffle this deck. As we know, i-th shuffle machine is characterized by its own numbers pi, 1, pi, 2, ..., pi, n such that if one puts n cards numbered in the order 1, 2, ..., n into the machine and presses the button on it, cards will be shuffled forming the deck pi, 1, pi, 2, ..., pi, n where numbers pi, 1, pi, 2, ..., pi, n are the same numbers of cards but rearranged in some order.At the beginning of the experiment the cards in the deck are ordered as a1, a2, ..., an, i.e. the first position is occupied by the card with number a1, the second position — by the card with number a2, and so on. The scientist wants to transfer the card with number x to the first position. He can use all his shuffle machines as many times as he wants. You should determine if he can reach it.
[b]Input[/b]
In the first line the only positive integer n is written — the number of cards in the Innokentiy's deck.
The second line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ n) — the initial order of cards in the deck.
The third line contains the only positive integer k — the number of shuffle machines Innokentiy has.
Each of the next k lines contains n distinct integers pi, 1, pi, 2, ..., pi, n (1 ≤ pi, j ≤ n) characterizing the corresponding shuffle machine.
The last line contains the only integer x (1 ≤ x ≤ n) — the number of card Innokentiy wants to transfer to the first position in the deck.
Numbers n and k satisfy the condition 1 ≤ n·k ≤ 200000.
[b]Output[/b]
Output «YES» if the scientist can transfer the card with number x to the first position in the deck, and «NO» otherwise.
[b]Sample Input[/b]
4
4 3 2 1
2
1 2 4 3
2 3 1 4
1
[b]Sample Output[/b]
YES
HINT
[b]题意[/b]
给你一堆牌,给你k个洗牌机,给你洗牌机洗完之后的顺序,然后问你是否洗完牌之后,第一张牌为x
[b]题解:[/b]
建边,跑一发dfs就好了,其实就问你第一个位置和x所在的位置是否联通
[b]代码[/b]
#include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <fstream> #include <map> #include <stack> typedef long long ll; using namespace std; //freopen("D.in","r",stdin); //freopen("D.out","w",stdout); #define sspeed ios_base::sync_with_stdio(0);cin.tie(0) #define test freopen("test.txt","r",stdin) #define maxn 200101 #define mod 1000000009 #define eps 1e-9 const int inf=0x3f3f3f3f; const ll infll = 0x3f3f3f3f3f3f3f3fLL; inline ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } //************************************************************************************** int a[maxn]; vector<int> e[maxn]; map<pair<int,int> ,int> Q; int flag=0; int vis[maxn]; void dfs(int x) { if(x==1) flag=1; if(flag) return; if(vis[x]) return; vis[x]=1; for(int i=0;i<e[x].size();i++) { if(vis[e[x][i]]) continue; dfs(e[x][i]); } } int main() { int n=read(); for(int i=1;i<=n;i++) { int x=read(); a[x]=i; } int k=read(); for(int i=1;i<=k;i++) { for(int j=1;j<=n;j++) { int x=read(); if(Q[make_pair(j,x)]==0) { e[j].push_back(x); Q[make_pair(j,x)]=1; } } } int x=read(); dfs(a[x]); if(flag) puts("YES"); else puts("NO"); }
相关文章推荐
- jquery动画总结
- ASP.net后台弹出消息对话框的方法!【转】
- 生产者消费者模式(1)
- 【Spark Core】TaskScheduler源码与任务提交原理浅析2
- 10行Java代码实现最近被使用(LRU)缓存
- OpenGL_ES 创建一个平面 绘制纹理 响应输入事件
- POJ1088滑雪(入门级DP)
- bzoj1257[CQOI]余数之和
- ZOJ 2104 Let the Balloon Rise
- poj3984-迷宫问题(广搜)
- Android学习笔记之dispatchTouchEvent和OnInterceptTouchEvent和OnTouchEvent三个方法之间的联系...
- Eddy's research I
- 【Spark Core】TaskScheduler源码与任务提交原理浅析1
- 每个程序员都需要了解的一个SQL技巧
- SWIFT中获取配置文件路径的方法
- 迭代器模式
- git使用的基础及常用命令
- ACdream 1727 Sequence
- 添加view类图中的二级菜单
- IntelliJ公司版破解方法