Codeforces Educational Codeforces Round 31

来源:互联网 发布:xd网络上是什么意思啊 编辑:程序博客网 时间:2024/06/06 09:24

C. Bertown Subway
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself.

There are n stations in the subway. It was built according to the Bertown Transport Law:

  1. For each station i there exists exactly one train that goes from this station. Its destination station is pi, possibly pi = i
  2. For each station i there exists exactly one station j such that pj = i

The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n).

The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of pi for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes.

The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! 

Input

The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations.

The second line contains n integer numbers p1p2, ..., pn (1 ≤ pi ≤ n) — the current structure of the subway. All these numbers are distinct.

Output

Print one number — the maximum possible value of convenience.

Examples
input
32 1 3
output
9
input
51 5 4 3 2
output
17
Note

In the first example the mayor can change p2 to 3 and p3 to 1, so there will be 9 pairs: (1, 1)(1, 2)(1, 3)(2, 1)(2, 2)(2, 3)(3, 1)(3, 2)(3, 3).

In the second example the mayor can change p2 to 4 and p3 to 5.



题意:有n个站台,第i个站台可以从i坐到pi站,pi是1-n的一个排列,现在只能换一对站台对应的pi,问最多x->y的数量,x->(经过其他任意多站,可以是0站)->y


思路:因为pi是不会相同的,所以从i点出发,最终还会回到i点,路途经过点形成一个连通块,不在该连通块的点不会与之相连,遍历一次就可以找出所有的连通块数量,设每一个连通块中元素个数为k,则该连通块可以形成的(x,y)就有k*k个,当交换一对pi后,可以是两个连通块合二为一,所以要将最大的两个连通块和起来,最后计算便得到答案


#include<iostream>#include<string.h>#include<stdio.h>#include<algorithm>using namespace std;#define LL long longconst int maxn = 100100;LL n,p[maxn],fa[maxn],vis[maxn],cnt,mx1,mx2,ans;void work(){    if(cnt>=mx1){        ans+=mx2*mx2;        mx2 = mx1;        mx1 = cnt;    }    else if(cnt>=mx2){        ans+=mx2*mx2;        mx2 = cnt;    }    else ans+=cnt*cnt;}int main(){    scanf("%lld",&n);    memset(vis,0,sizeof vis);    for(int i=1;i<=n;i++) {scanf("%lld",&p[i]);fa[i]=i;}    for(int i=1;i<=n;i++){        LL now = i;        if(vis[i]) continue;        while(!vis[now]){            vis[now] = 1;            fa[now] = i;            now = p[now];        }    }    sort(fa+1,fa+n+1);    mx1 = mx2 = cnt = ans = 0;    for(int i=1;i<=n;i++){        if(fa[i]!=fa[i-1]) work(),cnt = 0;        cnt++;    }    work();    ans+=(mx1+mx2)*(mx1+mx2);    printf("%lld\n",ans);    return 0;}




原创粉丝点击