Header Ad

HackerEarth Monk at the Graph Factory problem solution

In this HackerEarth Monk at the Graph Factory problem solution, Our Code Monk recently learnt about Graphs and is very excited!

He went over to the Graph-making factory to watch some freshly prepared graphs. Incidentally, one of the workers at the factory was ill today, so Monk decided to step in and do her job.

The Monk's Job is to Identify whether the incoming graph is a tree or not. He is given N, the number of vertices in the graph and the degree of each vertex.

Find if the graph is a tree or not.


HackerEarth Monk at the Graph Factory problem solution


HackerEarth Monk at the Graph Factory problem solution.

#include<bits/stdc++.h>

using namespace std;

#define vi vector < int >
#define pii pair < int , int >
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define foreach(it,v) for( __typeof((v).begin())it = (v).begin() ; it != (v).end() ; it++ )
#define ll long long
#define llu unsigned long long
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define dbg(x) { cout<< #x << ": " << (x) << endl; }
#define dbg2(x,y) { cout<< #x << ": " << (x) << " , " << #y << ": " << (y) << endl; }
#define all(x) x.begin(),x.end()
#define mset(x,v) memset(x, v, sizeof(x))
#define sz(x) (int)x.size()

int d[102];

int main()
{
int n,i;
scanf("%d",&n);
assert(1 <= n && n <= 100);
int sum = 0;
for(i=0;i<n;i++)
{
scanf("%d",&d[i]);
assert(1 <= d[i] && d[i] <= 1000);
sum += d[i];
}
if(sum == 2*(n-1))
{
printf("Yes");
}
else
{
printf("No");
}
return 0;
}

Second solution

n = int(raw_input())
l = map(int, raw_input().split())
ans = sum(l)
if ans == 2*(n-1):
print "Yes"
else:
print "No:

Post a Comment

0 Comments