সোমবার, ৮ অক্টোবর, ২০১৮

Is it a tree or not? By using DFS

Problem Link: https://www.spoj.com/problems/PT07Y/


///...................SUBHASHIS MOLLICK...................///
///.....DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING....///
///.............ISLAMIC UNIVERSITY,BANGLADESH.............///
///....................SESSION-(14-15)....................///
#include<bits/stdc++.h>
using namespace std;
#define sf(a) scanf("%lld",&a)
#define sf2(a,b) scanf("lld",&a,&b)
#define sf3(a,b,c) scanf("lld%lld",&a,&b,&c)
#define pf(a) printf("%lld",a)
#define pf2(a,b) printf("lld",a,b)
#define pf3(a,b,c) printf("lld%lld",a,b,c)
#define nl printf("\n")
#define ll long long
#define pb push_back
#define MPI map<int,int>mp
#define fr(i,n) for(i=0;i<n;i++)
#define fr1(i,n) for(i=1;i<=n;i++)
#define frl(i,a,b) for(i=a;i<=b;i++)
//freopen("Input.txt","r",stdin);
//freopen("Output.txt","w",stdout);
#define M 10005
long vis[M],flag=0;
vector<long>vec[M];
void dfs(long u)
{
    long i,cnt=0,v;
    for(i=0; i<vec[u].size(); i++)
    {
        v=vec[u][i];
        if(vis[v]==1)
        {
            cnt++;
        }
    }
    if(cnt>1)
    {
        flag=1;
    }
    for(i=0; i<vec[u].size(); i++)
    {
        v=vec[u][i];
        if(vis[v]==0)
        {
            vis[v]=1;
            dfs(v);
        }
    }
}
main()
{
    long n,m,q;
    cin>>n>>m;
    long i,u,v;
    for(i=1; i<=m; i++)
    {
        cin>>u>>v;
        vec[u].push_back(v);
        vec[v].push_back(u);
    }
    flag=0;
    if(n!=(m+1))
        flag=1;
    vis[1]=1;
    dfs(1);
    for(i=1; i<=n; i++)
    {
        if(vis[i]==0)
        {
            flag=1;
        }
    }
    if(flag==1)
        cout<<"NO"<<endl;
    else
        cout<<"YES"<<endl;
}

কোন মন্তব্য নেই:

একটি মন্তব্য পোস্ট করুন

Factory Pattern

Factory Method  is a creational design pattern that provides an interface for creating objects in a superclass but allows subclasses to alte...