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个车站,每个车站对应另一个车站(可以对应自己),题目规定所有车站都能够被到达。所以,这个图应该是由几个环组成的。然后题目要你求出在所有车站中,能从车站

a到达车站b的有序对(a,b)的个数(可以自己到自己)的最大值。有规律易得,在一个元素数目为n的环中,这样的有序对有n*n个。然后题目表示,允许你在任然全是环的情况下,修改

两个车站对应的下一个车站。容易看出,把最大的两个环融合在一起时,有序对是最多的。可以由完全平方公式来证明。

(a+b)>=a2+b2

所以,先找出图中所有的环,然后把环的大小存入容器。排序。把最大的两个相加求有序对数目,然后把剩下的环的有序对数目依次加和。问题就解决了。
#include<iostream>
#include<vector>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
int main()
{
vector<int> v;
long long num;
int mc[100010];
bool fw[100010];
long long n,x;
cin>>n;
for(int i=1;i<=n;i++) cin>>mc[i];
v.clear();
memset(fw,false,sizeof(fw));
for(int i=1;i<=n;i++)
{
if(fw[i]==true) continue;
int sz=1;
fw[i]=true;
int x=mc[i];
while(x!=i)
{
fw[x]=true;
x=mc[x];
sz++;
}
v.push_back(sz);
}
long long ans=0;
sort(v.begin(),v.end());
if(v.size()>=2)
{
ans=v[v.size()-1]+v[v.size()-2];
ans*=ans;
for(int j=0;j<v.size()-2;j++)
{
ans+=v[j]*v[j];
}
}
else ans=n*n;
cout<<ans<<endl;
}

  

Codeforces 884C.Bertown Subway ----判环,思路的更多相关文章

  1. Codeforces Round #460 (Div. 2): D. Substring(DAG+DP+判环)

    D. Substring time limit per test 3 seconds memory limit per test 256 megabytes input standard input ...

  2. CodeForces 937D 936B Sleepy Game 有向图判环,拆点,DFS

    题意: 一种游戏,2个人轮流控制棋子在一块有向图上移动,每次移动一条边,不能移动的人为输,无限循环则为平局,棋子初始位置为$S$ 现在有一个人可以同时控制两个玩家,问是否能使得第一个人必胜,并输出一个 ...

  3. CodeForces 659E New Reform (图的遍历判环)

    Description Berland has n cities connected by m bidirectional roads. No road connects a city to itse ...

  4. E. Andrew and Taxi(二分+拓扑判环)

    题目链接:http://codeforces.com/contest/1100/problem/E 题目大意:给你n和m,n代表有n个城市,m代表有m条边,然后m行输入三个数,起点,终点,花费.,每一 ...

  5. E - Andrew and Taxi-二分答案-topo判环

    E - Andrew and Taxi 思路 :min max   明显二分答案,二分需要破坏的那些边的中机器人数量最多的那个. check 过程建边时直接忽略掉小于 mid 的边,这样去检验有无环存 ...

  6. UVA818-Cutting Chains(二进制枚举+dfs判环)

    Problem UVA818-Cutting Chains Accept:393  Submit:2087 Time Limit: 3000 mSec  Problem Description Wha ...

  7. [bzoj3012][luogu3065][USACO12DEC][第一!First!] (trie+拓扑排序判环)

    题目描述 Bessie has been playing with strings again. She found that by changing the order of the alphabe ...

  8. HDUOJ--4888--Redraw Beautiful Drawings【isap】网络流+判环

    链接:http://acm.hdu.edu.cn/showproblem.php? pid=4888 题意:一个矩阵.限定每行行和.列和,每一个格子数字不超过k,问矩阵是否存在,如存在推断有单解还是多 ...

  9. CodeForces-1217D (拓扑排序/dfs 判环)

    题意 https://vjudge.net/problem/CodeForces-1217D 请给一个有向图着色,使得没有一个环只有一个颜色,您需要最小化使用颜色的数量. 思路 因为是有向图,每个环两 ...

随机推荐

  1. 用django统计代码行数+注释行数

    实现统计代码行数: 1.首先在url.py中配置 from django.conf.urls import url from django.contrib import admin from app0 ...

  2. 设计模式之Composite(组合)(转)

    Composite定义: 将对象以树形结构组织起来,以达成"部分-整体" 的层次结构,使得客户端对单个对象和组合对象的使用具有一致性. Composite比较容易理解,想到Comp ...

  3. 【转】SQLyog SSH 密钥登陆认证提示: No supported authentication methods available 解决方法

    问题背景: 问题原因: SQLyog不支持非标准的的私钥格式 解决方案: 使用puttyGen重新导入原来的私钥,然后重新保存成PPK证书文件,最后用SQLyog加载该PPK文件即可. 效果截图: 原 ...

  4. loadRunner回访脚本时报Error -27987: Requested image not found [MsgId: MERR-27987]

    loadRunner录制:登陆订机票网址->订机票的过程 loadRunner回访脚本时报Error -27987: Requested image not found  [MsgId: MER ...

  5. SQL非域环境下带自动故障转移数据库镜像的实现方法(包括镜像服务器)

    使用数据库镜像来提高数据库的高可用性,在镜像服务器创建镜像数据库的快照以卸载报表查询对生产数据库的负载.TechNet有讲座对此技术进行介绍,但看到大家在讲座的讨论区中遇到了很多问题,下面我把在非域环 ...

  6. Hadoop学习笔记之三:DataNode

    DataNode对ClientDatanodeProtocol.InterDatanodeProtocol两个协议接口进行了实现,通过ipc::Server向Client.其它DN提供RPC服务(参见 ...

  7. MyEclipse配置默认自带的HTML/JSP代码格式化

    MyEclipse自带默认的HTML/JSP代码格式化并不适合个人开发习惯,因此特意配置如下: 设置行宽为:720(直接加10倍) 使用tabs缩进,单位:1 缩进标签元素要求删除: a开头:a. b ...

  8. UVA - 748 Exponentiation

    Problems involving the computation of exact values of very large magnitude and precision are common. ...

  9. hiho一下 第144周

    题目1 : 机会渺茫 时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi最近在追求一名学数学的女生小Z.小Z其实是想拒绝他的,但是找不到好的说辞,于是提出了这样的要求: ...

  10. ucos ii 46个系统API函数解析

    源: ucos ii 46个系统API函数解析