标签:
Time Limit: 20 Sec
Memory Limit: 256 MB
http://codeforces.com/gym/100187/problem/J
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.
Input
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.
Output
Output «YES» if the scientist can transfer the card with number x to the first position in the deck, and «NO» otherwise.
Sample Input
4
4 3 2 1
2
1 2 4 3
2 3 1 4
1
Sample Output
YES
40‘
题意
给你一堆牌,给你k个洗牌机,给你洗牌机洗完之后的顺序,然后问你是否洗完牌之后,第一张牌为x
题解:
图论建边,再进行dfs,问你是否起点和终点联通。至于成环什么的用vis防止。
//debug:第一次在test22超时,原因并不是因为规模过大到达20w,而是因为1.一开始的节点没vis=1 2.一个子dfs结束后不用vis复原,因为状态是一样的,不必要再重新从别的父亲节点到该子节点 3.题意没理解清楚?(其实第二个样例还是没怎么清楚)
代码:
1 #include <cstdio> 2 #include <cmath> 3 #include <cstring> 4 #include <ctime> 5 #include <iostream> 6 #include <algorithm> 7 #include <set> 8 #include <vector> 9 #include <sstream> 10 #include <queue> 11 #include <typeinfo> 12 #include <fstream> 13 #include <map> 14 #include <stack> 15 typedef long long ll; 16 using namespace std; 17 #define test freopen("1.txt","r",stdin) 18 #define maxn 2000001 19 #define mod 10007 20 #define eps 1e-9 21 const int inf=0x3f3f3f3f; 22 const ll infll = 0x3f3f3f3f3f3f3f3fLL; 23 inline int read() 24 { 25 ll x=0,f=1;char ch=getchar(); 26 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} 27 while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} 28 return x*f; 29 } 30 inline void out(int x) { 31 if(x>9) out(x/10); 32 putchar(x%10+‘0‘); 33 } 34 //************************************************************************************** 35 int a[200010]; 36 vector<int>e[200010]; 37 int n,k; 38 void init() 39 { 40 n=read(); 41 for(int i=1;i<=n;i++) 42 a[read()]=i; 43 k=read(); 44 for(int i=1;i<=k;i++) 45 { 46 for(int j=1;j<=n;j++) 47 { 48 int v=read(); 49 if(j!=v) e[j].push_back(v); 50 } 51 } 52 } 53 bool vis[200000+100]; 54 int dfs(int u) 55 { 56 if(u==1) return 1; 57 for(int i=0;i<e[u].size();i++) 58 { 59 int v=e[u][i]; 60 if(!vis[v]){ 61 vis[v]=1; 62 if(dfs(v)) 63 return 1; 64 } 65 } 66 return 0; 67 } 68 void solve() 69 { 70 memset(vis,0,sizeof(vis)); 71 int u=read(); 72 vis[a[u]]=1; 73 if(dfs(a[u])) 74 printf("YES\n"); 75 else printf("NO\n"); 76 } 77 int main() 78 { 79 init(); 80 solve(); 81 return 0; 82 }
codeforces Gym 100187J J. Deck Shuffling dfs
标签:
原文地址:http://www.cnblogs.com/diang/p/4747347.html