题目描述
blablabla
样例
blablabla
算法1
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
const int N = 10010, M = 20010;
int n, m;
int h[N], e[M], ne[M], idx;
int din[N];
int dist[N];
vector<int> seq;
void topsort()
{
queue<int> q;
for (int i = 1; i <= n; i ++)
if (!din[i])
q.push(i);
while(q.size())
{
auto t = q.front();
q.pop();
seq.push_back(t);
for (int i = h[t]; ~i; i = ne[i])
{
int j = e[i];
if (-- din[j] == 0)
q.push(j);
}
}
}
void add(int a, int b)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}
int main()
{
cin >> n >> m;
memset(h, -1, sizeof h);
while(m --)
{
int a, b;
cin >> a >> b;
add(b, a);
din[a] ++;
}
topsort();
if (seq.size() < n) cout << "Poor Xed";
else
{
for (int i = 1; i <= n; i ++) dist[i] = 100;
for (auto i : seq)
for (int j = h[i]; ~j; j = ne[j])
{
int k = e[j];
dist[k] = max(dist[k], dist[i] + 1);
}
int res = 0;
for (int i = 1; i <= n; i ++) res += dist[i];
cout << res;
}
return 0;
}
算法2
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
blablabla