UVa 10044 - Erdos Numbers

題目:Erdos是個偉大的數學家(不知道的話,自行百度吧),他署名的論文有1000多份,

於是數學及澳門定義了一個Erdos數的概念,直接和Erdos發表論文的記為1,

和Erdos為1的人共同發表論文的人記為2,依次類推,現在已知一些論文和作者,

求對應的Erdos數。

分析:圖論,最短路。

首先,按照要求,取出所有的人名,利用map映射到唯一的ID;

然後,同一偏論文的作者,相互連邊,利用bfs求最短路;

最后,對每個查詢的節點取深度信息即可。

說明:圖大概有10000個點,1000000條邊,數值開小會RE或者SF。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <queue>
#include <map>

using namespace std;

//邻接表
typedef struct _linklist
{
    int        point;
    _linklist* next;
}linklist ;
linklist* link_head[10001];
linklist  link_node[1000001];
int       link_size;

void link_initial()
{
    memset(link_head, 0, sizeof(link_head));
    memset(link_node, 0, sizeof(link_node));
    link_size = 0;
} 

void link_add(int a, int b)
{
    link_node[link_size].point = b;
    link_node[link_size].next = link_head[a];
    link_head[a] = &link_node[link_size ++];
}
//邻接表 end 

int  deep[10001];
void bfs(int s, int n)
{
	for (int i = 0; i < n; ++ i)
		deep[i] = -1;
	deep[s] = 0;
	queue<int>Q;

	Q.push(s);
	while (!Q.empty()) {
		int now = Q.front(); Q.pop();
		for (linklist *p = link_head[now]; p; p = p->next) {
			if (deep[p->point] == -1) {
				deep[p->point] = deep[now] + 1;
				Q.push(p->point);
			}
		}
	}
}

string buf, name;
int    save[10001];

int main()
{
	int T,n,m;
	cin >> T;
	for (int t = 1; t <= T; ++ t) {
		cin >> n >> m;
		cin.ignore();
		int count = 0;
		map <string, int>Map;
		link_initial();
		for (int i = 0; i < n; ++ i) {
			getline(cin, buf);
			int strs = 0, stre = 0, size = 0;
			while (buf[strs] != ':') {
				stre = strs;
				while (buf[stre] != ',' && buf[stre] != ':' || buf[stre-1] != '.')
					++ stre;
				string str = buf.substr(strs, stre-strs);
				if (!Map.count(str))
					Map.insert(pair<string, int>(str, count ++));
				save[size ++] = Map[str];
				strs = stre;
				while (buf[strs] < 'A' || buf[strs] > 'Z')
					if (buf[strs] != ':') ++ strs;
					else break;
			}
			for (int j = 0; j < size; ++ j)
				for (int k = 0; k < j; ++ k)
					if (j != k) {
						link_add(save[j], save[k]);
						link_add(save[k], save[j]);
					}
		}

		bfs(Map["Erdos, P."], count);
		cout << "Scenario " << t << endl;
		for (int i = 0; i < m; ++ i) {
			getline(cin, name);
			if (!Map.count(name) || deep[Map[name]] == -1)
				cout << name << " infinity" << endl;
			else
				cout << name << " " << deep[Map[name]] << endl;
		}
	}
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-07 06:30:03

UVa 10044 - Erdos Numbers的相关文章

UVA - 12046 Great Numbers

Description Problem G - Great Numbers In this problem you have to count the number of great numbers of length n. Here a great number must have the following property: the number must be divisible by all of its decimal digits. it does not contain any

UVA - 471 Magic Numbers

Description  Magic Numbers  Write a program that finds and displays all pairs ofintegers and such that: neither nor have any digits repeated; and , where N is a given integer; Input and Output The input file consist a integer at the beginning indicat

Uva - 12050 Palindrome Numbers【数论】

题目链接:uva 12050 - Palindrome Numbers 题意:求第n个回文串 思路:首先可以知道的是长度为k的回文串个数有9*10^(k-1),那么依次计算,得出n是长度为多少的串,然后就得到是长度为多少的第几个的回文串了,有个细节注意的是, n计算完后要-1! 下面给出AC代码: 1 #include <bits/stdc++.h> 2 typedef long long ll; 3 using namespace std; 4 const int maxn=3010; 5

UVA 136 Ugly Numbers

原题代号:UVA 136 原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=72 题目原题: Ugly Numbers Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence1, 2, 3, 4, 5,

UVa 136 Ugly Numbers(优先队列)

题意  质因数只可能有2,3,5的数称为丑数  输出第1500个丑数 STL优队列应用  1是丑数 丑数的2,3,5倍都是丑数  用优先队列模拟就行了 #include <cstdio> #include <cstring> #include <set> #include <queue> using namespace std; typedef long long ll; //priority_queue<ll, vector<ll>, g

UVA 10042 Smith Numbers(数论)

Smith Numbers Background While skimming his phone directory in 1982, Albert Wilansky, a mathematician of Lehigh University , noticed that the telephone number of his brother-in-law H. Smith had the following peculiar property: The sum of the digits o

UVA 11461 Square Numbers解题报告

Discription A square number is an integer number whose square root is also an integer. For example 1, 4, 81 are some square numbers. Given two numbers a and b you will have to find out how many square numbers are there between a and b (inclusive). In

UVA - 138 - Street Numbers (简单数论)

题目传送:UVA - 138 思路:题意好难懂,看了半天,还是搜了题解才搞懂题意,真是给英语跪啦 m在家的位置,而n是最后一个位置,直接输出前10组满足1~m-1的和 == m+1 ~ n的和,这是题意: 然后通过题意可以得到n*(n+1)/2 - m - m*(m-1)/2 = m*(m-1)/2; 化简可得2*m*m = n*(n+1); 然后可以通过枚举n来求得m,看m是否为整数(可由double到int判断),是整数则满足情况 打表代码: #include <cstdio> #incl

UVa 10006 Carmichael Numbers (快速幂 + 素性测试)

Carmichael Numbers Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description  Carmichael Numbers  An important topic nowadays in computer science is cryptography. Some people even think that cryptography is t