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 请给一个有向图着色,使得没有一个环只有一个颜色,您需要最小化使用颜色的数量. 思路 因为是有向图,每个环两 ...
随机推荐
- python二叉树的深度遍历之先序遍历流程图
- The Little Prince-11/28
The Little Prince-11/28 Today I find some beautiful words from the book. You know -- one loves the s ...
- Spring/SpringMVC/MyBatis(持久层、业务层、控制层思路小结)
准备工作: ## 7 导入省市区数据到数据库中 1. 从FTP下载SQL脚本文件 2. 把脚本文件移动到易于描述绝对路径的位置 3. 进入MySQL控制台 4. 使用`xxx_xxx`数据库 5. 运 ...
- 3 字节的 UTF-8 序列的字节 3 无效 解决
参考下列应该可以解决,笔者为3. 1.https://blog.csdn.net/hostel_2/article/details/51517361 2.https://blog.csdn.net/u ...
- request 模块详细介绍
request 模块详细介绍 request Requests 是使用 Apache2 Licensed 许可证的 基于Python开发的HTTP 库,其在Python内置模块的基础上进行了高度的封装 ...
- Python学习基础(二)——集合 深浅拷贝 函数
集合 # 集合 ''' 集合是无序不重复的 ''' # 创建列表 l = list((1, 1, 1)) l1 = [1, 1, 1] print(l) print(l1) print("* ...
- Java Web-----JSP与Servlet(一)
JSP (java服务器页面) JSP全名为Java Server Pages,中文名叫java服务器页面,其根本是一个简化的Servlet设计,它[1] 是由Sun Microsystems公司倡 ...
- k8s开发环境
在搭建开发环境之前, 请Try Kubernetes,Get Started and CONCEPTS 可以自己使用minikube 来搭建个环境, 自己玩一玩. K8s需要一些依赖. 参看官方文档 ...
- Linux限制普通用户只能使用某命令
修改sudoers(/etc/sudoers)
- 《学习OpenCV3》第14章课后习题
1.在一条含有 N 个点的封闭轮廓中,我们可以通过比较每个点与其它点的距离,找出最外层的点.(这个翻译有问题,而且这个问题是实际问题) a.这样一个算法的复杂度是多少? b.怎样用更快的速度完成这个任 ...