码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 4039 纸老虎类型

时间:2014-09-18 00:40:22      阅读:298      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   io   os   java   ar   strong   

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 }

 

HDU 4039 纸老虎类型

标签:des   style   blog   color   io   os   java   ar   strong   

原文地址:http://www.cnblogs.com/qq1012662902/p/3978342.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!