-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathfirma.cpp
72 lines (66 loc) · 1.38 KB
/
firma.cpp
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
# include <cstdio>
# include <algorithm>
# include <queue>
# define NR 100005
using namespace std;
queue <int> q;
int i,j,n,m,s,tip,maxx,x,y,S;
int grad[NR], T[NR], nr[NR], cost[NR];
void sortaret ()
{
int i,j,k;
while (! q.empty())
{
k=q.front(); q.pop();
maxx=max(maxx, nr[k]);
nr[T[k]]+=nr[k]+1;
--grad[T[k]];
if (grad[T[k]]==0) q.push(T[k]);
}
}
void sortaret2 ()
{
int i,j,k;
while (! q.empty())
{
k=q.front(); q.pop();
S+=cost[k];
cost[T[k]]=max(cost[T[k]], cost[k]+1);
--grad[T[k]];
if (grad[T[k]]==0) q.push(T[k]);
}
}
int main ()
{
freopen ("firma.in", "r", stdin);
freopen ("firma.out", "w", stdout);
scanf ("%d", &tip);
scanf ("%d%d%d", &n, &m, &s);
for (i=1; i<=m; ++i)
{
scanf ("%d%d", &x, &y);
T[y]=x; ++grad[x];
}
if (tip==1) //nr de sefi
{
int nr=0;
for (i=1; i<=n; ++i)
if (! T[i]) ++nr;
printf ("%d\n", nr);
}
else if (tip==2) //departamentul maxim
{
for (i=1; i<=n; ++i)
if (grad[i]==0) q.push(i);
sortaret ();
printf ("%d\n", maxx+1);
}
else if (tip==3) //suma minima
{
for (i=1; i<=n; ++i)
if (grad[i]==0) {cost[i]=s; q.push(i);}
sortaret2();
printf ("%d\n", S);
}
return 0;
}