1. 程式人生 > >Codeforces 884C.Bertown Subway ----判環,思路

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

str eal 問題 ctu val inpu ger arr sport

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 ----判環,思路