• 1742. Team building


    1742. Team building

    Time limit: 1.0 second Memory limit: 64 MB
    There are n programmers in the software development company. Each of them thinks that he is the greatest or the second greatest programmer in the company. In the latter case he can name the greatest programmer in his opinion.
    The administration decided to divide all programmers into development teams using the following algorithm:
    1. If there are programmers, who are not assigned to a development team, choose any of them and mark him as current one.
    2. Create a new development team and assign it to the current programmer.
    3. If the current programmer thinks that one of his colleagues is the greatest programmer and this colleague is not assigned to a development team, then this colleague is assigned to the same development team and is marked as current programmer. Then the step 3 is repeated. Otherwise, the team is formed, and the administration returns to the step 1.
    What is the minimal and maximal number of teams which can be formed in this company according to the algorithm?

    Input

    The first line contains an integer n (1 ≤ n ≤ 105). The i-th of the following n lines contains the number of the programmer, who is the greatest, according to thei-th programmer's opinion.

    Output

    Output the minimal and maximal number of development teams, separated with space.

    Sample

    inputoutput
    4
    2
    3
    4
    2
    1 2
    Problem Author: Artyom Ripatti Problem Source: Ufa SATU Contest. Petrozavodsk Summer Session, August 2009
    ***************************************************************************************
    手动找环(因为每个节点的出度都唯一),先找出入度为0的点搜索,如果发现pre==vis[next[pre0]],则找出了一个强连通分量,ans++;
    ***************************************************************************************
     1 #include<iostream>
     2 #include<cstring>
     3 #include<string>
     4 #include<cstdio>
     5 #include<cmath>
     6 using namespace std;
     7 int in[100010];
     8 int next[100010];
     9 int n,i,j,k;
    10 int vis[100010];
    11 int cnt[100010];
    12 int a[100010];
    13 int getnum(int pre)
    14  {
    15      int gs=pre;
    16      int tot=0;
    17      while(vis[pre]<=0)
    18       {
    19           vis[pre]=gs;
    20            cnt[pre]=tot++;
    21           if(vis[next[pre]]>0)
    22            {
    23                if(vis[next[pre]]==gs)//找环返回
    24                 return  cnt[next[pre]];
    25                else
    26                  return tot-1;
    27            }
    28           pre=next[pre];
    29       }
    30  }
    31 int main()
    32 {
    33     cin>>n;
    34     memset(in,0,sizeof(in));
    35     for(i=1;i<=n;i++)
    36      {
    37          cin>>next[i];
    38          in[next[i]]++;
    39      }
    40      int  len=0;
    41      for(i=1;i<=n;i++)
    42       {
    43           if(in[i]==0)
    44             a[++len]=i;
    45       }
    46      int ans1=0;
    47      int ans2=0;
    48     memset(vis,0,sizeof(vis));
    49     for(i=1;i<=len;i++)
    50      {
    51          ans1++;
    52          ans2+=getnum(a[i])+1;
    53      }
    54     for(i=1;i<=n;i++)//再扫一遍,防止漏掉
    55      {
    56          if(vis[i]<=0)
    57           {
    58               ans1++;
    59               ans2+=getnum(i)+1;
    60           }
    61      }
    62      cout<<ans1<<' '<<ans2<<endl;
    63      return 0;
    64 
    65 }
    View Code
  • 相关阅读:
    前台 图片上传 上传预览 调用上传服务(多张图片展示)
    正则表达式验证,只能输入数字
    点击文本框搜索,出现在下拉列表中
    keycode 锁键盘按键(只能输入数字)
    Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
    map 理解
    mybatis 关联关系查询 java
    mybatis 批量插入值的sql
    EJB
    JPA概要
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3252303.html
Copyright © 2020-2023  润新知