hdu 3342 Legal or Not
http://acm.hdu.edu.cn/showproblem.php?pid=3342 裸题。 注意有重边。
/* ***********************************************
Author :111qqz
Created Time :2015年12月17日 星期四 19时29分00秒
File Name :code/hdoj/3342.cpp
************************************************ */
1#include <cstdio>
2#include <cstring>
3#include <iostream>
4#include <algorithm>
5#include <vector>
6#include <queue>
7#include <set>
8#include <map>
9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
1using namespace std;
2const double eps = 1E-8;
3const int dx4[4]={1,0,0,-1};
4const int dy4[4]={0,-1,1,0};
5const int inf = 0x3f3f3f3f;
6const int N=1E2+7;
int n,m;
bool v[N][N];
int in[N];
1void topo()
2{
3 queue<int>q;
1 for ( int i = 0 ; i < n ; i++)
2 {
3 if (in[i]==0) q.push(i);
4 }
1 int cnt = 0 ;
2 while (!q.empty())
3 {
4 cnt++;
5 int u = q.front(); q.pop();
1 for ( int i = 0 ; i< n ; i++)
2 {
3 if (!v[u][i]) continue;
1 in[i]--;
2 if (in[i]==0)
3 {
4 q.push(i);
5 }
6 }
7 }
8 if (cnt==n)
9 {
10 puts("YES");
11 }
12 else
13 {
14 puts("NO");
15 }
16}
17int main()
18{
19 #ifndef ONLINE_JUDGE
20 freopen("code/in.txt","r",stdin);
21 #endif
1 while (~scanf("%d %d",&n,&m)!=EOF)
2 {
3 if (n==0) break;
4 ms(v,false);
5 ms(in,0);
6 while (m--)
7 {
8 int x,y;
9 scanf("%d %d",&x,&y);
10 if (v[x][y]) continue ; //重边?
11 v[x][y] = true;
12 in[y]++;
13 }
topo();
}
1 #ifndef ONLINE_JUDGE
2 fclose(stdin);
3 #endif
4 return 0;
5}