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应用)的更多相关文章

  1. mapreduce中一个map多个输入路径

    package duogemap; import java.io.IOException; import java.util.ArrayList; import java.util.List; imp ...

  2. .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法

    .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法 0x00 为什么需要Map(MapWhen)扩展 如果业务逻辑比较简单的话,一条主管道就够了,确实用不到 ...

  3. Java基础Map接口+Collections工具类

    1.Map中我们主要讲两个接口 HashMap  与   LinkedHashMap (1)其中LinkedHashMap是有序的  怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...

  4. Java基础Map接口+Collections

    1.Map中我们主要讲两个接口 HashMap  与   LinkedHashMap (1)其中LinkedHashMap是有序的  怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...

  5. 多用多学之Java中的Set,List,Map

            很长时间以来一直代码中用的比较多的数据列表主要是List,而且都是ArrayList,感觉有这个玩意就够了.ArrayList是用于实现动态数组的包装工具类,这样写代码的时候就可以拉进 ...

  6. Java版本:识别Json字符串并分隔成Map集合

    前言: 最近又看了点Java的知识,于是想着把CYQ.Data V5迁移到Java版本. 过程发现坑很多,理论上看大部分很相似,实践上代码写起来发现大部分都要重新思考方案. 遇到的C#转Java的一些 ...

  7. MapReduce剖析笔记之八: Map输出数据的处理类MapOutputBuffer分析

    在上一节我们分析了Child子进程启动,处理Map.Reduce任务的主要过程,但对于一些细节没有分析,这一节主要对MapOutputBuffer这个关键类进行分析. MapOutputBuffer顾 ...

  8. MapReduce剖析笔记之七:Child子进程处理Map和Reduce任务的主要流程

    在上一节我们分析了TaskTracker如何对JobTracker分配过来的任务进行初始化,并创建各类JVM启动所需的信息,最终创建JVM的整个过程,本节我们继续来看,JVM启动后,执行的是Child ...

  9. MapReduce剖析笔记之五:Map与Reduce任务分配过程

    在上一节分析了TaskTracker和JobTracker之间通过周期的心跳消息获取任务分配结果的过程.中间留了一个问题,就是任务到底是怎么分配的.任务的分配自然是由JobTracker做出来的,具体 ...

随机推荐

  1. 为多个文件夹下的C源代码编写Makefile文件

    上一篇文章写了如何为在同一个文件夹下的C源代码,本篇文章为多个文件夹下的C源代码编写Makefile文件. 建立两个文件夹,分别为abs与src.其最终目录结构如下: 1 $ ls * 2 jun.c ...

  2. Kubernetes busybox nslookup问题

    使用最新版本的busybox会出现nslookup提示无法解析的问题: Server: 10.96.0.10 Address: 10.96.0.10:53 ** server can't find k ...

  3. 【转载】用Scikit-Learn构建K-近邻算法,分类MNIST数据集

    原帖地址:https://www.jiqizhixin.com/articles/2018-04-03-5 K 近邻算法,简称 K-NN.在如今深度学习盛行的时代,这个经典的机器学习算法经常被轻视.本 ...

  4. Linux操作系统的安装以及基本的操作命令详解

    背景:使用的虚拟机安装Linux  虚拟机使用的是VMware  Linux版本:CentOS-6.7-X86    自行下载:CentOS-6.7-x86_64-bin-DVD1.iso 打开VMw ...

  5. css 相对绝对定位

    用Div+CSS进行网站布局时,做一些浮动层等特殊特殊效果时要考虑到定位问题.这就要用到Position属性等.Position属性有四个值:static.fixed.absolute和relativ ...

  6. python爬虫-url

    特此声明: 以下内容来源于博主:http://blog.csdn.net/pleasecallmewhy                                     http://cuiq ...

  7. Jboss remote getshell (JMXInvokerServlet) vc版

    #include "stdafx.h" #include <Windows.h> #include <stdio.h> #include <winht ...

  8. 51nod 1289 大鱼吃小鱼 栈

    1289 大鱼吃小鱼 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 有N条鱼每条鱼的位置及大小均不同,他们沿着X轴游动,有的向左,有的向右.游动的速度是一样的,两条 ...

  9. python之Django rest_framework总结

    一.rest api    a.api就是接口         如: - http://www.oldboyedu.com/get_user/                - http://www. ...

  10. poj 1011 :Sticks (dfs+剪枝)

    题意:给出n根小棒的长度stick[i],已知这n根小棒原本由若干根长度相同的长木棒(原棒)分解而来.求出原棒的最小可能长度. 思路:dfs+剪枝.蛮经典的题目,重点在于dfs剪枝的设计.先说先具体的 ...