目录
思路:
从点1出发 进行dfs
dfs内 遍历和该点相连的其他点 如果颜色相同则返回false
主函数里遍历1~n的点 一旦有重复的就跳出
因为:图可能是不联通的,所以必须遍历1~n个点
如果1~n个点都满足不重复 则输出Yes 否则输出No
- #include
- using namespace std;
-
- const int N=510;
- int n,m,k;
- bool g[N][N];
- bool st[N];
- int color[N];
-
- bool dfs(int u)
- {
- st[u]=true;
- for(int i=1;i<=n;i++) //遍历该点相关联的节点
- if(g[u][i])
- {
- if(color[u]==color[i]) return false;
- if(!st[i])
- if(!dfs(i)) return false;
- }
- return true;
- }
-
- int main()
- {
- cin>>n>>m>>k;
- for(int i=0;i
- {
- int a,b;
- cin>>a>>b;
- g[a][b]=g[b][a]=true;
- }
-
- int t;
- cin>>t;
- while(t--)
- {
- set<int>s;
- memset(color,0,sizeof color);
- memset(st,false,sizeof st);
- for(int i=1;i<=n;i++)
- {
- cin>>color[i];
- s.insert(color[i]);
- }
- if(s.size()!=k)
- {
- cout<<"No"<
- continue;
- }
- int i;
- for(i=1;i<=n;i++)
- if(!dfs(i)) break; //一旦有重复的点 则跳出
- if(i-1==n) cout<<"Yes"<
//如果上面循环不跳出 说明没有重复的点 - else cout<<"No"<
- }
- return 0;
- }
测试点3:图可能是不联通的 所以不能是dfs(1)
- #include
- using namespace std;
-
- const int N=510;
- int n,m,k;
- bool g[N][N];
- bool st[N];
- int color[N];
-
- bool dfs(int u)
- {
- st[u]=true;
- for(int i=1;i<=n;i++) //遍历该点相关联的节点
- if(g[u][i])
- {
- if(color[u]==color[i]) return false;
- if(!st[i])
- if(!dfs(i)) return false;
- }
- return true;
- }
-
- int main()
- {
- cin>>n>>m>>k;
- for(int i=0;i
- {
- int a,b;
- cin>>a>>b;
- g[a][b]=g[b][a]=true;
- }
-
- int t;
- cin>>t;
- while(t--)
- {
- set<int>s;
- memset(color,0,sizeof color);
- memset(st,false,sizeof st);
- for(int i=1;i<=n;i++)
- {
- cin>>color[i];
- s.insert(color[i]);
- }
- if(s.size()!=k)
- {
- cout<<"No"<
- continue;
- }
- if(dfs(1)) cout<<"Yes"<
- else cout<<"No"<
- }
- return 0;
- }
L1-035 情人节 - 15
- #include
- using namespace std;
- int main()
- {
- int cnt=0;
- string s,x2,x14;
- while(true)
- {
- cin>>s;
- if(s==".") break;
- cnt++;
- if(cnt==2) x2=s;
- if(cnt==14) x14=s;
- }
- if(cnt<2) cout<<"Momo... No one is for you ...";
- else if(cnt<14) cout<
" is the only one for you..."; - else cout<
" and "<" are inviting you to dinner..."; -
- }
L1-039 古风排版 - 20
- #include
- using namespace std;
- int main()
- {
- char ch[101][101];
- int n,t,m,cnt=0;
- cin>>n;
- cin.get();//接收回车 大坑
- string s;
- getline(cin,s);
- int l=s.size();
- for(int i=0;;i++)
- if((l+i)%n==0)
- {
- t=i;
- break;
- }
- l+=t,m=l/n;
- for(int j=m-1;j>=0;j--)
- for(int i=0;i
- {
- if(s[cnt]=='\0') ch[i][j]=' '; //注意最后一行最后是'/0'
- else ch[i][j]=s[cnt++];
- }
- for(int i=0;i
- {
- for(int j=0;j
- {
-
-
相关阅读:
Python连接MariaDB数据库
云原生原则及关键技术
如何做好利益相关方的期望管理?
【S1005基于bs架构的自行车在线租赁管理系统-哔哩哔哩】 https://b23.tv/WJJFGAh
虚拟机执行子系统
OneNote 教程,如何在 OneNote 中使用绘图和批注?
MySQL学习笔记:索引2
【图像拼接】基于matlab最低能量线裁剪图像拼接【含Matlab源码 2127期】
MLOps的演进历程
2023年信息科学与工程学院学生科协第一次前端培训
-
原文地址:https://blog.csdn.net/weixin_61639349/article/details/128084989