当前位置:网站首页>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
YesorNoIndicates 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;
}边栏推荐
- Blue Bridge Cup Birthday candles (violence)
- How can a 35 year old programmer build a technological moat?
- 电商常规问题part1
- 虚拟机的作用
- After 95, Alibaba P7 published the payroll: it's really fragrant to make up this
- 二、并发、测试笔记 青训营笔记
- Torefs API and toref API
- About binary cannot express decimals accurately
- I failed in the postgraduate entrance examination and couldn't get into the big factory. I feel like it's over
- Advanced level of C language (high level) pointer
猜你喜欢

My ideal software tester development status

"Xiaodeng in operation and maintenance" meets the compliance requirements of gdpr

Wechat applet full stack development practice Chapter 3 Introduction and use of APIs commonly used in wechat applet development -- 3.10 tabbar component (I) how to open and use the default tabbar comp
![[Linux] process control and parent-child processes](/img/4c/89f87ee97f0f8e9033b9f0ef46a80d.png)
[Linux] process control and parent-child processes

How can a 35 year old programmer build a technological moat?

Leetcode-543. Diameter of Binary Tree

Bi she - college student part-time platform system based on SSM

抽丝剥茧C语言(高阶)指针进阶练习

Asynchronous components and suspend (in real development)

MobaXterm
随机推荐
MySQL service is missing from computer service
Stockage et pratique des données en langage C (haut niveau)
$refs: get the element object or sub component instance in the component:
L'étape avancée du pointeur de langage C (haut de gamme) pour l'enroulement des cocons
I failed in the postgraduate entrance examination and couldn't get into the big factory. I feel like it's over
Redis data migration
Bi she - college student part-time platform system based on SSM
JS small exercise
弹性布局(一)
Tencent's one-day life
Asynchronous components and suspend (in real development)
Initial experience of teambiion network disk (Alibaba cloud network disk)
考研失败,卷不进大厂,感觉没戏了
ROS2规划系统plansys2简单的例子
按键精灵脚本学习-关于天猫抢红包
[semantic segmentation] - multi-scale attention
Jenkins远程构建项目超时的问题
1089: highest order of factorial
About binary cannot express decimals accurately
1140_ SiCp learning notes_ Use Newton's method to solve the square root