2014牡丹江网络zoj3816Generalized Palindromic Number(dfs或者bfs)

  1 #include <iostream>
  2 #include <stdio.h>
  3 #include <cmath>
  4 #include <algorithm>
  5 #include <iomanip>
  6 #include <cstdlib>
  7 #include <string>
  8 #include <memory.h>
  9 #include <vector>
 10 #include <queue>
 11 #include <stack>
 12 #include <map>
 13 #include <set>
 14 #include <ctype.h>
 15 #include <sstream>
 16 #define INF 1000000000
 17 #define ll long long
 18 #define min3(a,b,c) min(a,min(b,c))
 19 #define max3(a,b,c) max(a,max(b,c))
 20 #define MAXN 100010
 21
 22 using namespace std;
 23
 24 bool mk[100010];
 25 bool vis[100010];
 26 bool open[100010];
 27
 28 vector<int> E[100010];
 29 queue<int> K;
 30
 31 int main(){
 32     int t;
 33     cin>>t;
 34     while(t--){
 35         memset(mk,0,sizeof(mk));
 36         memset(E,0,sizeof(E));
 37         memset(vis,0,sizeof(vis));
 38         memset(open,0,sizeof(open));
 39         while(!K.empty()) K.pop();
 40
 41         int n,m,k;
 42         cin>>n>>m>>k;
 43
 44         for(int i=1;i<=k;i++){
 45             int tmp;
 46             cin>>tmp;
 47         }
 48
 49         for(int i=1;i<=m;i++){
 50             int u,v;
 51             scanf("%d%d",&u,&v);
 52             E[u].push_back(v);
 53             E[v].push_back(u);
 54         }
 55
 56
 57         int L;
 58         cin>>L;
 59
 60         for(int i=1;i<=L;i++){
 61             int tmp;
 62             cin>>tmp;
 63             mk[tmp]=1;
 64             K.push(tmp);
 65         }
 66         if(L<k){
 67             cout<<"No"<<endl;
 68             continue;
 69         }
 70
 71         bool ok=0;
 72
 73          int nt;
 74         int start=K.front(); K.pop(); mk[start]=0; vis[start]=1;
 75         queue<int> que;
 76         que.push(start);
 77         if(!K.empty())  nt=K.front();
 78         while(!que.empty()){
 79             int cur=que.front(); que.pop();
 80             int siz=E[cur].size();
 81             for(int i=0;i<siz;i++){
 82                 int v=E[cur][i];
 83                 if(!vis[v]&&!mk[v]){
 84                     vis[v]=1;
 85                     que.push(v);
 86                 }
 87
 88                 if(v==nt&&!vis[v]){
 89                     vis[v]=1;
 90                     que.push(v);
 91                     mk[v]=0;
 92                     K.pop();
 93                     if(!K.empty()) nt=K.front();
 94                 }
 95                 else if(!vis[v]&&mk[v])
 96                     open[v]=1;
 97             }
 98
 99             if(que.empty()){
100                 if(open[nt]==1&&!vis[nt]){
101                     K.pop();
102                     que.push(nt);
103                     mk[nt]=0;
104                     vis[nt]=1;
105                     if(!K.empty())
106                         nt=K.front();
107                 }
108             }
109         }
110
111         bool flag=true;
112         for(int i=1; i<=n; ++i)
113            if(!vis[i]){
114                    flag=false;
115                    break;
116            }
117         if(K.empty() && flag) ok=1;
118         if(ok){
119             cout<<"Yes"<<endl;
120         }else{
121             cout<<"No"<<endl;
122         }
123
124     }
125     return 0;
126
127 }
  1 /*
  2     题意: 给定一个n个节点m条边的无向图,接着又给出一个序列(由图中的一些节点所组成)
  3     问能否按照给定序列的顺序来访问图,并且保证图可以访问完毕!
  4
  5     思路:是可以走回头路的搜索!那么我们就按照给定的序列进行搜索,如果当前的节点在给定序列中
  6     出现过,但是访问的顺序和给定序列的顺序不一样,那么就将该节点标记一下open[];
  7     第一次搜索完毕之后,接着从open[]标记的节点(并且该节点符合给定序列的顺序)开始搜索!
  8     最后不要忘记检查图的连通性....
  9     如果不清楚,还是看代码吧....
 10 */
 11 #include<iostream>
 12 #include<cstring>
 13 #include<cstdio>
 14 #include<algorithm>
 15 #include<vector>
 16 #define N 100005
 17 using namespace std;
 18
 19 vector<int>g[N];
 20 int vis[N], open[N];
 21 int mk[N], que[N];
 22 int n, m, k, l, cnt;
 23 bool flag;
 24
 25 void init(){
 26     memset(vis, 0, sizeof(vis));
 27     memset(open, 0, sizeof(open));
 28     memset(mk, 0, sizeof(mk));
 29     memset(g, 0, sizeof(g));
 30 }
 31
 32 void dfs(int u){
 33     vis[u]=1;
 34     open[u]=0;
 35     if(u==que[cnt]){
 36         ++cnt;
 37         if(cnt>k) flag=true;
 38     }
 39     else if(mk[u])
 40        vis[u]=0;
 41     int len=g[u].size();
 42     for(int i=0; i<len; ++i){
 43         int v=g[u][i];
 44         if(!vis[v]){
 45             if(mk[v] && v!=que[cnt]){
 46                 open[v]=1;
 47                 continue;
 48             }
 49             if(open[v] && v!=que[cnt])
 50                 continue;
 51             dfs(v);
 52         }
 53     }
 54 }
 55
 56 int main(){
 57     int t;
 58     scanf("%d", &t);
 59     while(t--){
 60         init();
 61         scanf("%d%d%d", &n, &m, &k);
 62         for(int i=1; i<=k; ++i){
 63             int x;
 64             scanf("%d", &x);
 65             mk[x]=1;
 66         }
 67
 68         while(m--){
 69             int u, v;
 70             scanf("%d%d", &u, &v);
 71             g[u].push_back(v);
 72             g[v].push_back(u);
 73         }
 74         scanf("%d", &l);
 75         for(int i=1; i<=l; ++i)
 76             scanf("%d", &que[i]);
 77         if(l<k){
 78             printf("No\n");
 79             continue;
 80         }
 81         cnt=1;
 82         flag=false;
 83         open[que[cnt]]=1;
 84         while(cnt<=k){//就是从给定序列节点开始并且open标记开始搜索
 85                 if(!open[que[cnt]]) break;//如果访问到给定序列的当前节点没有被open标记,说明
 86                 dfs(que[cnt]);            //不能按照给定的序列的顺序访问图中的节点
 87         }
 88         if(flag){
 89                int i;
 90                for(i=1; i<=n; ++i)
 91                   if(!vis[i])  break;
 92                if(i>n)
 93                   printf("Yes\n");
 94                   else printf("No\n");
 95         }
 96         else
 97                   printf("No\n");
 98     }
 99     return 0;
100 }
时间: 2024-10-11 07:52:25

2014牡丹江网络zoj3816Generalized Palindromic Number(dfs或者bfs)的相关文章

ZOJ3816-Generalized Palindromic Number(DFS数位搜索)

Generalized Palindromic Number Time Limit: 2 Seconds      Memory Limit: 65536 KB A number that will be the same when it is written forwards or backwards is known as a palindromic number. For example, 1234321 is a palindromic number. We call a number 

ZOJ - 3816 Generalized Palindromic Number dfs

Generalized Palindromic Number Time Limit: 2 Seconds                                     Memory Limit: 65536 KB A number that will be the same when it is written forwards or backwards is known as a palindromic number. For example, 1234321 is a palind

ZOJ-3811 Untrusted Patrol DFS 2014牡丹江网络赛C题

n个点,m条双向边,k个传感器.其中有l个传感器记录到了第一次到达的时间顺序,求是否有可能检查了所有的顶点. 首先判断l,l<k一定是不行的.然后按照传感器的时间顺序dfs,先从第一个传感器位置搜索dfs搜到所有的到达传感器的位置结束,如果这个传感器被标记为可访问,则从这个传感器继续搜索下一层传感器,否则是不能满足时间顺序的.最后还要判断整个图的连通性,所有的顶点都要可以到达的. #include <iostream> #include <cstdio> #include &

2014牡丹江网络预选赛F题(隐式图BFS暴搜)zoj3814

Sawtooth Puzzle Time Limit: 10 Seconds      Memory Limit: 65536 KB Recently, you found an interesting game called Sawtooth Puzzle. This is a single-player game played on a grid with 3 x 3 cells. Each cell contains a part of an image. Besides, each ed

ZOJ 3816 Generalized Palindromic Number dfs+暴力枚举

题目链接:点击打开链接 题意: 给定一个数n 找一个最大的数u使得u<n && u为回文. 枚举前面有多少位是一样的.然后分类讨论.啪啦啪啦 #include <cstdio> #include <algorithm> #include <cstring> #include <iostream> #include <vector> using namespace std; typedef long long ll; cons

2014牡丹江网络赛解题报告

The 2014 ACM-ICPC Asia Mudanjiang Regional First Round 题目链接 A题解题报告 B题解题报告 C题解题报告 D题解题报告 E题解题报告 F题解题报告 G题(未完成) H题解题报告 I题解题报告 J题解题报告

2014牡丹江网络预选赛D题(状压DP)zoj3812

We Need Medicine Time Limit: 10 Seconds      Memory Limit: 65536 KB      Special Judge A terrible disease broke out! The disease was caused by a new type of virus, which will lead to lethal lymphoedema symptom. For convenience, it was named LL virus.

2014牡丹江网络预选赛B题(找规律)zoj3810

A Volcanic Island Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge An underwater volcano has erupted massively in somewhere of the deep Atlantis Ocean. This large eruption led to the birth of a new volcanic island, which had a sha

2014牡丹江网络预选赛E题(线段树)zoj3813

Alternating Sum Time Limit: 2 Seconds      Memory Limit: 65536 KB There is a digit string S with infinite length. In addition, S is periodic and it can be formed by concatenating infinite repetitions of a base string P. For example, if P = 3423537, t