hdu 4039 The Social Network

这题党开始纯用STL写的,我写了个大的测试数据,果断TLE啊。

然后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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值