-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSeventh.c
More file actions
80 lines (70 loc) · 1.21 KB
/
Copy pathSeventh.c
File metadata and controls
80 lines (70 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <stdio.h>
int arr[100][100] = {0};
int in[100]={0};
int vis[100] = {0};
int n,m;
int topo()
{
int flag = 0;
int count = 0;
for(int i=0; i<n;i++)
{
if(vis[i] == 0 && in[i] == 0)
{
vis[i] = 1;
for(int j=0;j<26;j++)
{
if(arr[i][j] >0)
{
in[j]--;
}
}
topo();
vis[i] = 0;
for(int j=0;j<26;j++)
{
if(arr[i][j] >0)
{
in[j]++;
}
}
flag = 1;
}
if(flag == 1)
{
count++;
}
}
return count;
}
int main()
{
int a,b;
scanf("%d %d",&n,&m);
for(int i=0;i<m;i++)
{
scanf("%d %d",&a,&b);
arr[a][b] = 1;
in[b]++;
}
int flag = 0;
for(int i = 0; i<n; i++)
{
if(in[i] == 0)
flag = 1;
}
int counter;
if(flag == 1)
counter = topo();
else
counter = -1;
printf("%d\n",counter);
}
// 5 7
// 0 1
// 0 2
// 0 3
// 0 4
// 1 3
// 1 4
// 2 4