bzoj1589

来源:互联网 发布:淘宝帐号永久冻结解封 编辑:程序博客网 时间:2024/06/03 17:28

treedp
乱搞一发

#include<bits/stdc++.h>using namespace std;#define FOR(i,s,t) for(int i=(s);i<=(t);i++)inline int read(void){    int x = 0, c, f = 1;    do{c=getchar();if(c=='-')f=-1;}while(c<'0'||c>'9');    do{x=x*10+c-'0';c=getchar();}while(c>='0'&&c<='9');    return x * f;}const int N = 101000;int to[N], f[N], n, Index;int vis[N], ind[N], vt[N];void DP(int x) {    f[x] = 1, vt[x] = 1;    if (!ind[to[x]]) DP(to[x]);    f[x] += f[to[x]];} int sta[N], stop;void Sp(int x) {    stop = 0;    int tot = 0;    while (!vis[x]) {        vis[x] = 1, tot++;        sta[++stop] = x, x = to[x];    }    while (stop) {        f[sta[stop]] = tot;        stop--;    }}queue<int> Q;int main() {    n = read();    for (int i = 1; i <= n; i++)         ind[to[i] = read()]++;    for (int i = 1; i <= n; i++)         if (!ind[i]) Q.push(i);    while (!Q.empty()) {        int x = Q.front(); Q.pop();        vis[x] = 1;        if (!(--ind[to[x]])) Q.push(to[x]);    }    for (int i = 1; i <= n; i++)         if (!vis[i]) Sp(i);    for (int i = 1; i <= n; i++)         if (!ind[i] && !vt[i]) DP(i);    for (int i = 1; i <= n; i++)        printf("%d\n",f[i]);}
原创粉丝点击