ZOJ 3811 Untrusted Patrol dfs

本文解析了一道关于仓库巡逻的安全人员路径验证算法题。通过使用并查集和深度优先搜索等技术,验证了安全人员是否按照规定检查了所有货物堆。文章详细记录了解题过程中的思考及遇到的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 <= AiBi <= 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

Author: DAI, Longao
Source: The 2014 ACM-ICPC Asia Mudanjiang Regional First Round

 

今天做的网络赛,真是简直了,我来总结下这道题目所犯的一系列错误,做这道题目做的好曲折啊。

1.刚开始以为是割点,看了一会,不知道怎么在搜索的过程中判断如果当前节点是割点的话,儿子节点是不是已经访问过了,直到我自己找到了一种割点的反例,就是一种环,哭瞎了。

2.后来的思路和正解有点相近了,就是先把序列的点都标记下来,不能访问。然后,我是这样想的,就是在序列中两两之间找路径,正解也确实是这个思路,但是在确定这个路径的时候,我只想到了这两个点之间做一个dfs,,这样必超时。

3.这样想着想着就到了5点多,最终在网络赛期间也没有做出来。

4.看到了同学是一个一个点的拓展成一个区域,序列中下个点如果拓展的点和这个区域有重合的那么就说明有一条路径,啊啊啊啊啊啊啊,我当时咋没有想到,所以这样来说并查集也是可以做的。

5细节问题:l<k 一定是NO,图不连通的话一定是NO。

 

 代码一 并查集

————————————————————————————————————哭瞎——————————————————————————-——————————————-

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<string>
  5 #include<map>
  6 #include<set>
  7 #include<cmath>
  8 #include<stack>
  9 #include<queue>
 10 #include<vector>
 11 #include<algorithm>
 12 #include<sstream>
 13 #define inf 0x3f3f3f3f
 14 #define PI acos(-1.0)
 15 #define eps 1e-6
 16 #define LL long long
 17 #define MEM(a,b) memset(a,b,sizeof(a))
 18 #define PB push_back
 19 #define IN freopen("in.txt","r",stdin);
 20 #define OUT freopen("out.txt","w",stdout);
 21 #define BUG printf("bug************bug************bug\n");
 22 
 23 using namespace std;
 24 
 25 const int N=100010;
 26 vector <int>G[N];
 27 bool use[N],visit[N];
 28 int q[N],f[N];
 29 bool flag;
 30 
 31 int Find(int u)
 32 {
 33     if (f[u]!=u)
 34         f[u]=Find(f[u]);
 35     return f[u];
 36 }
 37 
 38 bool Union(int u,int v)
 39 {
 40     int fa1=Find(u);
 41     int fa2=Find(v);
 42     f[fa2]=fa1;
 43     return fa1==fa2;
 44 }
 45 
 46 void dfs(int u)
 47 {
 48     for (int i=0;i<G[u].size();i++){
 49         int v=G[u][i];
 50         if (use[v]){
 51             if (!Union(u,v))
 52                 dfs(v);
 53         }
 54     }
 55 }
 56 
 57 int main()
 58 {
 59     int T,n,m,k,u,v,l,fa1,fa2;
 60     bool ans;
 61     scanf("%d",&T);
 62     while (T--){
 63         MEM(use,true);
 64         scanf("%d%d%d",&n,&m,&k);
 65         for (int i=1;i<=n;i++){
 66             G[i].clear();
 67             f[i]=i;
 68         }
 69         for (int i=1;i<=k;i++){
 70             scanf("%d",&v);
 71             use[v]=false;
 72         }
 73         for (int i=1;i<=m;i++){
 74             scanf("%d%d",&u,&v);
 75             G[u].push_back(v);
 76             G[v].push_back(u);
 77         }
 78         scanf("%d",&l);
 79         for (int i=1;i<=l;i++)
 80                 scanf("%d",&q[i]);
 81         if (l<k){
 82             printf("No\n");
 83         }
 84         else{
 85             dfs(q[1]);
 86             use[q[1]]=true;
 87             ans=true;
 88             for (int i=2;i<=l;i++){
 89                 dfs(q[i]);
 90                 use[q[i]]=true;
 91                 fa1=Find(q[i-1]);
 92                 fa2=Find(q[i]);
 93                 if (fa1!=fa2){
 94                     ans=false;
 95                     break;
 96                 }
 97             }
 98             for (int i=1;i<=n;i++)
 99                 Find(i);
100             for (int i=2;i<=n;i++)
101                 if (f[i]!=f[1])
102                     ans=false;
103             if (ans)
104                 printf("Yes\n");
105             else
106                 printf("No\n");
107         }
108     }
109     return 0;
110 }

 代码二 类似floodfill

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#include<algorithm>
#include<sstream>
#define inf 0x3f3f3f3f
#define PI acos(-1.0)
#define eps 1e-6
#define LL long long
#define MEM(a,b) memset(a,b,sizeof(a))
#define PB push_back
#define IN freopen("in.txt","r",stdin);
#define OUT freopen("out.txt","w",stdout);
#define BUG printf("bug************bug************bug\n");

using namespace std;

const int N=100010;
vector <int>G[N];
bool use[N],visit[N];
int q[N];
bool flag;

void dfs(int u)
{
    for (int i=0;i<G[u].size();i++){
        int v=G[u][i];
        if (visit[v]){
            flag=true;
            continue;
        }
        else{
            if (!use[v])
                continue;
            use[v]=false;
            dfs(v);
            visit[v]=true;
        }
    }
}

int main()
{
    int T,n,m,k,u,v,l;
    bool ans;
    scanf("%d",&T);
    while (T--){
        MEM(use,true);
        MEM(visit,false);
        scanf("%d%d%d",&n,&m,&k);
        for (int i=1;i<=n;i++)
            G[i].clear();
        for (int i=1;i<=k;i++){
            scanf("%d",&v);
            use[v]=false;
        }
        for (int i=1;i<=m;i++){
            scanf("%d%d",&u,&v);
            G[u].push_back(v);
            G[v].push_back(u);
        }
        scanf("%d",&l);
        for (int i=1;i<=l;i++)
                scanf("%d",&q[i]);
        if (l<k){
            printf("No\n");
        }
        else{
            dfs(q[1]);
            visit[q[1]]=true;
            ans=true;
            for (int i=2;i<=l;i++){
                flag=false;
                dfs(q[i]);
                visit[q[i]]=true;
                if (!flag){
                    ans=false;
                    break;
                }
            }
            for (int i=1;i<=n;i++)
                if (!visit[i])
                    ans=false;
            if (ans)
                printf("Yes\n");
            else
                printf("No\n");
        }
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/wzb-hust/p/3960759.html

内容概要:该论文聚焦于T2WI核磁共振图像超分辨率问题,提出了一种利用T1WI模态作为辅助信息的跨模态解决方案。其主要贡献包括:提出基于高频信息约束的网络框架,通过主干特征提取分支和高频结构先验建模分支结合Transformer模块和注意力机制有效重建高频细节;设计渐进式特征匹配融合框架,采用多阶段相似特征匹配算法提高匹配鲁棒性;引入模型量化技术降低推理资源需求。实验结果表明,该方法不仅提高了超分辨率性能,还保持了图像质量。 适合人群:从事医学图像处理、计算机视觉领域的研究人员和工程师,尤其是对核磁共振图像超分辨率感兴趣的学者和技术开发者。 使用场景及目标:①适用于需要提升T2WI核磁共振图像分辨率的应用场景;②目标是通过跨模态信息融合提高图像质量,解决传统单模态方法难以克服的高频细节丢失问题;③为临床诊断提供更高质量的影像资料,帮助医生更准确地识别病灶。 其他说明:论文不仅提供了详细的网络架构设计与实现代码,还深入探讨了跨模态噪声的本质、高频信息约束的实现方式以及渐进式特征匹配的具体过程。此外,作者还对模型进行了量化处理,使得该方法可以在资源受限环境下高效运行。阅读时应重点关注论文中提到的技术创新点及其背后的原理,理解如何通过跨模态信息融合提升图像重建效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值