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,差了一点点,看到这道题的的时候就剩半个小时了.上面是官方的题意题解,打完了才知道暴力就可以过,暴力我们当时是想出来了的,如果稍稍再优化一下估计就过了 ...
随机推荐
- python 基础之运算符
运算符 a=10 ,b=20 运算符 描述 实例 + 加 - 两个对象相加 a + b 输出结果 30 - 减 - 得到负数或是一个数减去另一个数 a - b 输出结果 -10 * 乘 - 两个数相乘 ...
- python 判断路径是否存在
import os os.path.exists(文件绝对路径)
- off-by-one&doublefree. 看雪10月ctf2017 TSRC 第四题赛后学习
off-by-one 0x00 发现漏洞 1.off-by-one 在massage函数中,如图所示,可以修改的字节数比原内存大小多了一个字节 2.悬挂指针 可以看到,在free堆块的时候,没有清空指 ...
- Java代码实现文件上传(转载)
刚刚发表了一篇Java发送电子邮件,以前真是没注意,commons里这么多常用项目,惭愧呀,直到现在回顾;要学习的真是太多了,还是缺少真正的学习能力... 这里用到的是commons-fileuplo ...
- 转义字符 & sizeof & strlen
在定义了数组大小时: sizeof是运算符,表示编译时分配的空间大小,即数组定义的大小,char t[20] = "sfa".sizeof: 20; strlen: 3.在未定义数 ...
- Bootstrap 网页乱码
问题:今天早上在实践bootstrap的时候,用EditPlus写代码,标签中包含了中文.在浏览器解析的时候中文部分生成的乱码.但是网页部分已经声明了使用utf-8的编码方式. 解决:网页字体正常显示 ...
- windows 下phpstudy 升级mysql版本5.7
今天在导入sql文件的时候遇到了sql执行错误.最后找到原因是因为mysql版本过低,导致出错 原因:在执行sql的时候出现了两次CURRENT_TIMESTAMP ,最后得知在5.7版本之前都是不支 ...
- verilog RTL编程实践之四
1.verilog平时三个级别: 1.gate level: and or not xor 2.RTL level: reg comb seq 3.behavior:+ – * / 2.system ...
- 爬虫练习四:爬取b站番剧字幕
由于个人经常在空闲时间在b站看些小视频欢乐一下,这次就想到了爬取b站视频的弹幕. 这里就以番剧<我的妹妹不可能那么可爱>第一季为例,抓取这一番剧每一话对应的弹幕. 1. 分析页面 这部番剧 ...
- (原)pat1007素数猜想
---恢复内容开始--- 1007. 素数对猜想 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 让我们 ...