1280. Topological Sorting

http://acm.timus.ru/problem.aspx?space=1&num=1280

水题

代码:

#include<iostream>

#include<cstdio>

#include<cstring>

#include<string>

#include<vector>

#include<queue>

#include<map>

#include<set>

#include<stack>

#include<algorithm>

#include<cmath>



using namespace std;

//#pragma comment(linker,"/STACK:1000000000,1000000000")



#define LL long long



const int INF=0x3f3f3f3f;

const int N=1005;

const int M=100005;

int head[N],I;

struct node

{

    int j,next;

}side[M];

int a[N];

int num[N];

void Add(int i,int j)

{

    side[I].j=j;

    side[I].next=head[i];

    head[i]=I++;

}

int main()

{

    int n,m;

    while(scanf("%d %d",&n,&m)!=EOF)

    {

        memset(head,-1,sizeof(head));

        I=0;

        memset(num,0,sizeof(num));

        while(m--)

        {

            int i,j;

            scanf("%d %d",&i,&j);

            ++num[j];

            Add(i,j);

        }

        for(int i=0;i<n;++i)

        scanf("%d",&a[i]);

        int l;

        for(l=0;l<n;++l)

        {

            if(num[a[l]])

            break;

            for(int t=head[a[l]];t!=-1;t=side[t].next)

            --num[side[t].j];

        }

        if(l<n)

        printf("NO\n");

        else

        printf("YES\n");

    }

    return 0;

}

 

你可能感兴趣的:(sort)