#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 10;
vector <int> adj[maxn];
bool mark[maxn];
bool mar[maxn];
int par[maxn];
bool cycle = false;
int height = 1e3 + 10;
int mx = 0;
void dfs(int u, int h = 0){
    mx = max(mx, h);
    mark[u] = true;
    for(auto v: adj[u]){
        if(!mark[v]){
            par[v] = u;
            dfs(v, h + 1);
        }
        else{
            if(v != par[u]){
                cycle = true;
            }
        }
    }
}
int main(){
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    int n, m; cin >> n >> m;
    for(int i = 0; i < m; i ++){
        int u, v;
        cin >> u >> v;
        u --; v --;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    bool flag = false;
    for(int i = 0; i < n; i ++){
        if(!mark[i]){
            dfs(i);
        }
        if(adj[i].size() > 2){
            flag = true;
        }
    }
    for(int i = 0; i < n; i ++){
        for(int j = 0; j < n; j ++){
            mark[j] = false;
            par[j] = -1;
        }
        mx = 0;
        dfs(i);
        height = min(height, mx);
    }
    if(cycle){
        cout << "NO";
    }
    else if(flag && (height > 3)){
        cout << "NO";
    }
    else{
        cout << "YES" << endl;
        
    }
    return 0;
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |