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;
}


發佈了75 篇原創文章 · 獲贊 9 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章