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 ;
}
 

最新文章

  1. 网络爬虫:使用Scrapy框架编写一个抓取书籍信息的爬虫服务
  2. python爬虫学习(1) —— 从urllib说起
  3. Javascript学习笔记2.1 Javascript与DOM简介
  4. PSP记录个人项目耗时
  5. 三星笔记本预装WIN8_降级WIN7方法
  6. 如何修改ECSHOP后台管理中心的Title信息
  7. Spring中MultipartHttpServletRequest实现文件上传
  8. hdu 3622 二分+2-SAT判定
  9. Update msi using vbscript
  10. [访问系统] Api_Win32_Mac类工具包 (转载)
  11. Linq中的多表左联,详细语句
  12. 一句代码美化你的下框之jquery.selectMM修复版(jquery.selectMM v0.9 beta 20141217)
  13. 基于存储过程的MVC开源分页控件
  14. [HMLY]11.iOS函数式编程的实现&amp;&amp;响应式编程概念
  15. html的特点及结构
  16. POJ2248-Addition Chains
  17. ubuntu14.04, Cloudera Manager 5.11.1, cdh5.11.1 postgresql离线部署
  18. 四则运算法则在Java中的实现
  19. 判断Javascript变量是否为空 undefined 或者null(附样例)
  20. 垃圾回收GC3种算法的衍生品 增量回收:预测和控制GC所产生的中断时间

热门文章

  1. JavaWeb CSS
  2. 全志H3-NanoPi开发板SDK之三编译流程【转】
  3. 百度竞价推广URL通配符使用说明
  4. shell 计算文件交并差
  5. window7 共享wifi(不通过wifi软件)
  6. MapReduce-多个输出(使用MultipleOutput,不指定reduce任务个数)
  7. 拷贝struts2项目时,运行后启动的是拷贝前的项目
  8. SQL必知必会 记录
  9. Generator函数介绍
  10. mindmanager思维导图软件