Gym
来源:互联网 发布:梦幻之星新星捏脸数据 编辑:程序博客网 时间:2024/06/10 11:44
题目链接: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 LOCALusing 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 = 1000000007;const int maxn = 10;int a[200010];vector<int>v[200010];int vis[200010];int dfs(int u){ if(u==1) return 1; vis[u] = 1; int num = v[u].size(); for(int i = 0; i<num; i++) { if(!vis[v[u][i]]) { if(dfs(v[u][i])) return 1; } } return 0;}int main(){ int n,k,x; scanf("%d",&n); for(int i = 1; i<=n; i++) { scanf("%d",&x);//记录编号为x的牌在哪个位置 a[x] = i; v[i].clear(); } scanf("%d",&k); for(int i = 0; i<k; i++) for(int j = 1; 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");}
0 0
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- linux yum命令详解
- 题目1141:Financial Management
- "_OBJC_CLASS_$_xxx", referenced from: symbol(s) not found for architecture x86_64
- centos7 -- 安装jdk1.8
- C语言基础:运算符、表达式和语句
- Gym
- 【JUnit】JUnit软件测试技术(工具)
- C++ 文件读取
- 动态规划―移动路线
- 数据结构 绪论 day1
- hadoop 命令总结
- 什么是LTE(Long Term Evolution)
- Java 发送邮件
- 工作经验