hdu 4039 The Social Network
2011-09-11 18:15
411 查看
这题党开始纯用STL写的,我写了个大的测试数据,果断TLE啊。
然后YY了会儿,觉得是不是可以用图来做。
就转化为图。在纸上画了画,呀觉得可解。
A 的间接关系人 就是 A 的直接关系人的( 直接关系人 && 和A不是直接关系的人 )
所以只要遍历A的直接关系人,然后这些搜他们的直接关系人,搜到一个就++(找共同好友数),取个数最大的。
注意不要取到A 以及A的直接关系人即可。
再次感叹map的强大。
本来想搜过的存起来下次在询问的话直接输出,发现得存好多名字啊,懒得弄,才1000查询,就存了没有可推荐的,直接输出 “-”。1Y~
然后YY了会儿,觉得是不是可以用图来做。
就转化为图。在纸上画了画,呀觉得可解。
A 的间接关系人 就是 A 的直接关系人的( 直接关系人 && 和A不是直接关系的人 )
所以只要遍历A的直接关系人,然后这些搜他们的直接关系人,搜到一个就++(找共同好友数),取个数最大的。
注意不要取到A 以及A的直接关系人即可。
再次感叹map的强大。
本来想搜过的存起来下次在询问的话直接输出,发现得存好多名字啊,懒得弄,才1000查询,就存了没有可推荐的,直接输出 “-”。1Y~
#include <set> #include <map> #include <queue> #include <stack> #include <math.h> #include <stdio.h> #include <stdlib.h> #include <iostream> #include <limits.h> #include <string.h> #include <string> #include <algorithm> #define MID(x,y) ( ( x + y ) >> 1 ) #define L(x) ( x << 1 ) #define R(x) ( x << 1 | 1 ) #define FOR(i,s,t) for(int i=s; i<t; i++) #define BUG puts("here!!!") #define STOP system("pause") #define file_r(x) freopen(x, "r", stdin) #define file_w(x) freopen(x, "w", stdout) using namespace std; const int MAX = 2010; map< string, int > m; map< string, int >::iterator it; struct NODE{ int to; NODE *next;}; NODE *p[MAX], node[MAX*MAX]; int num[MAX]; int cou = 0; void init() { memset(p, 0, sizeof(p)); cou = 0; } void Add(int u,int v) { node[cou].to = v; node[cou].next = p[u]; p[u] = &node[cou++]; } bool ans[MAX]; bool b[MAX]; string sss[MAX]; string soo[MAX]; int main() { int ncases, n, q, u, v, ind = 1; char s1[MAX], s2[MAX]; string ss; scanf("%d", &ncases); while( ncases-- ) { m.clear(); init(); memset(ans, false, sizeof(ans)); scanf("%d%d", &n, &q); int cnt = 0; while( n-- ) { scanf("%s %s", &s1, &s2); ss = string(s1); if( m.count(ss) ) // map 寻找,很方便呐 u = m.find(ss)->second; else { u = cnt; sss[cnt] = ss; m.insert(make_pair(ss, cnt++)); } ss = string(s2); if( m.count(ss) ) v = m.find(ss)->second; else { sss[cnt] = ss; v = cnt; m.insert(make_pair(ss, cnt++)); } Add(u, v); Add(v, u); } printf("Case %d:\n", ind++); while( q-- ) { scanf("%s", s1); ss = s1; if( !m.count(ss) ) continue; int now = m.find(ss)->second; if( ans[now] == true ) { printf("-\n"); continue; } memset(b, false, sizeof(b)); memset(num, 0, sizeof(num)); NODE *head = p[now]; while( head ) { b[head->to] = true; // now 的直接联系人,也可以用队列存啦 head = head->next; } FOR(i, 0, cnt) if( b[i] ) { NODE *head = p[i]; while( head ) { num[head->to]++; head = head->next; } } int mmax = 0; FOR(i, 0, cnt) if( num[i] > mmax && i != now && !b[i] )//注意不要自己和直接联系人 mmax = num[i]; if( mmax == 0 ) { ans[now] = true; // 下次再询问直接输出 - 即可 printf("-\n"); continue; } int nn = 0; FOR(i, 0, cnt) if( num[i] == mmax && !b[i] && i != now )//注意不要自己和直接联系人 soo[nn++] = sss[i]; sort(soo, soo+nn); // 输出sort。。 FOR(i, 0, nn) { if( i != 0 ) printf(" "); printf("%s",soo[i].c_str()); } printf("\n"); } } return 0; }
相关文章推荐
- hdu 4039 The Social Network bfs
- 成都赛区网选 HDU 4039 The Social Network(BFS)
- hdu 4039 The Social Network
- HDU - 4039 The Social Network (BFS)
- hdu 4039 暴力 The social network
- 【map+字典序】hdu 4039 The Social Network
- HDU 4039 The Social Network bfs
- 36th成都区域赛网络赛 hdoj4039 The Social Network(建图+字符串处理)
- 36th成都区域赛网络赛 hdoj4039 The Social Network(建图+字符串处理)
- HDU 4033 4036 4039 The 36th ACM/ICPC Asia Regional Chengdu Site —— Online Contest
- Maximizing the Spread of Influence through a Social Network
- HDU4039 The Social Network
- #Paper Reading# Link Prediction by De-anonymization: How We Won the Kaggle Social Network Challenge
- 对Google Me的社会化网络 “The Real Life Social Network” 翻译
- SDNU-BFS——E - The Social Network
- HDU4039 The Social Network(bfs)
- 2011成都网赛 / 1009 - The Social Network
- The readings, filtered by social network!
- 2011成都网赛 / 1009 - The Social Network
- 八一八 The Social Network的小细节