|
沙发
楼主 |
发表于 2018-6-7 13:10:27
|
只看该作者
- #include <cstdio>
- #include <algorithm>
- #include <queue>
- using namespace std;
- const int N = 100010;
- int topo[N];
- struct Edge {
- int v, nex;
- }edge[N << 1]; int top;
- struct Poi {
- int e, in, ans;
- }poi[N];
- inline void add(int x, int y) {
- top++;
- edge[top].v = y;
- edge[top].nex = poi[x].e;
- poi[x].e = top;
- return;
- }
- void toposort(int n) {
- queue<int> Q;
- for(int i = 1; i <= n; i++) {
- if(!poi[i].in) {
- Q.push(i);
- }
- }
- int t = 0;
- while(!Q.empty()) {
- int op = Q.front();
- Q.pop();
- topo[++t] = op;
- for(int i = poi[op].e; i; i = edge[i].nex) {
- int ed = edge[i].v;
- poi[ed].in--;
- if(!poi[ed].in) {
- Q.push(ed);
- }
- }
- }
- return;
- }
- int main() {
- int m, n;
- scanf("%d%d", &n, &m);
- for(int i = 1, x, y; i <= m; i++) {
- scanf("%d%d", &x, &y);
- poi[x].in++;
- add(y, x);
- }
- toposort(n);
- for(int i = n; i >= 1; i--) {
- int op = topo[i];
- for(int i = poi[op].e; i; i = edge[i].nex) {
- int ed = edge[i].v;
- poi[op].ans = max(poi[op].ans, poi[ed].ans);
- }
- poi[op].ans++;
- }
- for(int i = 1; i <= n; i++) {
- printf("%d\n", poi[i].ans);
- }
- return 0;
- }
复制代码 |
|