###### hdu 4034 预处理+map映射字符串

#include<cstdio>
/*1.用map把人名字和序号对应
2.用gra[i][j]存储i和j的相同人个数
3.进来一个relation a-b,把b到a和有关系的那些人，如果b和那些人没关系就+1 ,把a和b到有关系的那些人都+1
真正做的时候如果发现a和b有关系，显然不用推荐。
复杂度1000*1000log1000 * T 勉强可以吧
*/
#include<algorithm>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn = 1500;
map<int,string> m1;
map<string,int> m2;
bool gra[maxn][maxn];
int com[maxn][maxn];
map<string,int> ::iterator itor;
using namespace std;
set<string> s;
set<string> ::iterator itors;
int main()
{
int T;
scanf("%d",&T);
for(int test = 1; test <= T; test++){
int n,q;
memset(gra,0,sizeof(gra));
memset(com,0,sizeof(com));
m1.clear();
m2.clear();
scanf("%d %d",&n,&q);
int cnt = 0;
for(int i = 1; i <= n; i++){
string s1,s2;
cin >> s1 >> s2;
//cout << s1 << s2;
itor = m2.find(s1);

if (itor == m2.end()){
m2[s1] = cnt;

m1[cnt] = s1;
cnt++;
}

itor = m2.find(s2);
if (itor == m2.end()){
m2[s2] = cnt;
m1[cnt] = s2;
cnt++;
}
int to = m2[s1];
int from = m2[s2];
gra[from][to] = 1;
gra[to][from] = 1;
for(int j = 0; j < cnt; j++)if (j != from){
if (gra[j][to]){
com[j][from]++;
com[from][j]++;
}
}
for(int j = 0; j < cnt; j++)if (j != to){
if (gra[j][from]){
com[j][to]++;
com[to][j]++;
}
}
}
s.clear();
printf("Case %d:\n",test);
for(int i = 0; i < q; i++){
//printf("q:\n");
s.clear();
string s1;
cin >> s1;
int ans = 0;
int from = m2[s1];
for(int j = 0; j < cnt; j++){
if (j != from && !gra[from][j] && com[from][j] > ans){
ans = com[from][j];
}
}
//printf("ans:\n");
if (ans == 0) printf("-\n");
else{
for(int j = 0; j < cnt; j++){
if (j != from && !gra[from][j] && com[from][j] == ans){
s.insert(m1[j]);
}
}
for(itors = s.begin(); itors != s.end(); itors++){
if (itors != s.begin()) printf(" ");
cout << *itors;
}
printf("\n");
}
}
}
return 0;
}

#### hdu 4034 Graph(深化最短路floyd)

2015-11-24 20:55:29

#### hdu 4034

2011-09-16 19:56:09

#### HDU 4034 Graph

2015-10-26 17:07:43

#### map映射 【字符串&整型】

2015-11-12 20:18:24

#### POJ 2986 A Triangle and a Circle（计算几何）

2015-08-05 10:43:07

#### STL(map和二维map/hdu1263)

2016-03-17 18:44:37

#### 我怎样构造比较字符串的#if 预处理表达式？

2014-06-06 15:47:30

#### 【骗自己是日更】开始dp习题啦。

2017-12-02 12:58:19

#### map映射容器应用-数字和字符转换

2014-06-21 16:30:24

#### 黑马程序员---Java中的映射Map

2014-06-24 23:50:58

## 不良信息举报

hdu 4034 预处理+map映射字符串