Vladislav Isenbaev is a two-time champion of Ural, vice champion of TopCoder Open 2009, and absolute champion of ACM ICPC 2009. In the time you will spend reading this problem statement Vladislav would
have solved a problem. Maybe, even two…
Since Vladislav Isenbaev graduated from the Specialized Educational and Scientific Center at Ural State University, many of the former and present contestants at USU have known him for quite a few years.
Some of them are proud to say that they either played in the same team with him or played in the same team with one of his teammates…
Let us defineIsenbaev's numberas follows. This number for Vladislav himself is 0. For people who played in the same team with him, the number is 1. For people who weren't his teammates but
played in the same team with one or more of his teammates, the number is 2, and so on. Your task is to automate the process of calculating Isenbaev's numbers so that each contestant at USU would know their proximity to the ACM ICPC champion.
Input
The first line contains the number of teamsn(1 ≤n≤ 100). In each of the followingnlines you are given the names of the three members of the corresponding team. The names
are separated with a space. Each name is a nonempty line consisting of English letters, and its length is at most 20 symbols. The first letter of a name is capital and the other letters are lowercase.
Output
For each contestant mentioned in the input data output a line with their name and Isenbaev's number. If the number is undefined, output “undefined” instead of it. The contestants must be ordered lexicographically.
Sample
input
output
7
Isenbaev Oparin Toropov
Ayzenshteyn Oparin Samsonov
Ayzenshteyn Chevdar Samsonov
Fominykh Isenbaev Oparin
Dublennykh Fominykh Ivankov
Burmistrov Dublennykh Kurpilyanskiy
Cormen Leiserson Rivest
|
Ayzenshteyn 2
Burmistrov 3
Chevdar 3
Cormen undefined
Dublennykh 2
Fominykh 1
Isenbaev 0
Ivankov 2
Kurpilyanskiy 3
Leiserson undefined
Oparin 1
Rivest undefined
Samsonov 2
Toropov 1
|
本题属于图论的应用了。
有两个思路:
1 Dijstra求一点都其他所有点的最短路径
2 宽度优先的图搜索
两者的时间效率和空间效率都是一样的。
难度指数看情况而定吧。有人说很简单。
但是对于对图论不太熟悉的人来说就一点都不简单了。
尤其是使用C++来实现更加难,因为要操作繁琐的指针问题。
本人对图论也不够熟,所以本题对我来说还是挺有难度的。做熟了LeetCode题目,但是LeetCode题好像只有一题是牵涉图论的,还是复制图问题,相当于遍历问题了。
我这里使用第二个思路来做:
1 读入数据,同时构造一个邻接矩阵
注意: a) 使用一个map来检测是否是重复的名字了,如果是重复的那么就不需要构造新的节点了,而是从map中取出之前的节点处理
b)使用int n代表距离,其中n初始化为-1,代表没有访问过的节点,并可以根据这个值来防止重复访问图中的节点,避免无限循环
c) 巧用set来避免重复节点,减少多余的访问
d)注意使用指针,不使用指针的话就不能保证数据的单一性,可能会在操作副本也说不定,那么结果很容易出错 -- 我就载在这里了。这就是为什么C++那么难,那么多人都谈指针色变啊!所以一定要熟练地使用指针,使用容器如vector和set来装载指针操作,否则就成了操作副本了。
2 使用Isenbaev来初始化根节点,进行宽度优先遍历图,注意可以没有Isenbaev这个节点(好像很多人载在这里),那么就可以提前结束循环了。注意判断节点是否遍历过了,判断其距离是否是-1,-1代表没访问过。
3 技巧:使用set容器装载所有数据,就已经自动排序好了,那么可以直接打印出来,而不需要排序了。这些数据就是之前装载在map中的数据。而没必要去遍历图
最后时间效率和空间效率都是O(n)了,还是挺高效的。
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
using namespace std;
static const string NAME = "Isenbaev";
//使用邻接矩阵图来做本题
struct IsenStrAdj
{
int n;
string name;
vector<IsenStrAdj *> nexts;
IsenStrAdj(string name1 = "", int n1 = -1):n(n1), name(name1), nexts(){}
bool operator<(const IsenStrAdj &isen) const
{
return name < isen.name;
}
};
class IsenAjecentAlgo
{
public:
void run()
{
int n = 0;
cin>>n;
unordered_map<string, IsenStrAdj *> umpSi;
string tmpName;
for (int i = 0; i < n; i++)
{
vector<IsenStrAdj *> vis;
for (int j = 0; j < 3; j++)
{
cin>>tmpName;
IsenStrAdj *isen = nullptr;
if (umpSi.count(tmpName)) isen = umpSi[tmpName];
else
{
isen = new IsenStrAdj(tmpName);
umpSi[tmpName] = isen;
}
vis.push_back(isen);
}
for (int k = 0; k < 3; k++)
{
IsenStrAdj *isen = vis[k];
set<IsenStrAdj *> sis(isen->nexts.begin(),isen->nexts.end());
for (int j = 0; j < 3; j++)
{
if (isen != vis[j])
{
IsenStrAdj *t = vis[j];
sis.insert(t);
}
}
isen->nexts.assign(sis.begin(), sis.end());//确保无重复
}
}
IsenStrAdj *isen = umpSi.count(NAME)? umpSi[NAME] : nullptr;
numberThem(isen);
set<IsenStrAdj> setIsen;
for (auto x:umpSi)
{
setIsen.insert(*(x.second));
}
for (auto x:setIsen)
{
cout<<x.name<<' ';
if (-1 == x.n) cout<<"undefined"<<endl;
else cout<<x.n<<endl;
}
}
void numberThem(IsenStrAdj *isen)
{
if (!isen) return ;
queue<IsenStrAdj *> qu;
qu.push(isen);
isen->n = 0;
int lv = 0;
while (qu.size())
{
lv++;
queue<IsenStrAdj *> lv_next;
while (qu.size())
{
isen = qu.front();
qu.pop();
for (int i = 0; i < isen->nexts.size(); i++)
{
if (-1 != isen->nexts[i]->n) continue;
IsenStrAdj *t = (isen->nexts[i]);
t->n = lv;
lv_next.push(t);
}
}
qu = lv_next;
}
}
};
int main()
{
IsenAjecentAlgo isen;
isen.run();
return 0;
}
分享到:
相关推荐
acm.timus.ru最全代码
Timus ... Problems code. (Mainly Java implementation) Chinese 刷题代码,主要是java实现,可能会有其他语言代码 代码来自LeetCode / NowCoder / timus 等 site url LeetCode NowCoder Timus LeetCode-cn
acm.timus.ru解决方案 这些是我对ACM.TIMUS.RU问题的解决方案
In the present world you ...This way every word or a group of words can be assigned a unique number, so you can remember words instead of call numbers. It is evident that it has its own charm if i
timus.ru_solutions 使用的语言:Python 使用的Python版本:3.9 我可以用Python语言解决的一些问题在“ python”目录中。 有些解决方案可以在Java中运行,但确切的解决方案/算法在Python中不起作用! 不知道为什么我...
acm.timus.ru 1709 problem
timus.ru_solutions_python 使用的python:3.9 我可以用Python语言解决的一些问题在python目录中。 有些解决方案可以在Java中运行,但确切的解决方案/算法在Python中不起作用! 我不知道为什么
资源分类:Python库 所属语言:Python 资源全名:timus-sender-0.1.1.post1.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
Timus上习题解答与代码参考,这一部分对应于Timus上的Beginner部分的习题
https://acm.timus.ru/problem.aspx?space=1&num=1119 题目答案
timus:Timus在线法官问题
将图表添加到Timus Online Judge配置文件 该扩展程序将已解决问题的数量图表添加到Timus Online Judge的个人资料和比较器中。 功能:*概要文件和比较中已解决问题计数的图表*向图表中添加更多用户,删除它们,自定义...
Timus图表 将图表添加到Timus Online Judge个人资料中 特征 概要文件中和比较期间已解决问题的计数图表 向图表添加更多用户,删除它们,自定义图例颜色 缓存配置文件数据 隐藏图表 (可选)突出显示最近两个月内接受...
timus OJ 1197 lonesome kinght
蒂莫斯 该文件夹包含用Python编写的文件(主要是Python2.7,有些是Python3.4)我通过这些文件在timus上通过了相应的测试。
http://acm.timus.ru 俄罗斯乌拉尔大学在线题库 SGU http://acm.sgu.ru/ 俄罗斯圣萨拉托夫州大学在线题库 ELJ http://acm.mipt.ru/judge/bin/problems.pl?lang=en file:///M|/acm/ACM大量习题题库及建议培养计划.txt...
timus-online-judge Timus Online Judge 是俄罗斯最大的带有自动评判系统的编程问题档案。 问题主要是从在乌拉尔联邦大学、乌拉尔锦标赛、乌拉尔 ACM ICPC 次区域比赛和彼得罗扎沃茨克训练营举办的比赛中收集的。 ...
http://acm.timus.ru/problem.aspx?space=1&num=1362 一道树形动态规划的题目解答,ural1362
语言:English将图表添加到Timus Online Judge个人资料中该扩展程序将已解决问题的数量图表添加到Timus Online Judge的个人资料和比较器中。功能:*概要文件和比较中已解决问题计数的图表*向图表中添加更多用户,删除...
Pascal acm_timus_ural_1148.pas