<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont

本文涉及的产品
转发路由器TR,750小时连接 100GB跨地域
简介: 问题描述: Mr Wang wants some boys to help him with a project. Because the project is rather complex, the more boys come, the better it will be.

问题描述:
Mr Wang wants some boys to help him with a project. Because the project is rather complex, the more boys come, the better it will be. Of course there are certain requirements.Mr Wang selected a room big enough to hold the boys. The boy who are not been chosen has to leave the room immediately. There are 10000000 boys in the room numbered from 1 to 10000000 at the very beginning. After Mr Wang’s selection any two of them who are still in this room should be friends (direct or indirect), or there is only one boy left. Given all the direct friend-pairs, you should decide the best way.

输入:

The first line of the input contains an integer n (0 ≤ n ≤ 100 000) - the number of direct friend-pairs. The following n lines each contains a pair of numbers A and B separated by a single space that suggests A and B are direct friends. (A ≠ B, 1 ≤ A, B ≤ 10000000)
.
输出:

The output in one line contains exactly one integer equals to the maximum number of boys Mr Wang may keep.

样例输入:
4
1 2
3 4
5 6
1 6
4
1 2
3 4
5 6
7 8
样例输出:
4
2

思路:
利用并查集的基础,进行修改,注意数组的范围。以及路径压缩。

代码:

#include<iostream>
#include<cstring>
#include<stdio.h>
const int MX=1000000;
using namespace std;
int pre[MX],t[MX];

int fid(int x)
{
    int r=x,t;
    while(r!=pre[r])
    {
        r=pre[r];
    }

     while (x != r)
        { t = pre[x];
        pre[x] = r;
     x = t; }
    return x;
}
void mix(int a,int b)
{
    int x=fid(a),y=fid(b);
    if(x!=y)
    {
        pre[x]=y;
    }
}
int main()
{
    int n;

   while(scanf("%d",&n)!=EOF)
    {
    int maxn=0;

       for(int i=1;i<=MX;i++)
         {pre[i]=i;

         t[i]=0;}
       for(int i=1;i<=n;i++)
        {
           int a,b;
           cin>>a>>b;
           if(a>maxn)maxn=a;
           if(b>maxn)maxn=b;
           if(fid(a)!=fid(b))
           mix(a,b);
        }
     int ct=1;
       for(int i=1;i<=maxn;i++)
       {
        t[fid(i)]++;
       }
      for(int i=1;i<=maxn;i++)
       {
              if(t[i]>ct)
              ct=t[i];
      }
    cout<<ct<<endl;
   }
 return 0;
}
目录
相关文章
|
Web App开发 前端开发 Java
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
 Connection reset by peer的常见原因: 1)服务器的并发连接数超过了其承载量,服务器会将其中一些连接关闭;    如果知道实际连接服务器的并发客户数没有超过服务器的承载量,看下有没有网络流量异常。
862 0
|
Web App开发 存储 前端开发
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
NoSuchObjectException(message:There is no database named cloudera_manager_metastore_canary_test_db_hive_hivemetastore_df61080e04cd7eb36c4336f71b5a8bc4) at org.
1082 0
|
Web App开发 前端开发
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
service cloudera-scm-agent stop service cloudera-scm-agent stop umount /var/run/cloudera-scm-agent/process umo...
764 0
|
Web App开发 前端开发 数据库
|
Web App开发 前端开发
|
Web App开发 前端开发 关系型数据库
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
如果mysql正在运行,/etc/init.d/mysqld stop 启动mysql(无需输入密码):bin/safe_mysqld –skip-grant-tables & 在bin目录下执行mysql,此时无需输入密...
808 0
|
Web App开发 前端开发 数据库
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
数据仓库建设步骤Posted on 2015-03-04 10:18 xuzhengzhu 阅读(1164) 评论(0) 编辑 收藏 1.系统分析,确定主题 确定一下几个因素:    ·操作出现的频率,即业务部门每隔多长时间做一次查询分析。
861 0
|
Web App开发 前端开发
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
在统计分析系统中, 维度:指人们分析事物的角度。比如,分析活跃用户,可以从时间的维度,也可以从地域的维度去看,也可以时间、地域两个维度组合去分析。
668 0