九野的博客,转载请注明出处 http://blog.csdn.net/acmmmm/article/details/11104265

题目链接 :http://222.197.181.5/problem.php?pid=1811

题意:T个测试数据

n m //n个点 m条边

m条无向边

que//下面有que个数据

a b // 表示a点的钥匙在b中

问,从0点开始能否遍历所有的点

思路:用BFS搜一遍即可,注意图是否连通,用并查集判断一下

BFS()时,q为正常队列,p为走到那个点是锁住时将q中点移到p中

#include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <stdlib.h>
#include <cstdlib>
#include <math.h>
#include <cstring>
#include <set>
#include <vector>
#define inf 1073741824
#define N 100100
#define ll int
using namespace std;
inline ll Max(ll a,ll b){return a>b?a:b;}int lock[N],key[N],n,m;//lock=0表示没锁 ,key[i] 表示i房间中的钥匙,没有钥匙=-1
vector<int>G[N];
queue<int>q,p;//q表示bfs的没锁的点,p表示被锁的点int f[N];
int find(int x){if(f[x]==x)return x;return f[x]=find(f[x]);
}bool vis[N],inp[N];void BFS(){memset(vis,0,sizeof(vis));memset(inp,0,sizeof(inp));q.push(0);vis[0]=true;int i,v,u,len;bool change=true;while(1){change=false;//跳出条件是有新的点可以走while(!q.empty()){u=q.front(); q.pop();len=G[u].size();for(i=0;i<len;i++){v=G[u][i];if(lock[v]==-1){if(inp[v]==false)p.push(v),inp[v]=true;//如果锁着且不在p中continue;}if(vis[v]==false){vis[v]=true;q.push(v);change=true;if(key[v]!=-1)//说明有钥匙lock[key[v]]=0;                 }}}if(change==false)break;if(!p.empty()){len=p.size();while(len--){int u=p.front();p.pop();if(lock[u]>=0) //u点没有锁q.push(u),vis[u]=true;else p.push(u);             }}}
}int main(){int i,j,a,b,que;int T,Cas=1;scanf("%d",&T);while(T--){memset(lock,0,sizeof(lock));memset(key,-1,sizeof(key));scanf("%d%d",&n,&m);for(i=0;i<n;i++)G[i].clear(),f[i]=i;while(m--){scanf("%d%d",&a,&b);G[a].push_back(b);G[b].push_back(a);f[find(a)]=find(b);}scanf("%d",&que);while(que--){scanf("%d%d",&b,&a);key[a]=b;lock[b]=-1;}for(i=0;i<n;i++)find(i);bool fu=false;for(i=0;i<n;i++)if(f[i]!=f[0]){fu=true;break;}if(fu){printf("Case #%d: No\n",Cas++);continue;}while(!q.empty())q.pop();while(!p.empty())p.pop();if(key[0]!=-1)lock[key[0]]=0;//起点房间如果有钥匙直接开门BFS();if(!p.empty())printf("Case #%d: No\n",Cas++);//p表示锁着的门,如果还有锁着的门就说明没有走到所有的点else printf("Case #%d: Yes\n",Cas++);}return 0;
}
/*
99
2 1
0 1
0
3 2
0 1
1 2
1
1 27 9
0 1
2 0
2 5
5 1
5 6
5 2
1 3
3 6
1 4
3
4 2
2 0
6 47 9
0 1
2 0
2 5
5 1
5 6
5 2
1 3
3 6
1 4
1
2 23 4
0 1
1 0
0 1
2 0
2
1 2
2 11 0
02 1
1 0
1
1 12 1
1 0
1
1 02 0
0ans:
y
n
y
n
n
y
n
y*/

转载于:https://www.cnblogs.com/suncoolcat/p/3303897.html

UESTC 1811 Hero Saving Princess相关推荐

  1. ACM比赛经验、刷题记录及模板库总结(更新中)

    前言 本文所提及的部分题目代码,可以在我的Github上找到 第一部分 经验分享及感受 第二部分 刷题记录 一.基础算法&程序语言 //strlen()函数的复杂度是O(n)要小心 //截取字 ...

  2. 多校1(1009)(杭电4308)

    题目: Saving Princess claire_ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  3. 哈理工OJ 1192 追逐(贪心算法)

    追逐 Time Limit: 1000 MS Memory Limit: 65536 K Total Submit: 140(68 users) Total Accepted: 75(64 users ...

  4. hdu1027 Ignatius and the Princess II (全排列 amp; STL中的神器)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=1027 Ignatiu ...

  5. hdu-1029 Ignatius and the Princess IV

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1029 题目类型: 给一个奇数个列的数组,其中一定存在某个数字,该数字的个数是大于一半的,问这个数字是 ...

  6. E - Ignatius and the Princess IV

    ####E - Ignatius and the Princess IV "OK, you are not too bad, em- But you can never pass the n ...

  7. 杭电1027Ignatius and the Princess II模拟

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=1027 题目: Problem Description Now our hero finds the doo ...

  8. 【HDU - 1026 】Ignatius and the Princess I (bfs + 记录路径)

    题干: The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has to rescue our pr ...

  9. 【宽搜】mzoj 1345 英雄 hero

    题面 题目描述(Description): 城堡迷宫由N×M个格子组成,英雄Mario玛丽奥要在城堡迷宫中从起始点移动到目标点去拯救被怪物掳去的公主,他每一步只能从当前所在的格子移动到相邻的4个格子之 ...

最新文章

  1. ZIP,一个没落天才的故事
  2. IntelliSense: const char * 类型的实参与 LPCTSTR 类型的形参不兼容
  3. java虚拟机监控_Java虚拟机监控工具
  4. java excutorthread_Java中ThreadPoolExecutor的参数理解
  5. Spring MVC和JQuery用于Ajax表单验证
  6. Hadoop HDFS概念学习系列之HDFS升级和回滚机制(十二)
  7. python数据库操作实例
  8. php 迭代设计模式 -- BookList -- 分页
  9. 美国计算机科学奥林匹克竞赛试题,中学信息奥林匹克竞赛试题精选.doc
  10. chrome 样式Bug?
  11. Educational Codeforces Round 67 E.Tree Painting (树形dp)
  12. vvv在线文档导出工具_墙裂推荐 | 在线文档编辑工具
  13. 知识点:西门子仿真PLCWindows平台测试说明
  14. 云服务器里存放数据安全吗
  15. Deepin系统安装docker
  16. Spring Bean生命周期: Bean的实例化
  17. mysql漏洞如何打补丁_“永恒之黑”针对win10和服务器漏洞分析
  18. 如何在A4纸上打印连续的条形码
  19. 更改iTunes(macOS下)备份路径至移动硬盘
  20. python唐诗分析综合_Python爬虫抓取唐诗宋词

热门文章

  1. 将页面元素置为不可修改Readonly,所有元素统一修改,统一调用
  2. ubuntu 修改时区、时间、同步网络时间、将时间写入硬件
  3. 2022-2028年中国氧化铟锡薄膜行业市场深度分析及前瞻研究报告
  4. 格式化_icecream_python
  5. 宿主机虚拟机文件复制 apt-get 换成yum
  6. java中调用python
  7. Android Studio中RecycerView依赖库加载问题
  8. 自动调度GPU的卷积层
  9. Nucleus-SE迁移:未实现的设施和兼容性
  10. GPU上创建目标检测Pipeline管道