zoj 3811 Untrusted Patrol(bfs或dfs)

本文深入探讨了AI算法在不同领域的应用,从大数据到嵌入式硬件,从图像处理到区块链,全面覆盖了信息技术的各个细分领域。

摘要生成于 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 <= 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

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

 

 

据说这题是bfs裸题,事实也是如此。

这里用两种实现方法,dfs和bfs,实际上这两种方法差不多。

注意两种特判条件。

 

dfs

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<vector>
 5 #include<set>
 6 #include<map>
 7 using namespace std;
 8 #define N 100006
 9 int n,m,k;
10 vector<int> v[N];
11 set<int> s;
12 int tag[N];
13 int vis[N];
14 int a[N];
15 void dfs(int st){
16     vis[st]=1;
17     for(int i=0;i<v[st].size();i++){
18         int u=v[st][i];
19         if(!vis[u]){
20             if(tag[u]) s.insert(u);
21             else dfs(u);
22         }
23     }
24 }
25 int main()
26 {
27     int t;
28     scanf("%d",&t);
29     while(t--){
30 
31         for(int i=0;i<N;i++){
32             v[i].clear();
33         }
34         memset(tag,0,sizeof(tag));
35         s.clear();
36         memset(vis,0,sizeof(vis));
37 
38         scanf("%d%d%d",&n,&m,&k);
39         int x,y;
40         for(int i=0;i<k;i++)scanf("%d",&x);
41         for(int i=0;i<m;i++){
42             scanf("%d%d",&x,&y);
43             v[x].push_back(y);
44             v[y].push_back(x);
45         }
46         int L;
47         scanf("%d",&L);
48         for(int i=0;i<L;i++){
49             scanf("%d",&a[i]);
50             tag[a[i]]=1;
51         }
52         if(L<k){
53             printf("No\n");
54             continue;
55         }
56         dfs(a[0]);
57 
58         int flag=1;
59         for(int i=1;i<L;i++){
60             if(s.find(a[i])==s.end()){
61                 flag=0;
62                 break;
63             }
64             else{
65                 s.erase(a[i]);
66                 dfs(a[i]);
67             }
68         }
69 
70         for(int i=1;i<=n;i++){
71             if(!vis[i]){
72                 flag=0;
73                 break;
74             }
75         }
76         if(flag==1){
77             printf("Yes\n");
78         }else{
79             printf("No\n");
80         }
81 
82     }
83     return 0;
84 }
View Code

 

bfs

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<vector>
 5 #include<set>
 6 #include<map>
 7 #include<queue>
 8 using namespace std;
 9 #define N 100006
10 int n,m,k;
11 vector<int> v[N];
12 set<int> s;
13 int tag[N];
14 int vis[N];
15 int a[N];
16 void bfs(int st){
17     queue<int>q;
18     q.push(st);
19     vis[st]=1;
20     while(!q.empty()){
21         int tmp=q.front();
22         q.pop();
23         for(int i=0;i<v[tmp].size();i++){
24             int u=v[tmp][i];
25             if(!vis[u]){
26                 if(tag[u]) s.insert(u);
27                 else q.push(u);
28                 vis[u]=1;
29             }
30         }
31     }
32 }
33 int main()
34 {
35     int t;
36     scanf("%d",&t);
37     while(t--){
38 
39         for(int i=0;i<N;i++){
40             v[i].clear();
41         }
42         memset(tag,0,sizeof(tag));
43         s.clear();
44         memset(vis,0,sizeof(vis));
45 
46         scanf("%d%d%d",&n,&m,&k);
47         int x,y;
48         for(int i=0;i<k;i++)scanf("%d",&x);
49         for(int i=0;i<m;i++){
50             scanf("%d%d",&x,&y);
51             v[x].push_back(y);
52             v[y].push_back(x);
53         }
54         int L;
55         scanf("%d",&L);
56         for(int i=0;i<L;i++){
57             scanf("%d",&a[i]);
58             tag[a[i]]=1;
59         }
60         if(L<k){
61             printf("No\n");
62             continue;
63         }
64         bfs(a[0]);
65 
66         int flag=1;
67         for(int i=1;i<L;i++){
68             if(s.find(a[i])==s.end()){
69                 flag=0;
70                 break;
71             }
72             else{
73                 s.erase(a[i]);
74                 bfs(a[i]);
75             }
76         }
77         for(int i=1;i<=n;i++){
78             if(!vis[i]){
79                 flag=0;
80                 break;
81             }
82         }
83         if(flag) printf("Yes\n");
84         else printf("No\n");
85     }
86     return 0;
87 }
View Code

 

 


 

内容概要:该研究通过在黑龙江省某示范村进行24小时实地测试,比较了燃煤炉具与自动/手动进料生物质炉具的污染物排放特征。结果显示,生物质炉具相比燃煤炉具显著降低了PM2.5、CO和SO2的排放(自动进料分别降低41.2%、54.3%、40.0%;手动进料降低35.3%、22.1%、20.0%),但NOx排放未降低甚至有所增加。研究还发现,经济性和便利性是影响生物质炉具推广的重要因素。该研究不仅提供了实际排放数据支持,还通过Python代码详细复现了排放特征比较、减排效果计算和结果可视化,进一步探讨了燃料性质、动态排放特征、碳平衡计算以及政策建议。 适合人群:从事环境科学研究的学者、政府环保部门工作人员、能源政策制定者、关注农村能源转型的社会人士。 使用场景及目标:①评估生物质炉具在农村地区的推广潜力;②为政策制定者提供科学依据,优化补贴政策;③帮助研究人员深入了解生物质炉具的排放特征和技术改进方向;④为企业研发更高效的生物质炉具提供参考。 其他说明:该研究通过大量数据分析和模拟,揭示了生物质炉具在实际应用中的优点和挑战,特别是NOx排放增加的问题。研究还提出了多项具体的技术改进方向和政策建议,如优化进料方式、提高热效率、建设本地颗粒厂等,为生物质炉具的广泛推广提供了可行路径。此外,研究还开发了一个智能政策建议生成系统,可以根据不同地区的特征定制化生成政策建议,为农村能源转型提供了有力支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值