hdu 3342 Legal or Not
http://acm.hdu.edu.cn/showproblem.php?pid=3342 裸题。 注意有重边。
1/* ***********************************************
2Author :111qqz
3Created Time :2015年12月17日 星期四 19时29分00秒
4File Name :code/hdoj/3342.cpp
5************************************************ */
6
7#include <cstdio>
8#include <cstring>
9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=1E2+7;
34
35int n,m;
36
37bool v[N][N];
38int in[N];
39
40void topo()
41{
42 queue<int>q;
43
44 for ( int i = 0 ; i < n ; i++)
45 {
46 if (in[i]==0) q.push(i);
47 }
48
49 int cnt = 0 ;
50 while (!q.empty())
51 {
52 cnt++;
53 int u = q.front(); q.pop();
54
55 for ( int i = 0 ; i< n ; i++)
56 {
57 if (!v[u][i]) continue;
58
59 in[i]--;
60 if (in[i]==0)
61 {
62 q.push(i);
63 }
64 }
65 }
66 if (cnt==n)
67 {
68 puts("YES");
69 }
70 else
71 {
72 puts("NO");
73 }
74}
75int main()
76{
77 #ifndef ONLINE_JUDGE
78 freopen("code/in.txt","r",stdin);
79 #endif
80
81 while (~scanf("%d %d",&n,&m)!=EOF)
82 {
83 if (n==0) break;
84 ms(v,false);
85 ms(in,0);
86 while (m--)
87 {
88 int x,y;
89 scanf("%d %d",&x,&y);
90 if (v[x][y]) continue ; //重边?
91 v[x][y] = true;
92 in[y]++;
93 }
94
95 topo();
96 }
97
98 #ifndef ONLINE_JUDGE
99 fclose(stdin);
100 #endif
101 return 0;
102}