标签:
3 2 3 1 2 1 3 3 3 2 2 1 1 3
YES NO
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
#define lson rt<<1,l,MID
#define rson rt<<1|1,MID+1,r
//#define lson root<<1
//#define rson root<<1|1
#define MID ((l+r)>>1)
typedef long long ll;
typedef pair<int,int> P;
const int maxn=10005;
const int base=1000;
const int inf=999999;
const double eps=1e-5;
int d[maxn];
vector<int> G[maxn];
bool tupo_sort(int n)
{
stack<int> s;
for(int i=1;i<=n;i++)
if(d[i]==0)s.push(i);
int cnt=0;
while(!s.empty())
{
int m=s.top();//printf("%d ",m);
s.pop();
cnt++;
for(int i=0;i<G[m].size();i++)
{
d[G[m][i]]--;
if(d[G[m][i]]==0)s.push(G[m][i]);
}
}
if(cnt<n)return false;
return true;
}
int main()
{
int n,m,i,j,k,t;
while(~scanf("%d%d",&n,&m))
{
memset(d,0,sizeof(d));
for(i=0;i<=n;i++)//每次使用注意 清空 这里忘记了 错误了很多次
G[i].clear();
while(m--)
{
int s,e;
scanf("%d%d",&s,&e);
G[e].push_back(s);
d[s]++;
}
if(tupo_sort(n))
puts("YES");
else
puts("NO");
}
return 0;
}
拓扑排序 杭电5154 Harry and Magical Computer
标签:
原文地址:http://blog.csdn.net/u013167299/article/details/42389753