码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces 884C.Bertown Subway ----判环,思路

时间:2017-11-01 18:15:45      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:line   证明   amp   enc   put   space   pos   pre   font   

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)2>=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 ----判环,思路

标签:line   证明   amp   enc   put   space   pos   pre   font   

原文地址:http://www.cnblogs.com/wsblm/p/7767730.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!