A - of the experimental data on the structure of FIG ten: determine whether a given sequence of FIG topology if there are legitimate

Description

Given a directed graph, which determines whether there exists a legal topology sequence to FIG.
Input

Input comprising a plurality of sets, each format.

The first line contains two integers n, m, representing the number of vertices and edges with a directed graph. (N <= 10)

M behind the two lines each integers ab, from a to b represents a directed edge there.

Output

If the presence of a given sequence to FIG legal topology, then the YES output; otherwise, output NO.

Sample

Input

1 0
2 2
1 2
2 1
Output

YES
NO

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
int in[110];
int Map[110][110];
int main()
{
    int n,m;
    int u,v;
    int flag;
    while(~scanf("%d %d",&n,&m))
    {
        memset(in,0,sizeof(in));
        memset(Map,0,sizeof(Map));
        for(int i=0;i<m;i++)
        {
            scanf("%d %d",&u,&v);
            Map[u][v] = 1;
            in[v]++;
        }
        for(int i=1;i<=n;i++)
        {
           flag = 0;
           for(int j=1;j<=n;j++)
           {
               if(in[j] == 0)
               {
                   in[j] = -1;
                   flag = 1;
                   for(int k=1;k<=n;k++)
                   {
                       if(Map[j][k] == 1)
                            in[k]--;
                   }
                   break;
               }
           }
           if(flag == 0)
            break;

        }
        if(flag)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}


Thinking II: find the degree of point 0 on a vis flag bit, then the degree connected to the point minus a (side removed), and finally to see if the n points are labeled know is not topology sequence of

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
int in[110];
int vis[110];
int Map[110][110];
int main()
{
    int n,m;
    int u,v;
    int flag;
    while(~scanf("%d %d",&n,&m))
    {
        memset(in,0,sizeof(in));
        memset(Map,0,sizeof(Map));
        memset(vis,0,sizeof(vis));
        for(int i=0;i<m;i++)
        {
            scanf("%d %d",&u,&v);
            Map[u][v] = 1;
            in[v]++;
        }
        for(int i=1;i<=n;i++)
        {
            if(in[i]==0)
            {
                vis[i]=1;
                in[i] = -1;
                for(int j=1;j<=n;j++)
                {
                    if(Map[i][j] == 1)
                    {
                        in[j]--;
                        Map[i][j] = 0;
                    }
                }
            }
        }
        flag = 1;
        for(int i=1;i<=n;i++)
        {
            if(vis[i] == 0)
            {
                flag = 0;
                break;
            }
        }
        if(flag)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}


Published 177 original articles · won praise 7 · views 30000 +

Guess you like

Origin blog.csdn.net/Fusheng_Yizhao/article/details/104919035