ZOJ 3811 / 2014 牡丹江赛区网络赛 C. Untrusted Patrol bfs/dfs/并查集
Untrusted Patrol
Time Limit: 3 Seconds Memory Limit: 65536 KB
Edward is a rich man. He owns a large factory for health drink production. As a matter of course, there is a large warehouse in the factory.
To ensure the safety of drinks, Edward hired a security man to patrol the warehouse. The warehouse has N piles of drinks and M passageways connected them (warehouse is not big enough). When the evening comes, the security man will start to patrol the warehouse following a path to check all piles of drinks.
Unfortunately, Edward is a suspicious man, so he sets sensors on K piles of the drinks. When the security man comes to check the drinks, the sensor will record a message. Because of the memory limit, the sensors can only record for the first time of the security man's visit.
After a peaceful evening, Edward gathered all messages ordered by recording time. He wants to know whether is possible that the security man has checked all piles of drinks. Can you help him?
The security man may start to patrol at any piles of drinks. It is guaranteed that the sensors work properly. However, Edward thinks the security man may not works as expected. For example, he may digs through walls, climb over piles, use some black magic to teleport to anywhere and so on.
Input
There are multiple test cases. The first line of input is an integer T indicates the number of test cases. For each test case:
The first line contains three integers N (1 <= N <= 100000), M (1 <= M <= 200000) and K (1 <= K <= N).
The next line contains K distinct integers indicating the indexes of piles (1-based) that have sensors installed. The following M lines, each line contains two integers Ai and Bi (1 <= Ai, Bi <= N) which indicates a bidirectional passageway connects piles Ai and Bi.
Then, there is an integer L (1 <= L <= K) indicating the number of messages gathered from all sensors. The next line contains L distinct integers. These are the indexes of piles where the messages came from (each is among the K integers above), ordered by recording time.
Output
For each test case, output "Yes" if the security man worked normally and has checked all piles of drinks, or "No" if not.
Sample Input
2
5 5 3
1 2 4
1 2
2 3
3 1
1 4
4 5
3
4 2 1
5 5 3
1 2 4
1 2
2 3
3 1
1 4
4 5
3
4 1 2
Sample Output
No
Yes 转自:http://blog.csdn.net/napoleon_acm/article/details/39124615
题目: LINK
给定一个无向图,n个点, m条边,k个特殊点(有传感器),只有当第一次到达特殊点的时候才会发出信号,给出发出信号的序列,问是否存在这样的路径使得每个点至少遍历一次,而且特殊点第一次到达的顺序和和题目输入一样。 (1 <= N <= 100000), M (1 <= M <= 200000) 先特判 如果询问时输入的L<k, 那么直接No, 因为l<k肯定有传感器的点没有到达,不满足每个点都遍历一次。 先把第一个特殊点入队,遍历所有的可以到达的点(中途不经过其他特殊点),标记为可以到达。 之后判断第二个点是否可以从第一个点到达(是否被标记),如果不可以则No,否则遍历从第二个特殊点出发的可以到达的点(同样中途不经过剩余特殊点). ....... 依次处理完所有点即可. 如果前面的一个特殊点可以到达某个点,那么 他后面的特殊点一定也可以到达这些点,因为他可以回到前面的特殊点再走过去.
本题可用多种思路做,但是拍代码前一定要先分析好再开拍,不然,,,
可用方法:bfs、dfs、并查集。
吐血ac。。。
代码:
bfs:
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map> #define N 100005
#define M 15
#define mod 6
#define mod2 100000000
#define ll long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int T;
int n,m,k,l;
int vis[N];
int vis2[N];
vector<int>bian[N];
int re[N];
int a,b;
vector<int>::iterator it; void ini()
{
int i;
memset(vis,,sizeof(vis));
memset(vis2,,sizeof(vis2));
for(i=;i<=;i++){
bian[i].clear();
}
scanf("%d%d%d",&n,&m,&k);
for(i=;i<=k;i++){
scanf("%d",&re[i]);
}
while(m--){
scanf("%d%d",&a,&b);
bian[a].push_back(b);
bian[b].push_back(a);
}
scanf("%d",&l);
for(i=;i<=l;i++){
scanf("%d",&re[i]);
vis2[ re[i] ]=;
}
} int solve()
{
if(l!=k){
return ;
}
queue<int>q;
int r;
vis[ re[] ]=;
for(int i=;i<=l;i++){
// printf(" i=%d re=%d vis=%d\n",i,re[i],vis[ re[i] ]);
if(vis[ re[i] ]==) return ;
// vis2[ re[i] ]=0;
q.push(re[i]);
while( q.size()> )
{
r=q.front();
//printf(" r=%d\n",r);
q.pop();
// vis[r]=1; for(it=bian[r].begin();it!=bian[r].end();it++){
// printf(" it=%d\n",*it);
if(vis[*it]==) continue;
vis[ *it ]=;
if( vis2[ *it ]== ){
q.push( (*it) );
}
}
/*
for(int j=0;j<(int)bian[r].size();j++){
int y=bian[r][j];
if(vis[y]==1) continue;
vis[y]=1;
if(vis2[y]==0){
q.push(y); }
}*/
}
} for(int i=;i<=n;i++){
if(vis[i]==) return ;
}
return ;
} int main()
{
//freopen("data.in","r",stdin);
scanf("%d",&T);
for(int cnt=;cnt<=T;cnt++)
// while(T--)
//while(scanf("%I64d%I64d%I64d",&a,&b,&c)!=EOF)
{
ini();
if(solve()==){
printf("No\n");
}
else{
printf("Yes\n");
}
} return ;
}
dfs版本:
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map> #define N 100005
#define M 15
#define mod 6
#define mod2 100000000
#define ll long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int T;
int n,m,k,l;
int vis[N];
int vis2[N];
vector<int>bian[N];
int re[N];
int a,b; void ini()
{
int i;
memset(vis,,sizeof(vis));
memset(vis2,,sizeof(vis2));
for(i=;i<=;i++){
bian[i].clear();
}
scanf("%d%d%d",&n,&m,&k);
for(i=;i<=k;i++){
scanf("%d",&re[i]);
}
while(m--){
scanf("%d%d",&a,&b);
bian[a].push_back(b);
bian[b].push_back(a);
}
scanf("%d",&l);
for(i=;i<=l;i++){
scanf("%d",&re[i]);
vis2[ re[i] ]=;
}
} void dfs(int s)
{
vector<int>::iterator it;
for(it=bian[s].begin();it!=bian[s].end();it++){
if(vis[*it]==) continue;
vis[*it]=;
if(vis2[*it]==){
dfs(*it);
}
}
} int solve()
{
if(l!=k){
return ;
}
vis[ re[] ]=;
for(int i=;i<=l;i++){
// printf(" i=%d re=%d vis=%d\n",i,re[i],vis[ re[i] ]);
if(vis[ re[i] ]==) return ;
dfs(re[i]);
} for(int i=;i<=n;i++){
if(vis[i]==) return ;
}
return ;
} int main()
{
//freopen("data.in","r",stdin);
scanf("%d",&T);
for(int cnt=;cnt<=T;cnt++)
// while(T--)
//while(scanf("%I64d%I64d%I64d",&a,&b,&c)!=EOF)
{
ini();
if(solve()==){
printf("No\n");
}
else{
printf("Yes\n");
}
} return ;
}
并查集版:http://blog.csdn.net/qq574857122/article/details/39121391
转自:
思路:
先把无触发器的点放到图里。
然后根据触发器的信号顺序把点依次加入图中,加入时只添加(与无触发器点相连的边)
然后判断这个点能否与之前的图连通。bfs+并查集判断。
==其实并查集就够了,写的时候有脑洞,bfs就冒出来了
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map> #define N 100005
#define M 15
#define mod 6
#define mod2 100000000
#define ll long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int T;
int n,m,k,l;
int f[N];
int rank[N];
int vis2[N];
vector<int>bian[N];
int re[N];
vector<int>::iterator it; int find(int v)
{
//printf(" v=%d f=%d\n",v,f[v]);
return f[v]=f[v]==v ? v : find(f[v]);
} void merge(int x,int y)
{
int a=find(x),b=find(y);
if(a==b) return;
if(rank[a]<rank[b]){
f[a]=b;
}
else{
f[b]=a;
if(rank[b]==rank[a]){
++rank[a];
}
}
} void ini()
{
int i;
int a,b;
//memset(vis,0,sizeof(vis));
memset(rank,,sizeof(rank));
memset(vis2,,sizeof(vis2));
for(i=;i<=;i++){
bian[i].clear();
} scanf("%d%d%d",&n,&m,&k);
for(i=;i<=n;i++){
f[i]=i;
// printf(" i=%d f=%d\n",i,f[i]);
}
for(i=;i<=k;i++){
scanf("%d",&re[i]);
}
while(m--){
scanf("%d%d",&a,&b);
bian[a].push_back(b);
bian[b].push_back(a);
}
scanf("%d",&l);
for(i=;i<=l;i++){
scanf("%d",&re[i]);
vis2[ re[i] ]=;
}
} int solve()
{
int i,x,y;
if(l!=k){
return ;
} // for(i=1;i<=n;i++){
// printf(" i=%d f=%d\n",i,f[i]);
// }
// vis[ re[1] ]=1;
for(i=;i<=n;i++){
if(vis2[i]==) continue;
for(it=bian[i].begin();it!=bian[i].end();it++){
if(vis2[*it]==) continue;
merge(i,*it);
}
} i=;
for(it=bian[ re[i] ].begin();it!=bian[ re[i] ].end();it++){
if(vis2[*it]==) continue;
merge(re[i],*it);
}
vis2[ re[] ]=; for(i=;i<=l;i++){
// printf(" i=%d re=%d vis=%d\n",i,re[i],vis[ re[i] ]);
for(it=bian[ re[i] ].begin();it!=bian[ re[i] ].end();it++){
if(vis2[*it]==) continue;
merge(re[i],*it);
}
x=find(re[i-]);
y=find(re[i]);
//printf(" re[i-1]=%d re[i]=%d x=%d y=%d\n",re[i-1],re[i],x,y);
if(x!=y) return ;
vis2[ re[i] ]=;
}
x=find();
for(i=;i<=n;i++){
if(x!=find(i)) return ;
}
return ;
} int main()
{
//freopen("data.in","r",stdin);
scanf("%d",&T);
for(int cnt=;cnt<=T;cnt++)
// while(T--)
//while(scanf("%I64d%I64d%I64d",&a,&b,&c)!=EOF)
{
ini();
if(solve()==){
printf("No\n");
}
else{
printf("Yes\n");
}
} return ;
}
ZOJ 3811 / 2014 牡丹江赛区网络赛 C. Untrusted Patrol bfs/dfs/并查集的更多相关文章
- hdu5441(2015长春赛区网络赛1005)类最小生成树、并查集
题意:有一张无向图,一些点之间有有权边,某条路径的值等于路径上所有边的边权的最大值,而某个点对的值为这两点间所有路径的值的最小值,给出多个询问,每个询问有一个值,询问有多少点对满足其值小于等于询问值. ...
- ZOJ 3810 A Volcanic Island (2014年牡丹江赛区网络赛B题)
1.题目描写叙述:点击打开链接 2.解题思路:本题是四色定理的模板题.只是有几种情况要提前特判一下:n==1直接输出,1<n<5时候无解,n==6时候套用模板会出现同样的块.因此要特判一下 ...
- ZOJ 3814 Sawtooth Puzzle (2014年牡丹江赛区网络赛F题)
1.题目描写叙述:点击打开链接 2.解题思路:本题是一道隐式图的搜索题目.一般来说,这类题目首先要定义状态,接下来是弄清楚状态怎样转移,以及状态怎样判重,怎样推断当前状态是否和目标状态同样.至于求解最 ...
- hdu5438(2015长春赛区网络赛1002)拓扑序+DFS
题意:给出一张无向图,每个节点有各自的权值,问在点数为奇数的圈中的点的权值总和是多少. 通过拓扑序的做法标记出所有非圈上的点,做法就是加每条边的时候将两点的入度都加一,然后将所有度数为1的点入队,删去 ...
- HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011亚洲北京赛区网络赛)
HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011 亚洲北京赛区网络赛题目) Eliminate Witches! Time Limit: 2000/1000 ...
- HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛)
HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛) Panda Time Limit: 10000/4000 MS (Java/Others) Memory Limit: ...
- ICPC 2018 徐州赛区网络赛
ACM-ICPC 2018 徐州赛区网络赛 去年博客记录过这场比赛经历:该死的水题 一年过去了,不被水题卡了,但难题也没多做几道.水平微微有点长进. D. Easy Math 题意: ...
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 M. Frequent Subsets Problem【状态压缩】
2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 M. Frequent Subsets Problem 题意:给定N和α还有M个U={1,2,3,...N}的子集,求子集X个数,X满足:X是U ...
- HDU 5875 Function -2016 ICPC 大连赛区网络赛
题目链接 网络赛的水实在太深,这场居然没出线zzz,差了一点点,看到这道题的的时候就剩半个小时了.上面是官方的题意题解,打完了才知道暴力就可以过,暴力我们当时是想出来了的,如果稍稍再优化一下估计就过了 ...
随机推荐
- 关于HTML5中Video标签无法播放mp4的解决办法
1.首先先排除掉代码问题.路径问题.浏览器不支持问题等常规问题,这些问题另行百度. <video width="500px" height="300px" ...
- leetcode_1033. Moving Stones Until Consecutive
https://leetcode.com/problems/moving-stones-until-consecutive/ 题意:给定3个点,每次从两个端点(位置最小或位置最大)中挑选一个点进行移动 ...
- tp5 -- 微信公众号支付
近来期间比较忙, 忙完之后发现最近有挺多的东西没有整理,于是乎.就将以前用到的一些小东西整理了一下. 如果对您有帮助,则是我最大的幸运. 本篇主要是说了一下整合TP5的微信公众号支付. 不过由于最近T ...
- FTP文传协议的应用
我开发的项目中一直用到都是AFNetworking上传图片的方法,最近老大说要用FTP上传,网上的资料很少,毕竟这种上传方式现在用的不多了,于是花了一天时间学习了FTP文件传输协议.下面是我的个人理解 ...
- Mac OSX用 dd 命令,浇灌ISO镜像到USB驱动器
Mac OSX用 dd 命令,浇灌ISO镜像到USB驱动器 字数244 阅读197 评论0 喜欢0 把ISO镜像转换为一个可启动的USB设备.一种可行的方法是通过OS X的Terminal “浇灌”到 ...
- 121. Best Time to Buy and Sell Stock@python
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- WebAssembly MDN简单使用
MDN 就是通过编译器编译完成c后生成的胶水代码 引入js 就能直接调用定义在c或者c++中的函数了 c代码如下: #include <stdio.h> #include <stdl ...
- UVa 167(八皇后)、POJ2258 The Settlers of Catan——记两个简单回溯搜索
UVa 167 题意:八行八列的棋盘每行每列都要有一个皇后,每个对角线上最多放一个皇后,让你放八个,使摆放位置上的数字加起来最大. 参考:https://blog.csdn.net/xiaoxiede ...
- html中注释的问题
在修改jsp页面的时候遇到了一个有点难懂的注释,mark一下 <script> <!-- alert("js code"); //--> </scri ...
- 【数论】贝壳找房计数比赛&&祭facinv
震惊!阶乘逆元处理背后竟有如此玄机…… 题目描述 贝壳找房举办了一场计数比赛,比赛题目如下. 给一个字符串 s 和字符串 t,求出 s 的所有去重全排列中 t 出现的次数.比如aab的去重全排列为aa ...