Codeforces-Bertown Subway(思维)

来源:互联网 发布:quickconnect 域名 编辑:程序博客网 时间:2024/04/28 06:31
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 theBertown Transport Law:

  1. For each station i there exists exactly one train that goes from this station. Its destination station ispi, possiblypi = i;
  2. For each station i there exists exactly one stationj such that pj = i.

The President will consider the convenience of subway after visiting it. Theconvenience is the number of ordered pairs(x, y) such that person can start at stationx and, after taking some subway trains (possibly zero), arrive at stationy (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 fornot more than two subway stations. Of course, breaking theBertown Transport Law is really bad, so the subway must be built according to theLaw 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 possibleconvenience 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 numbersp1, p2, ..., 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 to3 and p3 to1, 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 to4 and p3 to5.


思路:求出每个连通块的大小,把最大的连个合并,剩下的不变,ans等于每个连通块的平方和,暴力写就行,注意这题要用long long

AC代码:

#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<queue>using namespace std;int n,x;int arr[100005],cnt[100005];bool vis[100005];int main(){    while(~scanf("%d",&n))    {        memset(vis,false,sizeof(vis));        memset(cnt,0,sizeof(cnt));//存每个连通快的大小        for(int i=1;i<=n;i++)        {            scanf("%d",&x);            arr[i]=x;        }        int tmp,c;        for(int i=1;i<=n;i++)        {            if(vis[i]) continue;            tmp=arr[i];c=1;vis[i]=true;            while(!vis[tmp])            {                c++;                vis[tmp]=true;                tmp=arr[tmp];            }            cnt[i]=c;        }        sort(cnt+1,cnt+n+1);        long long int ans=0;        if(n>1)ans=(long long)(cnt[n]+cnt[n-1])*(long long)(cnt[n]+cnt[n-1]);//合并最大的两个        else ans=1;        for(int i=1;i<=n-2;i++)        {            ans+=(long long)cnt[i]*(long long)cnt[i];        }        printf("%lld\n",ans);    }    return 0;}


阅读全文
0 0
原创粉丝点击