Codeforces Educational Codeforces Round 31 - C - Bertown Subway

21 篇文章 0 订阅
7 篇文章 0 订阅

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
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个站台,第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 long
const 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;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值