HDU 4039 纸老虎类型

The Social Network

Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 2593    Accepted Submission(s): 742

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分别表示关系数量,询问数量。   下面n行为两个字符串s1, s2代表s1与s2是朋友,下面Q行询问。每行一个s1,找出不是s1且与s1不是朋友且与s1有公共朋友的人,输出和s1公共朋友最多的人,若有多个符合条件,按字典序输出。

思路:

一看输入输出感觉这道题很难的样子。。。再一看题目意思和题目时限。。水题。。

时限这么长,直接按照题目意思暴力一下就行了。

代码;

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <string>
 4 #include <iostream>
 5 #include <algorithm>
 6 #include <vector>
 7 #include <map>
 8 #include <queue>
 9 using namespace std;
10
11 int di[2005][2005];
12 map<string,int>ma;
13 map<int,string>mb;
14 vector<int>ve[2005];
15 int visited[2005];
16 int n, Q;
17
18 bool cmp(char s1[],char s2[]){
19     return strcmp(s1,s2)>0;
20 }
21
22 main()
23 {
24     int i, j, k;
25     int t, kase=1;
26     cin>>t;
27     char c1[20], c2[20];
28     while(t--){
29         ma.clear();mb.clear();
30         for(i=0;i<n*2+5;i++) ve[i].clear();
31
32         scanf("%d %d",&n,&Q);
33         k=0;
34         int x, y;
35         memset(di,0,sizeof(di));
36         for(i=0;i<n;i++){
37             scanf("%s%s",c1,c2);
38             if(ma.find(c1)!=ma.end()) x=ma[c1];
39             else
40             ma[c1]=k++,x=ma[c1],mb[x]=c1;
41             if(ma.find(c2)!=ma.end()) y=ma[c2];
42             else
43             ma[c2]=k++,y=ma[c2],mb[y]=c2;
44             ve[x].push_back(y);
45             ve[y].push_back(x);
46             di[x][y]=di[y][x]=1;
47         }
48         int f;
49         int num[2005];
50         int id=0;
51         string ss[2005];
52         int maxh, z;
53         printf("Case %d:\n",kase++);
54         while(Q--){
55             scanf("%s",c1);
56             id=0;
57             memset(num,0,sizeof(num));
58             x=ma[c1];
59             f=0;
60             maxh=-1;
61             for(i=0;i<ve[x].size();i++){
62                 y=ve[x][i];
63                 for(j=0;j<ve[y].size();j++){
64                     z=ve[y][j];
65                     if(z!=x&&!di[z][x]){
66                         num[z]++;f=1;
67                         maxh=max(maxh,num[z]);
68                     }
69                 }
70             }
71             if(!f) {
72                 printf("-\n");continue;
73             }
74             for(i=0;i<k;i++) {
75                 if(num[i]==maxh){
76                     ss[id++]=mb[i];
77                 }
78             }
79             std::sort(ss,ss+id);
80             cout<<ss[0];
81             for(i=1;i<id;i++) cout<<" "<<ss[i];
82             cout<<endl;
83         }
84     }
85 }
时间: 2024-10-27 12:26:10

HDU 4039 纸老虎类型的相关文章

HDU 4039 The Social Network bfs

算了下复杂度好像是n^3 就感觉不大好做.结果n^31a... #include <cstdio> #include <algorithm> #include <iostream> #include <string.h> #include <map> #include <vector> #include <string> #include <queue> using namespace std; #define

【map+字典序】hdu 4039 The Social Network

[map+字典序]hdu 4039 The Social Network 题目链接:hdu 4039 The Social Network 题目大意 模拟一个社交网络系统,先给你n个朋友关系,对每个人,系统会推荐给他一些人给他交朋友,系统推荐的是:他朋友的朋友且与他不是朋友,而且只推荐他最多共同朋友的朋友,如果会推荐多个,按照字典序从小到大输出,如果一个也不推荐输出"-" 熟练map映射:字典序本身map容器里的关键字就是按照字典序存储的,所以最后只要遍历一遍容器,把满足题意的字符串输

hdu 4039

#include <stdio.h> #include <map> #include <string> #include <iostream> #include <queue> #include <set> using namespace std; #define MAX 1500 bool path[MAX][MAX]; bool num[MAX]; map<string, int> list; map<int ,

HDU 1853--Cyclic Tour【最小费用最大流 &amp;&amp; 有向环最小权值覆盖 】

Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others) Total Submission(s): 1950    Accepted Submission(s): 984 Problem Description There are N cities in our country, and M one-way roads connecting them. Now L

推荐朋友 - LintCode

拼多多笔试第三题 除了题目具体方法值得注意外,数据的输入格外注意 题目 描述 给n个人的朋友名单,告诉你user,请找出user最可能认识的人.(他和user有最多的共同好友且他不是user的朋友) n <= 500. 好友关系是相互的.(b若出现在a的好友名单中,a一定出现在b的好友名单中) 每个人的好友关系不超过 m 条,m <= 3000. 如果有两个人和user的共同好友数目一样,编号更小的那个认为是最可能认识的人. 如果user和所有陌生人都没有共同好友,输出-1. 样例 给出 li

hdu 1176 免费馅饼(数塔类型)

http://acm.hdu.edu.cn/showproblem.php?pid=1176 免费馅饼 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 33362    Accepted Submission(s): 11410 Problem Description 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上

hdu 1176 免费馅饼 (dp 数塔类型)

免费馅饼 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 25712    Accepted Submission(s): 8760 Problem Description 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy的人品实在是太好了,这馅饼别处都不掉,就掉落在他身旁的1

hdu 2987最大权闭合图模板类型题

/* 最大权闭合图模板类型的题,考验对知识概念的理解. 题意:现在要辞退一部分员工,辞退每一个员工可以的到一部分利益(可以是负的),并且辞退员工,必须辞退他的下属,求最大利益和辞退的最小人数. 最大权闭合图模板类型. 求出最大权后沿着源点s,dfs到的点就为最小的人数. 证明/* 转载:利用一个经典的trick:多关键字 > 建图前,对所有b[i],执行变换b[i]=b[i]*10000-1,然后,会惊异地发现, > 此时最大流所对应的方案就是满足辞退最少人数的了. > 为什么?显然,变

HDU 4810 这道题 是属于什么类型?

统计每一位出现1的个数  求组合数 直接贴代码 #include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <cstdlib> #include <ctime> #include <string> #define CL(a,b) memset(a,b,sizeof(a)) #define INF 0x3fffffff