Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- bool fl=false;
- void dfs(int v, vector <vector <int> > &graph, vector <bool> &visited, vector <int> &value, int prev) {
- visited[v] = true;
- value[v]=prev;
- for (int i = 0; i < (int)graph[v].size(); ++i) {
- if(graph[v][i]){
- if(value[i]!=0 && value[i]%2==value[v]%2){
- fl=true;
- return;
- }
- }
- if (graph[v][i] && !visited[i]) {
- dfs(i, graph, visited, value, prev+1);
- }
- }
- }
- int main() {
- int n, m;
- cin>>n>>m;
- vector <vector<int> > graph(n, vector <int>(n));
- vector <bool> visited(n, 0);
- vector <bool> used(n, 0);
- vector <int> value(n, 0);
- for(int i=0;i<n;i++){
- for(int j=0;j<n;j++){
- if(i==j){
- graph[i][j]=false;
- }
- }
- }
- int a,b;
- for(int i=0;i<m;i++){
- cin>>a>>b;
- graph[a-1][b-1]=true;
- graph[b-1][a-1]=true;
- used[a-1]=true;
- used[b-1]=true;
- }
- while(1){
- int h=0;
- for(int i=0;i<n;i++){
- if(used[i] && !visited[i]){
- h++;
- dfs(i, graph, visited, value, 1);
- if(fl){
- cout<<"NO";
- return 0;
- }
- }
- }
- if(h==0){
- break;
- }
- }
- cout<<"YES";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement