博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 3172 Virtual Friends(简单并查集)
阅读量:4034 次
发布时间:2019-05-24

本文共 2838 字,大约阅读时间需要 9 分钟。

1.题目大意:

网上交友,计算朋友间都有关系的最多人数,用并查集做,

2,wrong answer的原因:

1、有多组样例

while(scanf("%d",&t)!=EOF)

    {
        while(t--)
        { }

    }

2、超时错在用charchange()给出每个人对应的数字编号,改正方法用map;

3/超时解决方式、find方法进行路径压缩、判断0个关系的情况(不确定)

三、题目:

Virtual FriendsTime Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2085    Accepted Submission(s): 593Problem DescriptionThese days, you can do all sorts of things online. For example, you can use various websites to make virtual friends. For some people, growing their social network (their friends, their friends' friends, their friends' friends' friends, and so on), has become an addictive hobby. Just as some people collect stamps, other people collect virtual friends. Your task is to observe the interactions on such a website and keep track of the size of each person's network. Assume that every friendship is mutual. If Fred is Barney's friend, then Barney is also Fred's friend. InputInput file contains multiple test cases. The first line of each case indicates the number of test friendship nest.each friendship nest begins with a line containing an integer F, the number of friendships formed in this frindship nest, which is no more than 100 000. Each of the following F lines contains the names of two people who have just become friends, separated by a space. A name is a string of 1 to 20 letters (uppercase or lowercase).  OutputWhenever a friendship is formed, print a line containing one integer, the number of people in the social network of the two people who have just become friends. Sample Input13Fred BarneyBarney BettyBetty Wilma Sample Output234 SourceUniversity of Waterloo Local Contest 2008.09  Recommendchenrui

 

四、代码:

#include
#include
#include
using namespace std;map
m;int set[100005];int num[100005];int find(int x){ int r=x; while(r!=set[r]) r=set[r]; int i=x; while(i!=r) { int j=set[i]; set[i]=r; i=j; } return r;}void merge(int x,int y){ int fx=find(x); int fy=find(y); if(fx!=fy) { set[fx]=fy; num[fy]+=num[fx]; printf("%d\n",num[fy]); } else { printf("%d\n",num[fy]); }}int main(){ int t; char a[25]; char b[25]; while(scanf("%d",&t)!=EOF) { while(t--) { int n; scanf("%d",&n); for(int i=1;i<100005;i++) { set[i]=i; num[i]=1; } m.clear(); int ans=1; for(int i=1;i<=n;i++) { scanf("%s%s",a,b); if(!m[a]) { m[a]=ans++; } if(!m[b]) { m[b]=ans++; } merge(m[a],m[b]); } } } return 0;}

 

转载地址:http://tycdi.baihongyu.com/

你可能感兴趣的文章
01Java基础语法-16. while循环结构
查看>>
01Java基础语法-17. do..while循环结构
查看>>
01Java基础语法-18. 各种循环语句的区别和应用场景
查看>>
01Java基础语法-19. 循环跳转控制语句
查看>>
Django框架全面讲解 -- Form
查看>>
socket,accept函数解析
查看>>
今日互联网关注(写在清明节后):每天都有值得关注的大变化
查看>>
”舍得“大法:把自己的优点当缺点倒出去
查看>>
[今日关注]鼓吹“互联网泡沫,到底为了什么”
查看>>
[互联网学习]如何提高网站的GooglePR值
查看>>
[关注大学生]求职不可不知——怎样的大学生不受欢迎
查看>>
[关注大学生]读“贫困大学生的自白”
查看>>
[互联网关注]李开复教大学生回答如何学好编程
查看>>
[关注大学生]李开复给中国计算机系大学生的7点建议
查看>>
[关注大学生]大学毕业生择业:是当"鸡头"还是"凤尾"?
查看>>
[茶余饭后]10大毕业生必听得歌曲
查看>>
gdb调试命令的三种调试方式和简单命令介绍
查看>>
C++程序员的几种境界
查看>>
VC++ MFC SQL ADO数据库访问技术使用的基本步骤及方法
查看>>
VUE-Vue.js之$refs,父组件访问、修改子组件中 的数据
查看>>