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:
- For each station i there exists exactly one train that goes from this station. Its destination station is pi, possibly pi = i;
- 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!
InputThe 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.
OutputPrint one number — the maximum possible value of convenience.
Examplesinput3
2 1 3output9input5
1 5 4 3 2output17NoteIn 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 ----判环,思路的更多相关文章
- 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 ...
- CodeForces 937D 936B Sleepy Game 有向图判环,拆点,DFS
题意: 一种游戏,2个人轮流控制棋子在一块有向图上移动,每次移动一条边,不能移动的人为输,无限循环则为平局,棋子初始位置为$S$ 现在有一个人可以同时控制两个玩家,问是否能使得第一个人必胜,并输出一个 ...
- CodeForces 659E New Reform (图的遍历判环)
Description Berland has n cities connected by m bidirectional roads. No road connects a city to itse ...
- E. Andrew and Taxi(二分+拓扑判环)
题目链接:http://codeforces.com/contest/1100/problem/E 题目大意:给你n和m,n代表有n个城市,m代表有m条边,然后m行输入三个数,起点,终点,花费.,每一 ...
- E - Andrew and Taxi-二分答案-topo判环
E - Andrew and Taxi 思路 :min max 明显二分答案,二分需要破坏的那些边的中机器人数量最多的那个. check 过程建边时直接忽略掉小于 mid 的边,这样去检验有无环存 ...
- UVA818-Cutting Chains(二进制枚举+dfs判环)
Problem UVA818-Cutting Chains Accept:393 Submit:2087 Time Limit: 3000 mSec Problem Description Wha ...
- [bzoj3012][luogu3065][USACO12DEC][第一!First!] (trie+拓扑排序判环)
题目描述 Bessie has been playing with strings again. She found that by changing the order of the alphabe ...
- HDUOJ--4888--Redraw Beautiful Drawings【isap】网络流+判环
链接:http://acm.hdu.edu.cn/showproblem.php? pid=4888 题意:一个矩阵.限定每行行和.列和,每一个格子数字不超过k,问矩阵是否存在,如存在推断有单解还是多 ...
- CodeForces-1217D (拓扑排序/dfs 判环)
题意 https://vjudge.net/problem/CodeForces-1217D 请给一个有向图着色,使得没有一个环只有一个颜色,您需要最小化使用颜色的数量. 思路 因为是有向图,每个环两 ...
随机推荐
- 什么是ASCII
以下内容是从百度百科学的 1)ASCII(American Standard Code for Information Interchange:美国信息交换标准代码) 2)产生原因 在计算机中,所有的 ...
- numpy数学数据处理
数学和统计方法 sum 对数组中全部或某轴向的元素求和.零长度的数组的sum为0. mean 算术平均数.零长度的数组的mean为NaN. import numpy as np import nump ...
- Jmeter下进行ip伪造
转至https://blog.csdn.net/xingchao416/article/details/53506051 1.首先获取一些闲置的ip地址,且必须为固定地址,不能是自动获取的地址,方法: ...
- greenplum presto impala选型与测评
查看原文请至:https://my.oschina.net/hblt147/blog/1843028
- flask 在模板中渲染表单
在模板中渲染表单 为了能够在模板中渲染表单,我们需要把表单类实例传入模板.首先在视图函数里实例化表单类LoginForm,然后再render_template()函数中使用关键脑子参数form将表单实 ...
- tomcat2章2
package ex02.pyrmont1; import java.io.File; public class Constants { public static final String WEB_ ...
- 设置PyCharm创建文件时自动添加头文件
找到该路径并添加以下信息 File->settings->Editor->File and Code Templates->Python Script #!/usr/bin/ ...
- 了解Redis过期策略及实现原理
我们在使用redis时,一般会设置一个过期时间,当然也有不设置过期时间的,也就是永久不过期. 当我们设置了过期时间,redis是如何判断是否过期,以及根据什么策略来进行删除的. redis设置过期时间 ...
- inst_for_mysql5.7.sh
#!/bin/bash # Author: wangshenjin<wangshenjin233@foxmail.com> # Description: install percona-s ...
- Golang命令行拷贝文件
package main import ( "fmt" "io" "os" ) func main() { list := os.Args ...