programming-challenges Erdös Numbers (110206) 题解

祁建业
2023-12-01

主要是对各种输入字符串的解析,其实不是很喜欢,不过也算练了基本功了。关于输入的各种处理要注意的问题,网上都可以找到,就不详细记载了,反正是挺讨厌的,但是从积极的方面想,这样才是我们在真实世界中经常要处理的情况吧?后来发现一个自己一直没有认识到的错误,vector.resize()不会做reset,以前在vector中的值还是被保留了。另外学了一个新的reset vector的方法,fill(vector.begin(), vector.end(), 0); 

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <assert.h>
#include <algorithm>
#include <math.h>
#include <ctime>
#include <functional>
#include <string.h>
#include <stdio.h>
#include <numeric>
#include <float.h>

using namespace std;

const int DIMENTION = 10010; 
vector<int> G[DIMENTION]; 
vector<int> erdosNum(DIMENTION); 
int authorNum = 0;

void wfs(int node) {
	queue<int> que; 
	que.push(node); 
	vector<bool> visited(DIMENTION); 
	visited[node] = true;
	erdosNum[node] = 0; 

	while (!que.empty()) {
		int node = que.front(); que.pop();
		for (int i = 0; i < G[node].size(); i++) {
			if (!visited[G[node][i]]) {
				visited[G[node][i]] = true;
				erdosNum[G[node][i]] = erdosNum[node] + 1; 
				que.push(G[node][i]); 
			}
		}
	}
}

int main() {
	int TC; cin >> TC; 
	for (int tc = 1; tc <= TC; tc++) {
		// init
		for (int i = 0; i < DIMENTION; i++) {
			G[i].clear();
		}

		fill(erdosNum.begin(), erdosNum.end(), 0);
		authorNum = 0;

		int m, n; cin >> m >> n; getchar();
		map<string, int> strToNodes;
		vector<string> author(n);
		vector<string> paper(m); 
		 
		for (int i = 0; i < m; i++) getline(cin, paper[i]);
		for (int i = 0; i < n; i++) getline(cin, author[i]);

		// build graph
		for (int i = 0; i < m; i++) {
			string authors = paper[i].substr(0, paper[i].find_first_of(':'));
			vector<int> allNodes; 
			
			int pos = authors.rfind(".,"); 
			while (pos != string::npos) {
				string oneAuthor = authors.substr(pos+3); 
				if (strToNodes.find(oneAuthor) == strToNodes.end()) {
					allNodes.push_back(authorNum);
					strToNodes[oneAuthor] = authorNum; 
					authorNum++; 
				}
				else {
					allNodes.push_back(strToNodes[oneAuthor]); 
				}
				authors.erase(pos+1); 

				pos = authors.rfind(".,");
			}
			if (strToNodes.find(authors) == strToNodes.end()) {
				allNodes.push_back(authorNum);
				strToNodes[authors] = authorNum;
				authorNum++;
			}
			else {
				allNodes.push_back(strToNodes[authors]);
			}

			for (int j = 0; j < allNodes.size(); j++) {
				for (int k = j + 1; k < allNodes.size(); k++) {
					G[allNodes[j]].push_back(allNodes[k]);
					G[allNodes[k]].push_back(allNodes[j]);
				}
			}
		}

		wfs(strToNodes["Erdos, P."]);
		cout << "Scenario " << tc << endl; 
		for (int i = 0; i < author.size(); i++) {
			int num = 0; 
			if (strToNodes.find(author[i]) != strToNodes.end())
				num = erdosNum[strToNodes[author[i]]];
			if (author[i] == "Erdos, P.") {
				cout << author[i] << " 0" << endl;
			}
			else if (num == 0) {
				cout << author[i] << " infinity" << endl; 
			}
			else {
				cout << author[i] << " " << num << endl; 
			}
		}
	}

	return 0; 
}


 类似资料: