hdu 4039 - 杂-1

12 篇文章 0 订阅

http://acm.hdu.edu.cn/showproblem.php?pid=4039
The 36th ACM/ICPC Asia Regional Chengdu Site —— Online Contest
1009

题意:模拟一个社交网络系统,先给你n个朋友关系,对每个人,系统会推荐给他一些人给他交朋友,系统推荐的是他朋友的朋友,而且只推荐他最多个朋友的朋友,如果会推荐多个,按照字典序从小到大输出,如果一个也不推荐输出“-”。。。1 ≤ N, Q ≤ 1000

分析:由于数据量小,直接暴力即可,找他所有的朋友的朋友只需要n^2即可,二重for循环搞定,为节省时间,用邻接表存,因为每个人是以名字形式给出的,用字典树做一个映射。。。注意一个小地方,就是a的朋友的朋友可能是a的朋友。。。

代码:其实就是一水题,只是rank排在前面了,贴出来玩玩。。

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#include <iostream>
using namespace std;

const int N=2010;
int n, q, up, num;
char s[20], s1[20];
struct trie
{
	int a[26];
	int num;
	void init()
	{
		memset(a, -1, sizeof(a));
		num = -1;
	}
} t[10000];
vector<int> a[N];
vector<int>::iterator it, it1;
int cnt[N], flag[N], flag1;
struct node
{
	int i;
	char s[16];
} ss[N], ss1[N];

inline int insert(char *s)
{
	int p=0;
	while(*s)
	{
		if(t[p].a[*s-'a']==-1)
		{
			t[up].init();
			t[p].a[*s-'a'] = up++;
		}
		p = t[p].a[*s-'a'];
		s++;
	}
	if(t[p].num==-1)
	{
		t[p].num = num++;
		flag1 = 1;
	}
	return t[p].num;
}
inline int query(char *s)
{
	int p=0;
	while(*s)
	{
		if(t[p].a[*s-'a']==-1)
			return -1;
		p = t[p].a[*s-'a'];
		s++;
	}
	return t[p].num;
}
int cmp(const node &a, const node &b)
{
	return strcmp(a.s, b.s)<0;
}

int main()
{
	int i, j, cas, cas1, x, y, mx;
	
	scanf("%d", &cas);
	for(cas1=1; cas1<=cas; cas1++)
	{
		scanf("%d%d%d", &n, &q);
		t[0].init();
		up = num = 1;
		for(i=1; i<=n; i++)
		{
			scanf("%s %s", s, s1);
			flag1 = 0;
			x = insert(s);
			if(flag1==1)
				strcpy(ss[x].s, s);
			flag1 = 0;
			y = insert(s1);
			if(flag1==1)
				strcpy(ss[y].s, s1);
			a[x].push_back(y);
			a[y].push_back(x);
		}
		printf("Case %d:\n", cas1);
		while(q--)
		{
			scanf("%s", s);
			for(i=1; i<num; i++)
				cnt[i] = 0;
			x = query(s);
			mx = 0;
			for(i=1; i<num; i++)
				flag[i] = 0;
			flag[x] = 1;
			for(it=a[x].begin(); it!=a[x].end(); it++)
				flag[*it] = 1;
			for(it=a[x].begin(); it!=a[x].end(); it++)
			{
				y = *it;
				for(it1=a[y].begin(); it1!=a[y].end(); it1++)
				{
					if(flag[*it1]==0)
					{
						cnt[*it1]++;
						if(cnt[*it1]>mx)
							mx = cnt[*it1];
					}
				}
			}
			if(mx==0)
			{
				printf("-\n");
				continue;
			}
			up = 0;
			for(i=1; i<num; i++)
			{
				if(cnt[i]==mx)
				{
					strcpy(ss1[up].s, ss[i].s);
					up++;
				}
			}
			sort(ss1, ss1+up, cmp);

			printf("%s", ss1[0].s);
			for(i=1; i<up; i++)
				printf(" %s", ss1[i].s);
			printf("\n");
		}
		
		for(i=1; i<num; i++)
			while(!a[i].empty())
				a[i].pop_back();
	}

	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值