HDU 4039 BFS

/****************************************************************************************************
    只能用坑爹来形容。。。PE能judge成WA。。。
****************************************************************************************************/
#include <iostream>
#include <functional>
#include <algorithm>
//#include <hash_map>		//Visual C++ lib
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <sstream>
#include <utility>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <limits>
#include <memory>
#include <string>
#include <vector>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
using namespace std;

#define LOWBIT(x) ( (x) & ( (x) ^ ( (x) - 1 ) ) )
#define CLR(x, k) memset((x), (k), sizeof(x))
#define CPY(t, s) memcpy((t), (s), sizeof(s))
#define SC(t, s) static_cast<t>(s)
#define LEN(s) static_cast<int>( strlen((s)) )
#define SZ(s) static_cast<int>( (s).size() )

typedef double LF;
//typedef long long LL;		//GNU C++
//typedef unsigned long long ULL;
typedef __int64 LL;		//Visual C++ 2010
typedef unsigned __int64 ULL;

typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<double, double> PDD;

//typedef hash_map<int, int>::iterator HMI;
typedef map<int, int>::iterator MI;
typedef vector<int>::iterator VI;
typedef list<int>::iterator LI;
typedef set<int>::iterator SI;

template <typename T>
T sqa(const T &x)
{
	return x * x;
}
template <typename T>
T gcd(const T &a, const T &b)
{
	if (!a || !b)
	{
		return max(a, b);
	}
	T t;
	while (t = a % b)
	{
		a = b;
		b = t;
	}
	return b;
}
template <typename T>
T ext_gcd(T a, T b, T &x, T &y)
{
	if (!b)
	{
		x = 1;
		y = 0;
		return a;
	}
	T d = ext_gcd(b, a % b, x, y);
	T t = x;
	x = y;
	y = t - a / b * y;
	return d;
}

const int INF_INT = 0x3f3f3f3f;
const LL INF_LL = 0x7fffffffffffffffLL;		//15f
const double oo = 10e9;
const double eps = 10e-9;
const double PI = acos(-1.0);

const int MAXN = 2004;
const int MAXE = MAXN << 1;
const int MAXL = 100;
const int MAXC = 26;

int test, n, m;
struct Node
{
	Node *next[MAXC];
	int id;
}node[MAXN * MAXL], * const root = node;
int ncnt;
struct Edges
{
	int end;
	int next;
}edges[MAXE];
int ecnt, head[MAXN];
string name[MAXN];
int icnt;
int vcnt[MAXN];
int stop, S[MAXN];
string ans[MAXN];
bool vis[MAXN];

void initTrie()
{
	ncnt = 1;
	for (int i = 0; i < MAXC; ++i)
	{
		root->next[i] = NULL;
	}
	root->id = -1;
	return ;
}
Node * createNode()
{
	Node *ptr = &node[ncnt++];
	for (int i = 0; i < MAXC; ++i)
	{
		ptr->next[i] = NULL;
	}
	ptr->id = -1;
	return ptr;
}
int find(const string &ss)
{
	int buf;
	Node *ptr = root;
	for (int k = 0; k < SC(int, ss.length()); ++k)
	{
		buf = ss[k] - 'a';
		if (NULL == ptr->next[buf])
		{
			return -1;
		}
		ptr = ptr->next[buf];
	}
	return ptr->id;
}
void insert(const string &ss, const int &_id)
{
	int buf;
	Node *ptr = root;
	for (int k = 0; k < SC(int, ss.length()); ++k)
	{
		buf = ss[k] - 'a';
		if (NULL == ptr->next[buf])
		{
			ptr->next[buf] = createNode();
		}
		ptr = ptr->next[buf];
	}
	ptr->id = _id;
	return ;
}
void add_edge(int u, int v)
{
	edges[ecnt].end = v;
	edges[ecnt].next = head[u];
	head[u] = ecnt++;
	return ;
}
bool bfs(const int &u)
{
	CLR(vcnt, 0);
	CLR(vis, false);
	int v, w;
	stop = 0;
	int maxi = 0;
	vis[u] = true;
	for (int i = head[u]; i != -1; i = edges[i].next)
	{
		v = edges[i].end;
		vis[v] = true;
	}
	for (int i = head[u]; i != -1; i = edges[i].next)
	{
		v = edges[i].end;
		for (int j = head[v]; j != -1; j = edges[j].next)
		{
			w = edges[j].end;
			if (vis[w])
			{
				continue ;
			}
			++vcnt[w];
			if (vcnt[w] == maxi)
			{
				S[stop++] = w;
			}
			else
			{
				if (vcnt[w] > maxi)
				{
					maxi = vcnt[w];
					stop = 0;
					S[stop++] = w;
				}
			}
		}
	}
	return stop != 0;
}
bool cmp(const string &sa, const string &sb)
{
	return sa < sb;
}
bool _cmp(const int &a, const int &b)
{
	return name[a] < name[b];
}
void ace()
{
	int cas = 1;
	string s1, s2;
	int u, v;
	for (cin >> test; test--; ++cas)
	{
		cin >> n >> m;
		ecnt = 0;
		CLR(head, -1);
		initTrie();
		icnt = 0;
		for (int i = 0; i < n; ++i)
		{
			cin >> s1 >> s2;
			u = find(s1);
			if (-1 == u)
			{
				name[icnt] = s1;
				insert(s1, icnt);
				u = icnt++;
			}
			if (s1 == s2)
			{
				continue ;
			}
			v = find(s2);
			if (-1 == v)
			{
				name[icnt] = s2;
				insert(s2, icnt);
				v = icnt++;
			}
			if (u != v)
			{
				add_edge(u, v);
				add_edge(v, u);
			}
		}
		cout << "Case " << cas << ":" << endl;
		while (m--)
		{
			cin >> s1;
			u = find(s1);
			if (-1 == u)
			{
				puts("-");
				continue ;
			}
			if (!bfs(u))
			{
				puts("-");
				continue ;
			}
			for (int i = 0; i < stop; ++i)
			{
				ans[i] = name[ S[i] ];
			}
			sort(ans, ans + stop, cmp);
			cout << ans[0];
			for (int i = 1; i < stop; ++i)
			{
				cout << " " << ans[i];
			}
			cout << endl;
		}
	}
	return ;
}
int main()
{
	ace();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值