n个人,m种关系,输入a,b,表示a比b的工资高,每个人的基本工资都为888,至少需要准备多少钱发工资。
// #pragma GCC optimize(2)
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <stack>
#include <map>
#include <sstream>
#include <cstring>
#include <set>
#include <cctype>
#include <bitset>
#define IO \
ios::sync_with_stdio(false); \
// cout.tie(0);
using namespace std;
// int dis[8][2] = {0, 1, 1, 0, 0, -1, -1, 0, 1, -1, 1, 1, -1, 1, -1, -1};
typedef unsigned long long ULL;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 1e5 + 10;
const int maxm = 2e5 + 10;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const double pi = acos(-1);
struct Edge
{
int next, to;
} e[maxm];
int n, m, k;
int in[maxn], dep[maxn];
int head[maxn];
void add(int u, int v)
{
e[k].next = head[u];
e[k].to = v;
head[u] = k++;
}
LL topo()
{
queue<int> q;
LL ans = 0;
int cnt = 0;
for (int i = 1; i <= n; i++)
{
if (in[i] == 0)
{
q.push(i);
cnt++;
dep[i] = 0;
}
}
while (!q.empty())
{
int u = q.front();
q.pop();
ans += LL(888 + dep[u]);
for (int i = head[u]; i != -1; i = e[i].next)
{
int v = e[i].to;
in[v]--;
if (!in[v])
{
cnt++;
dep[v] = dep[u] + 1;
q.push(v);
}
}
}
if (cnt == n)
return ans;
else
return -1;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif
IO;
int x, y;
while (cin >> n >> m)
{
k = 0;
for (int i = 0; i <= n; i++)
{
head[i] = -1, in[i] = 0, dep[i] = 0;
}
for (int i = 0; i < m; i++)
{
cin >> y >> x;
in[y]++;
add(x, y);
}
cout << topo() << "\n";
}
return 0;
}