Codeforces Round 31 C. Bertown Subway(并查集)

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.

题意:有n个火车站,每个站有且只有一趟火车驶出(可以驶向自己),每个站有且只有一趟火车驶入。如果火车可以从一个城市驶到一个城市,那么愉快值加一(自己驶向自己也算),现在你可以改变两个城市的的火车的目的地,问怎样才能使得愉快值最大(输入不保证联通)输入n个数第i个数表示城市i的火车驶向ai。

思路:根据题意,这些路线必定是形成一个或者多个环(可能有自环)。现在,假如有一个点数为3的环,那么,我们从一个城市到一个城市的可能有 (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). 即n×n种。愉快之为n。改动两条路线,那么就可以使得两个环连成一个环,假设环1有a个点,环二有b个点,原来的愉快值为 a×a+b×b,连成一个环后的愉快值问(a+b)×(a+b),增加了 2×a×b,也就是说,a和b越大,愉快值增加越多。那么我们就可以用并查集来做了,最后加上其他环的愉快值就好了

#include "iostream"
#include "algorithm"
using namespace std;
const int Max=1e5+10;
int par[Max],rk[Max],n,m;
long long vis[Max];
void init()
{
    for(int i=1;i<=n;i++){
        par[i]=i;
        rk[i]=1;
    }
}
int Find(int x)
{
    if(x==par[x]) return x;
    return par[x]=Find(par[x]);
}
void unite(int x,int y)
{
    x=Find(x);
    y=Find(y);
    if(x==y) return;
    if(rk[x]<rk[y]) par[x]=y;
    else{
        par[y]=x;
        if(rk[x]==rk[y]) rk[x]++;
    }
}
bool same(int x,int y)
{
    return Find(x)==Find(y);
}
bool cmp(int a,int b)
{
    return a>b;
}
int main()
{
    ios::sync_with_stdio(false);
    int x;
    cin>>n;
    init();
    for(int i=1;i<=n;i++){
        cin>>x;
        if(!same(i,x))
            unite(i,x);
    }
    for(int i=1;i<=n;i++) vis[Find(i)]++;
    sort(vis,vis+Max,cmp);
    long long sum;
    sum=vis[0]+vis[1];
    long long ans=sum*sum;
    for(int i=2;i<Max;i++){
        if(vis[i]==0) break;
        ans+=vis[i]*vis[i];
    }
    cout<<ans<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41874469/article/details/81100248
今日推荐