HDU4039(map应用)
The Social Network
Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 2958 Accepted Submission(s): 870
Problem Description
The social network system (SNS) helps people to keep connecting with their friends. Every user in SNS has a friends list. The user can read news posted by the users in his friends list. Friend relation is symmetric - if A is a friend of B, B is always a friend of A.
Another important function in SNS is friend recommendation. One effective way to recommend friends is recommend by mutual friends. A mutual friend between two users A and B, is a user who is a friend of both A and B. A user can not be a friend of himself. For a specific user A, the system will recommend the user who is not himself or his friend, and has mutual friends with A. If more than one such user exists, recommend the one has most mutual friends with A. If still a tie exists, output all of them.
Input
The first line is a integer T (T≤100), the number of test case.
The beginning of each test case is two integers N and Q, the number of friend relationship and the number of query. 1 ≤ N, Q ≤ 1000
The following N lines each contain two different names separated by a single space. Each name consisted by only lowercase letters, and its length is less than or equal to 15. This means the two users are friends. No friend relationship will be given more than once.
The following Q lines each describe a query. Each line contain one user name. The data guarantee that this name appears at least once in above N lines.
Output
For each case, you should output one line containing “Case k: ” first, where k indicates the case number and counts from one. Then for each query, output one line, contains one or more names of recommended friends, separate by a single space, sorted by alphabetical order. If no persons can be recommended, output one line contains “-”.
Sample Input
1
10 11
hongshu digua
yingying hongshu
xmm hongshu
huaxianzi xmm
tangjiejie huaxianzi
xhmz yingying
digua xhmz
zt tangjiejie
xmm lcy
notonlysuccess ljq
hongshu
digua
yingying
xmm
huaxianzi
tangjiejie
xhmz
zt
lcy
notonlysuccess
ljq
Sample Output
Case 1:
xhmz
yingying
digua
digua tangjiejie yingying
hongshu lcy zt
xmm
hongshu
huaxianzi
hongshu huaxianzi
-
-
题意:给出n组朋友关系,有q个user推荐好友查询.每次按字典序输出user的推荐好友。推荐好友定义:①是user好友的好友.②不是user本身和user的好友
思路:map+暴搜
#include <iostream>
#include <string.h>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
const int MAXN=;
int n,m;
map<string,int> mp;
map<int,string> rp;
int tot;
int dep[MAXN];
vector<int> arc[MAXN];
vector<string> fri;
int vis[MAXN];
int getID(string name)
{
if(mp[name]==)
{
mp[name]=tot;
rp[tot]=name;
tot++;
}
return mp[name];
}
void addedge(int u,int v)
{
arc[u].push_back(v);
arc[v].push_back(u);
}
int main()
{
int T;
cin>>T;
for(int cas=;cas<=T;cas++)
{
mp.clear();
rp.clear();
for(int i=;i<tot;i++)
{
arc[i].clear();
}
tot=;
cin>>n>>m;
for(int i=;i<n;i++)
{
string A,B;
cin>>A>>B;
int u=getID(A);
int v=getID(B);
addedge(u,v);
}
cout<<"Case "<<cas<<":"<<endl;
for(int i=;i<m;i++)
{
memset(dep,,sizeof(dep));
memset(vis,,sizeof(vis));
fri.clear();
string name;
cin>>name;
int u=getID(name);
for(int i=;i<arc[u].size();i++)
{
int v=arc[u][i];
vis[v]=;//标记u的好友
}
for(int i=;i<arc[u].size();i++)
{
int v=arc[u][i];
for(int j=;j<arc[v].size();j++)
{
int to=arc[v][j];
if(to!=u&&!vis[to]) dep[to]++;//u的推荐好友不能是u自己和u的好友
}
}
int mx=*max_element(dep,dep+tot);
if(mx!=)
{
for(int i=;i<tot;i++)
{
if(dep[i]==mx)
{
string name=rp[i];
fri.push_back(name);
}
}
sort(fri.begin(),fri.end());
int len=fri.size();
for(int i=;i<len-;i++)
{
cout<<fri[i]<<" ";
}
cout<<fri[len-]<<endl;
}
else cout<<"-"<<endl;
}
}
return ;
}
HDU4039(map应用)的更多相关文章
- mapreduce中一个map多个输入路径
package duogemap; import java.io.IOException; import java.util.ArrayList; import java.util.List; imp ...
- .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法
.NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法 0x00 为什么需要Map(MapWhen)扩展 如果业务逻辑比较简单的话,一条主管道就够了,确实用不到 ...
- Java基础Map接口+Collections工具类
1.Map中我们主要讲两个接口 HashMap 与 LinkedHashMap (1)其中LinkedHashMap是有序的 怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...
- Java基础Map接口+Collections
1.Map中我们主要讲两个接口 HashMap 与 LinkedHashMap (1)其中LinkedHashMap是有序的 怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...
- 多用多学之Java中的Set,List,Map
很长时间以来一直代码中用的比较多的数据列表主要是List,而且都是ArrayList,感觉有这个玩意就够了.ArrayList是用于实现动态数组的包装工具类,这样写代码的时候就可以拉进 ...
- Java版本:识别Json字符串并分隔成Map集合
前言: 最近又看了点Java的知识,于是想着把CYQ.Data V5迁移到Java版本. 过程发现坑很多,理论上看大部分很相似,实践上代码写起来发现大部分都要重新思考方案. 遇到的C#转Java的一些 ...
- MapReduce剖析笔记之八: Map输出数据的处理类MapOutputBuffer分析
在上一节我们分析了Child子进程启动,处理Map.Reduce任务的主要过程,但对于一些细节没有分析,这一节主要对MapOutputBuffer这个关键类进行分析. MapOutputBuffer顾 ...
- MapReduce剖析笔记之七:Child子进程处理Map和Reduce任务的主要流程
在上一节我们分析了TaskTracker如何对JobTracker分配过来的任务进行初始化,并创建各类JVM启动所需的信息,最终创建JVM的整个过程,本节我们继续来看,JVM启动后,执行的是Child ...
- MapReduce剖析笔记之五:Map与Reduce任务分配过程
在上一节分析了TaskTracker和JobTracker之间通过周期的心跳消息获取任务分配结果的过程.中间留了一个问题,就是任务到底是怎么分配的.任务的分配自然是由JobTracker做出来的,具体 ...
随机推荐
- 20145240 《Java程序设计》第二次实验报告
20145240 <Java程序设计>第二次实验报告 北京电子科技学院(BESTI)实验报告 课程:Java程序设计 班级:1452 指导教师:娄嘉鹏 实验日期:2016.04.12 实验 ...
- Linux下的文件查找命令——find
Linux下几个常见的文件查找命令: which 查看可执行文件的位置 whereis 寻找特定文件,查看文件的位置 locate 配合数据库查看文件位置 find ...
- Android源码目录分析【转】
本文转载自:http://blog.csdn.net/backgarden_straw/article/details/8050783 在学习Android的过程中,学习写应用还好,一开始不用管太多代 ...
- centos下安装Anaconda
第一步:将下载好的Anaconda2-4.1.1-Linux-x86_64.sh软件传到linux下 第二步:[hadoop@spark1 ~]$ cd Desktop #进入到该软件所在目录,我的放 ...
- 按F12 IE浏览器的开发工具打不开解决方法
在电脑的任务栏中选中开发人员工具窗体右击,出现以下页面: 点击移动菜单项,然后用键盘的上下左右键移动,直到开发人员工具的最小化窗体出现:
- HDU 4348 To the moon (主席树区间更新)
题意:首先给你n个数,开始时间为0,最后按照操作输出 给你四种操作: 1. C l r d : 在(l,r)区间都加上d,时间加一2. Q l r : 询问现在(l,r)的区间和3. H l r ...
- 【转】php 操作数组(合并,拆分,追加,查找,删除等)
1. 合并数组 array_merge()函数将数组合并到一起,返回一个联合的数组.所得到的数组以第一个输入数组参数开始,按后面数组参数出现的顺序依次迫加.其形式为: array array_merg ...
- linux平台及windows平台mysql重启方法
各个平台mysql 重启: inux平台及windows平台mysql重启方法 Linux下重启MySQL的正确方法: 1.通过rpm包安装的MySQL service mysqld restart ...
- linux下 stat statfs 获取 文件 磁盘 信息
stat函数讲解 表头文件: #include <sys/stat.h> #include <unistd.h> 定义函数: int st ...
- pandas的Series
pandas.Series(data=None, index=None, dtype=None, name=None, copy=False, fastpath=False) 首先介绍一下基本的: d ...