Codeforces 884C.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

32 1 3

output

9

input

51 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;
}

  

时间: 2024-10-26 10:47:30

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

CodeForces 884A.B.C Book Reading(水) | Japanese Crosswords Strike Back(水) | Bertown Subway(DFS)

Recently Luba bought a very interesting book. She knows that it will take t seconds to read the book. Luba wants to finish reading as fast as she can. But she has some work to do in each of n next days. The number of seconds that Luba has to spend wo

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

Description Berland has n cities connected by m bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It isnot guaranteed that you can get from any city to any other one, using only the

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

题意: 一种游戏,2个人轮流控制棋子在一块有向图上移动,每次移动一条边,不能移动的人为输,无限循环则为平局,棋子初始位置为$S$ 现在有一个人可以同时控制两个玩家,问是否能使得第一个人必胜,并输出一个解,否则判断是否能平局 题解: 看到这个题首先我想到了强连通分量,但是事实证明求出强连通分量,缩点对解决问题没有什么帮助.... 能写一些看似正确的算法,但其实是假算法来的.. ........... 所以应该先分析策略,肯定是能赢就赢,不能赢就求平局,最后才算输 平局很好判断,有向图上,从$S$点

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

链接:http://acm.hdu.edu.cn/showproblem.php?pid=4888 题意:一个矩阵,限定每行行和.列和,每个格子数字不超过k,问矩阵是否存在,如存在判断有单解还是多解. 思路:之前多校的题目,那时候还不会网络流,现在A掉了,矩阵的建图模型,判断网络流是否可行只要判断最大流是否等于总行和或总列和即可,判环是看的别人的解题报告,方法是使用dfs查找残余网络中是否有还存在容量的弧形成了环,如果有,说明可以通过这个环改变容量网络内部的增广路方式,而源汇的流量是不会变的,就

HDU 1272 小希的迷宫 (无向图判环)

题意:给出一张无向图,判定是否有环,判定是否为一棵树: 思路:并查集判环,唯一祖先: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define M 0x3f3f3f3f int fa[500010],mark[500010]; void init() { for(int i=0;i<=500010;i++) fa[i]=i; } int fin(int x

hdu 1317 XYZZY(spfa判环)

http://acm.hdu.edu.cn/showproblem.php?pid=1317 大致题意:有n个房间,每个房间都有对应的能量值(可正可负),现在从1出发要到达n,初始能量为100,问是否能够达到n点,到达n的条件是中间及最后的能量值都要大于0. 思路:若不考虑环,那么求最长路判断是否大于0即可.若存在负环,对求最长路也没影响:但当存在正环时,最长路就不存在了.可用spfa判断,当某点入队超过n次,那么它必定在环中,直接将其dis置为INF,并不再将其近队列.最后若能到达n则可行,否

CF 936B Sleepy Game(判环+BFS)

题目链接:http://codeforces.com/problemset/problem/936/B 题目: Petya and Vasya arranged a game. The game runs by the following rules. Players have a directed graph consisting of n vertices and medges. One of the vertices contains a chip. Initially the chip

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

Problem UVA818-Cutting Chains Accept:393  Submit:2087 Time Limit: 3000 mSec  Problem Description What a ?nd! Anna Locke has just bought several links of chain some of which may be connected. They are made from zorkium, a material that was frequently

HDU - 4514 湫湫系列故事——设计风景线(并查集判环+树形DP)

题目链接:https://vjudge.net/problem/HDU-4514 随着杭州西湖的知名度的进一步提升,园林规划专家湫湫希望设计出一条新的经典观光线路,根据老板马小腾的指示,新的风景线最好能建成环形,如果没有条件建成环形,那就建的越长越好.  现在已经勘探确定了n个位置可以用来建设,在它们之间也勘探确定了m条可以设计的路线以及他们的长度.请问是否能够建成环形的风景线?如果不能,风景线最长能够达到多少?  其中,可以兴建的路线均是双向的,他们之间的长度均大于0. Input 测试数据有