শুক্রবার, ৫ জুন, ২০২০

Light oj Problem no:1003 ( Detect Cycle or Not)

Problem Link: http://lightoj.com/volume_showproblem.php?problem=1003



///...................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 100005
int visited[M], cycle = 0;
vector<int>vec[M];
void dfs(int u)
{
    int i;
    visited[u]=1;
    if (cycle==1)
        return;
    for (i=0; i<vec[u].size(); i++)
    {
        int v=vec[u][i];
        if (visited[v]==1)
        {
            cycle=1;
            return;
        }
        else if (visited[v]==0)
        {
            dfs(v);
        }
    }
    visited[u]=2;
}
main()
{
    int ts,cs=1;
    cin>>ts;
    while(ts--)
    {

        int m;
        cin>>m;
        int i,u,v,cnt=1;
        string s,s1;
        map<string,int>mp;
        for (i=0; i<10010; i++)
        {
            vec[i].clear();
            visited[i]=0;
        }
        for(i=1; i<=m; i++)
        {
            cin>>s>>s1;
            if(mp[s]==0)
            {
                mp[s]=cnt;
                cnt++;
            }
            u=mp[s];
            if(mp[s1]==0)
            {
                mp[s1]=cnt;
                cnt++;
            }
            v=mp[s1];
            vec[u].push_back(v);
        }
        cycle=0;
        for(i=1; i<=cnt; i++)
        {
            if(visited[i]==0)
            {
                dfs(i);
            }
        }
        if(cycle==1)
        {
            printf("Case %d: No\n",cs++);
        }
        else
            printf("Case %d: Yes\n",cs++);
    }
}

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

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

Factorization with prime Sieve

vector <int> prime; char sieve[1000009]; int N=1000009; void primeSieve ( ) { sieve[0] = sieve[1] = 1; prime.push_back(2); ...