Codeforces-Bertown Subway(思维)

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 p1, 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
3
2 1 3
Output
9
Input
5
1 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.


思路:求出每个连通块的大小,把最大的连个合并,剩下的不变,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;
}


发布了76 篇原创文章 · 获赞 9 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_37171272/article/details/78370571
今日推荐