You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.7 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
const int N = 10010, M = 30010;
int din[N]; // 记录每个节点入度
int dist[N]; // 记录每个节点距离起点的最小值
int n, m;
// 链式前向星
int e[M], h[N], idx, w[M], ne[M];
void add(int a, int b, int c = 0) {
e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
}
vector<int> path;
bool topsort() {
queue<int> q;
for (int i = 1; i <= n; i++)
if (!din[i]) q.push(i);
while (q.size()) {
int u = q.front();
q.pop();
path.push_back(u); // 记录拓扑序路径
for (int i = h[u]; ~i; i = ne[i]) {
int v = e[i];
din[v]--;
if (din[v] == 0) q.push(v);
}
}
return path.size() == n;
}
int main() {
memset(h, -1, sizeof h);
scanf("%d %d", &n, &m);
while (m--) {
int a, b;
scanf("%d %d", &a, &b);
add(b, a, 1); // a比b高意味着 b->边权为1的边->a
din[a]++;
}
if (!topsort()) {
puts("Poor Xed");
return 0;
}
// 每个员工奖金最少是100元
for (int i = 1; i <= n; i++) dist[i] = 100;
// 根据拓扑序求最长路
for (auto u : path) { // 枚举拓扑序路径
// 枚举节点u所有邻接的节点找出最大的转移,可以看一下题解中的图,就明白了
for (int i = h[u]; ~i; i = ne[i]) {
int v = e[i];
dist[v] = max(dist[v], dist[u] + w[i]);
}
}
int res = 0;
for (int i = 1; i <= n; i++) res += dist[i]; // 每个点的累加和,就是总的,最小的, 奖金数
printf("%d\n", res);
return 0;
}