zoukankan      html  css  js  c++  java
  • C. Bertown Subway

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

     就是求最大的环,可以让两个环连成一个,一开始用并查集做,不知怎的就是错就换数组了。

     1 #include <bits/stdc++.h>
     2 #define ll long long
     3 using namespace std;
     4 const int N = 100010;
     5 int fa[N], cnt[N];
     6 bool vis[N];
     7 int main() {
     8     int n;
     9     cin >> n;
    10     for(int i = 1; i <= n; i ++) cin >> fa[i];
    11     for(int i = 1; i <= n; i ++) {    
    12         if(!vis[i]) {
    13             ll ans = vis[i] = 1, tmp = fa[i];
    14             while(!vis[tmp]) {
    15                 ans ++;
    16                 vis[tmp] = true;
    17                 tmp = fa[tmp];
    18             }
    19             cnt[i] = ans;
    20         }
    21     }
    22     sort(cnt+1, cnt+1+n);
    23     ll sum = 0;
    24     if(n > 1) sum = (ll)(cnt[n]+cnt[n-1])*(ll)(cnt[n]+cnt[n-1]);
    25     else sum = 1;
    26     for(int i = 1; i <= n-2; i ++) {
    27         sum += (ll)cnt[i]*(ll)cnt[i];
    28     }
    29     printf("%lld
    ",sum);
    30     return 0;
    31 }
  • 相关阅读:
    如何将自己编写的软件放在真机上运行
    多台Mac电脑使用一个apple开发者账号
    Xcode5最初级的教程
    利用NSIS软件制作C#安装包
    C#针对DataTable进行分页方法
    Json 的日期格式转换成DateTime
    FI & RI
    Rename a TFS Project Collection
    char、varchar、nchar、nvarchar的区别
    Migration from TF Service to TF Server with the TFS Integration Platform
  • 原文地址:https://www.cnblogs.com/xingkongyihao/p/7801893.html
Copyright © 2011-2022 走看看