Codeforces-Bertown Subway(思维)
来源:互联网 发布:quickconnect 域名 编辑:程序博客网 时间:2024/04/28 06:31
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 theBertown Transport Law:
- For each station i there exists exactly one train that goes from this station. Its destination station ispi, possiblypi = i;
- For each station i there exists exactly one stationj such that pj = i.
The President will consider the convenience of subway after visiting it. Theconvenience is the number of ordered pairs(x, y) such that person can start at stationx and, after taking some subway trains (possibly zero), arrive at stationy (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 fornot more than two subway stations. Of course, breaking theBertown Transport Law is really bad, so the subway must be built according to theLaw 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 possibleconvenience he can get!
The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations.
The second line contains n integer numbersp1, p2, ..., pn (1 ≤ pi ≤ n) — the current structure of the subway. All these numbers are distinct.
Print one number — the maximum possible value of convenience.
32 1 3
9
51 5 4 3 2
17
In the first example the mayor can change p2 to3 and p3 to1, 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 to4 and p3 to5.
思路:求出每个连通块的大小,把最大的连个合并,剩下的不变,ans等于每个连通块的平方和,暴力写就行,注意这题要用long long
AC代码:
#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<queue>using namespace std;int n,x;int arr[100005],cnt[100005];bool vis[100005];int main(){ while(~scanf("%d",&n)) { memset(vis,false,sizeof(vis)); memset(cnt,0,sizeof(cnt));//存每个连通快的大小 for(int i=1;i<=n;i++) { scanf("%d",&x); arr[i]=x; } int tmp,c; for(int i=1;i<=n;i++) { if(vis[i]) continue; tmp=arr[i];c=1;vis[i]=true; while(!vis[tmp]) { c++; vis[tmp]=true; tmp=arr[tmp]; } cnt[i]=c; } sort(cnt+1,cnt+n+1); long long int ans=0; if(n>1)ans=(long long)(cnt[n]+cnt[n-1])*(long long)(cnt[n]+cnt[n-1]);//合并最大的两个 else ans=1; for(int i=1;i<=n-2;i++) { ans+=(long long)cnt[i]*(long long)cnt[i]; } printf("%lld\n",ans); } return 0;}
- Codeforces-Bertown Subway(思维)
- Bertown Subway CodeForces
- codeforces 884C. Bertown Subway
- codeforces 884C Bertown Subway
- Educational Codeforces Round 31 C.Bertown Subway(图论)
- Educational Codeforces Round 31 C. Bertown Subway
- 【想法】codeforces.com/contest/884/problem/C Bertown Subway
- Educational Codeforces Round 31 C. Bertown Subway dfs
- Codeforces 371E Subway Innovation【思维+前缀和】
- CodeForces 371 E.Subway Innovation(贪心)
- Codeforces Beta Round #89 (Div. 2) E. Bertown roads(Tarjan、边双连通分量)
- Codeforces Round #218 (Div. 2) E. Subway Innovation(思维 前缀和)
- Codeforces 131D - Subway
- Codeforces 131D Subway(找图上唯一环)
- Codeforces Beta Round #95 (Div. 2) -- D. Subway (DFS)
- codeforces 118E Bertown roads 边双连通
- Codeforces Beta Round #89 (Div. 2) E. Bertown roads
- Codeforces 118E Bertown roads【边双联通】
- 5. 1 IO流(字符流(6个),字节流(4个),(缓冲区装饰类(装饰设计模式)),(转换流))
- Javascript计算器(二)-简单的布局与美化
- Mybatis源码研究之SqlSource
- LaTex: 数学公式
- C++ 拷贝构造函数
- Codeforces-Bertown Subway(思维)
- 启动新进程之复制进程映像——fork系统调用
- Java数组的创建与初始化
- 电脑上不了Github的完整解决方法(亲测)
- 渲染管线理解4
- C语言知识点杂谈
- linux RT_PREEMPT 高CPU负载下的驱动稳定性?
- 3.JAVA后端学习笔记——基础知识2
- 上线两个月,微信小程序给那些用身体支持它的人带来了什么?