codeup-More is better

2024-01-13 20:08
文章标签 better codeup

本文主要是介绍codeup-More is better,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目描述

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.

样例输入

3
1 3
1 5
2 5
4
3 2
3 4
1 6
2 6

样例输出

4
5
Sample Input
  
4 1 2 3 4 5 6 1 6 4 1 2 3 4 5 6 7 8

Sample Output
  
4 2
Hint
A and B are friends(direct or indirect), B and C are friends(direct or indirect), then A and C are also friends(indirect).In the first sample {1,2,5,6} is the result. In the second sample {1,2},{3,4},{5,6},{7,8} are four kinds of answers.

这个问题是求最大集合中的元素个数,并且要尽量优化一下,毕竟数组开的很大,遍历的话花费时间很多

记录一下集合中的最大元素,然后遍历到它就行了

#include<cstdio>
#include<string.h>
int find(int x);
void pre(int x);
void Union(int a,int b);
int pn[10000010],p[10000010];
int main() 
{int n,m,i,j,t;int a,b;while(scanf("%d",&n)!=EOF){if(n==0)printf("1\n");else{int cnt,max,Max;memset(p,0,sizeof(p));cnt=0,max=0,Max=0;pre(10000000);for(i=1;i<=n;i++){scanf("%d%d",&a,&b);if(a>Max)Max=a;if(b>Max)Max=b;Union(a,b);}	for(i=1;i<=Max;i++){p[find(i)]++;}for(i=1;i<=Max;i++){if(p[i]>max)max=p[i];}	printf("%d\n",max);	}			}return 0;
}
void pre(int x)
{int i;for(i=1;i<=x;i++){pn[i]=i;}		
}
int find(int x)
{int r,z;r=x;while(r!=pn[r]){r=pn[r];}while(x!=pn[x]){z=x;x=pn[x];pn[z]=r;}return r;
}
void Union(int a,int b)
{int fa,fb;fa=find(a);fb=find(b);if(fa!=fb){pn[fb]=fa;}
}


这篇关于codeup-More is better的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/602638

相关文章

[论文笔记]Making Large Language Models A Better Foundation For Dense Retrieval

引言 今天带来北京智源研究院(BAAI)团队带来的一篇关于如何微调LLM变成密集检索器的论文笔记——Making Large Language Models A Better Foundation For Dense Retrieval。 为了简单,下文中以翻译的口吻记录,比如替换"作者"为"我们"。 密集检索需要学习具有区分性的文本嵌入,以表示查询和文档之间的语义关系。考虑到大语言模

@vueup/vue-quill使用quill-better-table报moduleClass is not a constructor

quill官方中文文档:https://www.kancloud.cn/liuwave/quill/1434144 扩展表格的使用 注意:想要使用表格 quill的版本要是2.0以后 升级到这个版本后 其他一些插件就注册不了了。 安装: npm install quill@latest   版本需要大于2.0版本 npm install quill-better-table 引入&

more is better(并查集)

题目描述 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 select

用智能插件(Fitten Code: Faster and Better AI Assistant)再次修改vue3 <script setup>留言板

<template><div><button class="openForm" @click="openForm" v-if="!formVisible">编辑</button><button @click="closeForm" v-if="formVisible">取消编辑</button><hr /><formv-if="formVisible"@submit.prevent

如何成为优秀的游戏测试工程师——Do Better

基础         之前基于入门游戏测试写了几点基础的要求,胜任游戏测试的基本条件——Be a G·Tester 《赢在测试2》         最近阅读了《赢在测试2——中国软件测试专家访谈录》,虽然是软件测试,但跟游戏测试在思想上有些地方是相通的。这本书非常不错,都是做了10多年测试的牛人们的心血总结,对于已经做游戏测试3到5年的童靴在思想上会有所帮助。其中每位专家都有提到如何成

hdu 1856 More is better

本题题意:给出关系,可能存在多个宗派;                   求出人数最多宗派的人数是多少?      核心代码:                      pre[yy]=xx;                     mark[xx]+=mark[yy];                     每确定一个宗派内的关系,就往该宗派的祖宗上加一;

HDU1561The more, The Better (树形DP)

Problem Description ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝物。但由于地理位置原因,有些城堡不能直接攻克,要攻克这些城堡必须先攻克其他某一个特定的城堡。你能帮ACboy算出要获得尽量多的宝物应该攻克哪M个城堡吗? Input 每个测试实例首先包括2个整数,N

背包(5)Hdu 1561 The more, The Better(有限制的背包,分组背包)

The more, The Better Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面

HDU ACM 1856. More is better(并查集)

/********************************** 题目大意:把是朋友的人放到一组,求出人数最多的一组,并输出人数; 题目解析:运用并查集把有关系的人合并到一组,并且计算出此集合的结点数rank[i];          把rank[i]与rank1比较大小,把值大的赋值给rank1,最后输出题目要求的结果rank1; 错误分析:1. 下面的1,比较大小放在Union中 ,可以

辞旧迎新 be better---记我的2013

辞旧迎新--be better 额 果然是老了。这篇日志放草稿箱里好久,还以为早就发出去了呢。。。之前在空间里发过,略微改了改。 在写这篇日志之前我把13年自己写过的40多篇很水的博客和20多篇日记照片都看了一遍。才发现记录真的能够帮助我们整理生命,也正是这些我自己都看着不成熟的日志,帮我记录着成长的轨迹。 2013年我们好多共同的记忆。四川又地震了,雾霾也长大了,安倍去参拜靖国