F - Experimental data structure of FIG on Three: reachability

Description

In the ancient legend of Warcraft, there are two corps, called a natural disaster, a man named Guards. In the area where they are, there are n Pass, numbered 1 ... n, some of the passes between channels is connected. Wherein Sentinel Pass No. 1, No. n Pass the scourge. One day, the leader of the Lich King Scourge decided to send troops to attack the Sentinel, Scourge of such a large force, and even fill the river across the river. But the Lich King did not want to pay unnecessary costs, he wanted to know under the premise does not build any channel, whether to attack troops can arrive Sentinel Pass and through the relevant channels. Because of the relatively large value of n (n <= 1000), then the Lich King found a good programming = _ = you, you help him solve this problem, otherwise you'll eat become his magic. In order to save himself, and quickly think of ways it.

Input

Input comprising a plurality of sets, each format.

The first line contains two integers n, m (n representing a Pass, between the channels there are m Pass).

The following m lines contains two integers a, b; represents a departure from b has a passage to the pass (note: the channel is unidirectional).
Output

If the Scourge can not build any channel to reach the No. 1 Pass, then the output YES, otherwise output NO.

Sample

Input

2 1
1 2
2 1
2 1
Output

NO
YES

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
#include<queue>
using namespace std;
int Map[1100][1100],vis[1100];
int n,m;
int flag;
void dfs(int t)
{
    vis[t] = 1;
    for(int i=n; i>=1; i--)
    {
        if(!vis[i]&&Map[t][i]==1)
        {
            if(i == 1)
            {
                flag = 1;
                return;
            }
            dfs(i);
        }

    }
}
int main()
{
    int t,p;
    int u,v;
    while(~scanf("%d %d",&n,&m))
    {
        memset(vis,0,sizeof(vis));
        memset(Map,0,sizeof(Map));
        flag = 0;
        for(int i=0; i<m; i++)
        {
            scanf("%d %d",&u,&v);
            Map[u][v] = 1;
        }
        dfs(n);
        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/104883452