asked 2 years ago
Reputation: 1
I want to know the conditions for a graph to become a tree.
Facts:
Condition for a graph to become a tree
Total edges = n-1
Sum of degrees of all nodes = 2(n-1)
Code
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,sum,num,i;
//Number of vertices
cin>>n;
sum=0;
for(i=0;i<n;++i){
// Degrees for all the vertices
cin>>num;
sum += num;
}
if(sum==2*(n-1)){
cout<<"Yes"<<endl;
}else{
cout<<"No"<<endl;
}
return 0;
}