AcWing 1192. 奖金
原题链接
简单
作者:
Shadow_7
,
2020-07-24 18:16:43
,
所有人可见
,
阅读 478
#include <iostream>
#include <cstring>
using namespace std;
const int N = 2e4 + 10;
int h[N], e[N], ne[N], idx;
int d[N];
int n, m;
int q[N], hh = 0, tt = -1;
int w[N], res;
void add(int a, int b){
e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}
bool topsort(){
for(int i = 1; i <= n; i ++)
if(!d[i]) q[++ tt] = i, w[i] = 100;
while(hh <= tt){
int t = q[hh ++];
for(int i = h[t]; i != -1; i = ne[i]){
int j = e[i];
d[j] --;
w[j] = w[t] + 1;
if(!d[j]) q[++ tt] = j;
}
}
return tt == n - 1;
}
int main(){
memset(h, -1, sizeof h);
cin >> n >> m;
for(int i = 0; i < m; i ++){
int a, b;
cin >> a >> b;
add(b, a);
d[a] ++;
}
if(topsort()){
for(int i = 1; i <= n; i ++) res += w[i];
cout << res;
}
else cout << "Poor Xed";
return 0;
}