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做出来的,具体 ...
随机推荐
- 20145230《java学习笔记》第九周学习总结
20145230 <Java程序设计>第9周学习总结 教材学习内容 JDBC JDBC简介 JDBC是用于执行SQL的解决方案,开发人员使用JDBC的标准接口,数据库厂商则对接口进行操作, ...
- 在.h和.cpp中包含头文件的区别
1.在.h中包含头文件,是为了声明一系列这个头文件的变量等,可能会产生重复包含的问题: 2.在.cpp中包含头文件只是为了实现这个头文件或者使用其中的方法,不会有重复包含的问题,所以尽量在源文件中包含 ...
- 吴恩达深度学习笔记(九) —— FaceNet
主要内容: 一.FaceNet人脸识别简介 二.使用神经网络对人脸进行编码 三.代价函数triple loss 四.人脸库 五.人脸认证与人脸识别 一.FaceNet简介 1.FaceNet是一个深层 ...
- Spring之rmi实例演示
环境介绍:本文中服务端客户端使用的都是ssm框架,配置文件分为spring_servlet.xml,spring_service.xml,mybatis.xml 在spring里面使用rmi完成远程调 ...
- MySql基础学习-Sql约束
1.主键约束(PRIMARY KEY) 主键 (PRIMARY KEY)是用于约束表中的一行,作为这一行的唯一标识符,在一张表中通过主键就能准确定位到一行,因此主键十分重要.主键不能有重复且不能为空. ...
- 第一个Python程序hello.py提示出现File "<stdin>",line 1错误
写第一个Python程序hello.py,内容仅有一句,print 'hello world', 运行 Python hello.py 出错,提示: File "<stdin>& ...
- Maven基础配置
重要网址 Maven主页:http://maven.apache.org/ Maven central repository:http://search.maven.org/ Maven aliyun ...
- 机器学习(七)—Adaboost 和 梯度提升树GBDT
1.Adaboost算法原理,优缺点: 理论上任何学习器都可以用于Adaboost.但一般来说,使用最广泛的Adaboost弱学习器是决策树和神经网络.对于决策树,Adaboost分类用了CART分类 ...
- Jfinal整合百度富文本编辑器ueditor
ueditor配置文件ueditor.config.js修改参数serverUrl:(改为要调用的action) 后台代码 package com.sandu.mega.admin.ueditor; ...
- WebView的简单使用
activity_main.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...