This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define int long long
int bfs(int n, vector<vector<int> >& adi){
vector<bool> vis(n);
deque<int> q;
q.push_back(0);
int last=0;
vis[0]=true;
while(!q.empty()){
int v=last=q.front();
q.pop_front();
for(auto u: adi[v]){
if(!vis[u]){
vis[u]=true;
q.push_back(u);
}
}
}
return last;
}
int dfs(int v, int hight, vector<bool>& vis, vector<vector<int> >& adi, bool& pos){
if(vis[v]){
return 0;
}
vis[v]=true;
int depth=1;
int further=0;
for(auto u: adi[v]){
int subtree=dfs(u, hight+1, vis, adi, pos);
depth=max(depth, subtree+1);
if(subtree>2){
further++;
}
}
pos=pos && (further<=1);
return depth;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<vector<int> > adi(n);
for(int i=0; i<m; i++){
int a, b;
cin >> a >> b;
adi[a-1].push_back(b-1);
adi[b-1].push_back(a-1);
}
bool pos=(m==n-1);
int root=bfs(n, adi);
vector<bool> vis(n, false);
dfs(root, 0, vis, adi, pos);
if(pos){
cout << "YES\n";
cout << 1 << "\n";
cout << 1 << "\n";
}
else{
cout << "NO\n";
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |