博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CF Gym 100187J Deck Shuffling (dfs判连通)
阅读量:7055 次
发布时间:2019-06-28

本文共 933 字,大约阅读时间需要 3 分钟。

题意:给你一堆牌,和一些洗牌机,可以改变牌的顺序,问你能不能通过洗牌机把数字为x的牌洗到第一个位置。

题解:反向建边,dfs判断连通性

#include
#include
using namespace std;const int maxn = 200000+4;int a[maxn];vector
son[maxn];int x;bool vis[maxn];bool dfs(int u){ if(a[u] == x) return true; for(int i = 0; i < son[u].size(); i++){ int v = son[u][i]; if(!vis[v]){ vis[v] = 1; if(dfs(v)) return true; } } return false;}int main(){ //freopen("in.txt","r",stdin); int n; scanf("%d",&n); for(int i = 0; i < n; i++){ scanf("%d",a+i); } int k; scanf("%d",&k); for(int i = 0; i < k; i++){ for(int j = 0; j < n; j++){ int t; scanf("%d",&t); if(t-1!=j) son[j].push_back(t-1); } } scanf("%d",&x); printf("%s",dfs(0)?"YES":"NO"); return 0;}

 

转载于:https://www.cnblogs.com/jerryRey/p/4658208.html

你可能感兴趣的文章
命名空间“Microsoft”中不存在类型或命名空间名“Reporting”(是否缺少程序集引用?)...
查看>>
【转】Scheme 编程环境的设置
查看>>
异常分类,异常抛出位置
查看>>
需求分析与原型设计
查看>>
敌兵布阵 HDU - 1166 (树状数组模板题,线段树模板题)
查看>>
代码赏析——史丰收速算
查看>>
oracle恢复误删除表
查看>>
Navicat for MySQL常见命令
查看>>
Threading and Tasks in Chrome
查看>>
七、Maven依赖管理
查看>>
Android 学习
查看>>
工厂模式
查看>>
spring中的web上下文,spring上下文,springmvc上下文区别(超详细)(转载)
查看>>
RxSwift 对 MJRefresh 使用的封装
查看>>
leetcode 118 Pascal's Triangle
查看>>
聚美第八天
查看>>
Java基础-使用Idea进行远程调试
查看>>
Jenkins发送邮件
查看>>
Python入门篇-递归函数Recursion
查看>>
百度/Google/网页字符
查看>>