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 预处理+map映射字符串 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)