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做出来的,具体 ...
随机推荐
- [Android]开源中国源码分析之一---启动界面
开源中国android端版本号:2.4 启动界面: 在AndroidManifest.xml中找到程序的入口, <activity android:name=".AppStart&qu ...
- 加和求不同的组合方式数目(dp)
描述 有n个正整数,找出其中和为t(t也是正整数)的可能的组合方式.如: n=5,5个数分别为1,2,3,4,5,t=5: 那么可能的组合有5=1+4和5=2+3和5=5三种组合方式. 输入 输入的第 ...
- linux基础(8)-文件处理(awk 、sed、grep)
grep基本用法 格式:grep [选项] [模式] [文件] 选项: -c:只显示有多少行匹配 ,而不具体显示匹配的行 -n:在每一行前面打印该行在文件中的行数 -i:在字符串比较的时候忽略大小 ...
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) D. Generating Sets 贪心+优先队列
D. Generating Sets time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- MySQL随机取数据
// 随机取9个 $rand_sql = "SELECT * FROM `tf_product` WHERE (`id` >= ((SELECT MAX(`id`) FROM `tf_ ...
- JavaScript 获取输入时的光标位置及场景问题
前言 在输入编辑的业务场景中,可能会需要在光标当前的位置或附近显示提示选项.比如社交评论中的@user功能,要确保提示的用户列表总是出现在@字符右下方,又或者是在自定义编辑器中 autocomplet ...
- Model compatibility cannot be checked because the database does not contain model metadata. Ensure that IncludeMetadataConvention has been added to the DbModelBuilder conventions
Model compatibility cannot be checked because the database does not contain model metadata. Ensure t ...
- javascript测试框架mocha
node测试框架mocha 简单.灵活.有趣,mocha是一个功能丰富的javascript测试框架,运行在node和浏览器中,使异步测试变得更加简单有趣.http://mochajs.org/ 安装 ...
- 去掉标题栏的方法(使用requestWindowFeature(Window.FEATURE_NO_TITLE);为什么失效?不仅失效,可能会挂)
使用requestWindowFeature(Window.FEATURE_NO_TITLE)隐藏标题栏失效的原因,不仅失效,可能会挂.可能是activity继承的是AppCompatActivity ...
- GIT和SVN比较
SVN与Git比较 摘要Svn是目前得到大多数人认可,使用得最多的版本控制管理工具,而Git的优势在于易于本地增加分支和分布式的特性,可离线提交,解决了异地团队协同开发等svn不能解决的问题.本文就这 ...