当前位置:网站首页>242. Bipartite graph determination
242. Bipartite graph determination
2022-07-07 07:35:00 【Recursions】
Given a with n A graph of vertices .
To dye every vertex on a graph , And make the adjacent vertices have different colors .
Ask if you can dye with up to two colors ? The title guarantees no double edges and self rings .
Input
- The first line is an integer n(1≤n≤1000), Represents the number of vertices
- Next, there are two integers in each line x and y, It means a vertex x and y There is a side between ,0≤x,y<n,x≠y
- With
EOF(end of file)
As the end of edge data
Output
Yes
orNo
Indicates whether two colors can be used for dyeing
Examples 1
Input
3 0 1 1 2 2 0
Output
No
#include<bits/stdc++.h>
using namespace std;
int n;
int color[1001];
vector<int>e[1001];
bool dfs(int v,int c)
{
color[v]=c;
for(int i=0;i<e[i].size();i++)
{
if(color[e[v][i]]==c)
return false;
if(color[e[v][i]]==0&&!dfs(e[v][i],-c))
return false;
}
return true;
}
int main()
{
while(cin>>n)
{
for(int i=0;i<n;i++)
{
int v1,v2;
cin>>v1>>v2;
e[v1].push_back(v2);
e[v2].push_back(v1);
}
for(int i=0;i<n;i++){
if(color[i]==0){
if(!dfs(i,1)){
cout<<"No"<<endl;
return 0;
}
}
}
cout<<"Yes"<<endl;
}
return 0;
}
边栏推荐
- 計算機服務中缺失MySQL服務
- Outsourcing for four years, abandoned
- 电商常规问题part1
- 1090: integer power (multi instance test)
- 点亮显示屏的几个重要步骤
- L'étape avancée du pointeur de langage C (haut de gamme) pour l'enroulement des cocons
- 外包干了三年,废了...
- Le Service MySQL manque dans le service informatique
- 海思芯片(hi3516dv300)uboot镜像生成过程详解
- 【leetcode】1020. Number of enclaves
猜你喜欢
MobaXterm
L'externalisation a duré trois ans.
Redis data migration
KBU1510-ASEMI电源专用15A整流桥KBU1510
Convolutional neural network -- understanding of pooling
Mutual conversion between InputStream, int, shot, long and byte arrays
Robot technology innovation and practice old version outline
Outsourcing for four years, abandoned
After 95, the CV engineer posted the payroll and made up this. It's really fragrant
Simple example of ros2 planning system plansys2
随机推荐
弹性布局(二)
面试官:你都了解哪些开发模型?
Model application of time series analysis - stock price prediction
Outsourcing for four years, abandoned
Tumor immunotherapy research prosci Lag3 antibody solution
Make a bat file for cleaning system garbage
Convolutional neural network -- understanding of pooling
JS small exercise
抽絲剝繭C語言(高階)指針的進階
Initial experience of teambiion network disk (Alibaba cloud network disk)
About binary cannot express decimals accurately
Leetcode-226. Invert Binary Tree
虚拟机的作用
Advanced level of C language (high level) pointer
nacos
电商常规问题part1
抽絲剝繭C語言(高階)數據的儲存+練習
URP - shaders and materials - light shader lit
idea添加类注释模板和方法模板
JS small exercise ---- time sharing reminder and greeting, form password display hidden effect, text box focus event, closing advertisement