vijos p1876 bfs+map
题意:
Xiaodao是一位喜欢参加ACM比赛的孩子.
所谓ACM比赛, 是一种团队比赛.
每一次比赛, 每队需要由恰好三位选手组成.
现在, Xiaodao希望组建一支新的队伍, 在这之前, 他需要知道每一位朋友有多少可能成为自己的好队友.
他计划给每一位朋友做出一个等级标号.
Xiaodao本人的等级标号为0.
如果一位朋友曾经和Xiaodao组队参加过比赛, 那么就标号为1.
如果一位朋友并没有与Xiaodao组队参加过比赛, 但是曾经与一位"与Xiaodao一起参加过比赛的人"组队参加过比赛. 那么就标号为2.
如果一位朋友无法标号为小于等于 k 的整数, 但是曾经与"标号为k的人"一起参加过比赛, 那么便可以标号为k+1.
其余的朋友们, 便无法给出编号, 我们记为"undefined".
现在, 我们给出了 n 组曾经参赛过的队伍, 每一组中给出了三位选手的名字.
我们希望知道每一位涉及到的选手应该被给予什么样的标号.
链接:点我
邻接表和邻接矩阵都没满分
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
using namespace std;
#define MOD 1000000007
const int INF=0x3f3f3f3f;
const double eps=1e-;
typedef long long ll;
#define cl(a) memset(a,0,sizeof(a))
#define ts printf("*****\n");
const int MAXN=;
int n,m,tt,u,tot;
int c[MAXN][MAXN],dist[MAXN],vis[MAXN];
string a[MAXN];
struct Node
{
int to,next;
}edge[MAXN*];
int head[MAXN];
int tol;
void init()
{
tol=;
memset(head,-,sizeof(head));
}
void addedge(int a,int b)
{
edge[tol].to=b;
edge[tol].next=head[a];
head[a]=tol++;
edge[tol].to=a;
edge[tol].next=head[b];
head[b]=tol++;
}
void bfs()
{
queue<int> q;
cl(vis);
vis[u]=;
dist[u]=;
int now,next;
q.push(u);
while(!q.empty())
{
now=q.front();
q.pop();
for(int i=head[now];i!=-;i=edge[i].next)
{
next=edge[i].to;
if(!vis[next])
{
dist[next]=dist[now]+;
vis[next]=;
q.push(next);
}
}
}
}
map<string,int> mp;
string s="Xiaodao";
int main()
{
int i,j,k;
/*#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
#endif*/
scanf("%d",&n);
tot=;
cl(c);
string s1,s2,s3;
bool flag=;
init();
int i1,i2,i3;
for(i=;i<n;i++)
{
cin>>s1,cin>>s2,cin>>s3;
if((s1==s||s2==s||s3==s)&&!flag) u=tot,flag=;
i1=mp[s1],i2=mp[s2],i3=mp[s3];
if(!mp[s1]) a[tot]+=s1,i1=mp[s1]=tot++;
if(!mp[s2]) a[tot]+=s2,i2=mp[s2]=tot++;
if(!mp[s3]) a[tot]+=s3,i3=mp[s3]=tot++;
addedge(i1,i2),addedge(i1,i3),addedge(i2,i3);
}
bfs();
sort(a+,a+tot+);
for(i=;i<=tot;i++)
{
int l=a[i].length();
if(l==) continue;
for(j=;j<l;j++)printf("%c",a[i][j]);
int id=mp[a[i]];
if(!vis[id]) printf(" undefined\n");
else printf(" %d\n",dist[id]);
}
}
vijos p1876 bfs+map的更多相关文章
- Catch That Cow POJ - 3278 bfs map超时,短路判断顺序。
题意:可以把n边为n+1,n-1,n*2问从n到k的最少变化次数. 坑:标题写了.有点不会写bfs了... ac代码 #define _CRT_SECURE_NO_WARNINGS #include& ...
- PAT 甲级 1034 Head of a Gang (30 分)(bfs,map,强连通)
1034 Head of a Gang (30 分) One way that the police finds the head of a gang is to check people's p ...
- 845. 八数码(bfs+map)
在一个3×3的网格中,1~8这8个数字和一个“X”恰好不重不漏地分布在这3×3的网格中. 例如: 1 2 3 X 4 6 7 5 8 在游戏过程中,可以把“X”与其上.下.左.右四个方向之一的数字交换 ...
- 小花梨判连通 (bfs+思维+map统计数量)
如果两个集合存储颜色的情况相同,说明这两个在k个图中都是在一个集合的 学到的点:用map,将vector映射一个整数时,只有vector后面的邻接的数据都一样时,才认为两个vector一样 代码: # ...
- hdu.1067.Gap(bfs+hash)
Gap Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- 最少步数(dfs + bfs +bfs优化)
最少步数 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 ...
- DFS和BFS
BFS 代码步骤: 1.写出每个点和每个点的邻接点的对应关系 2.方法参数:传一个对应关系和起始点 3.创建一个队列,然后每次都移除第一个,然后把移除的邻接点添加进去,打印取出的第一个,然后循环,一直 ...
- BFS搜索算法应用_Codevs 1004 四子连棋
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <algorithm> #include <cs ...
- hdu 5025 bfs+状压
http://acm.hdu.edu.cn/showproblem.php?pid=5025 N*N矩阵 M个钥匙 K起点,T终点,S点需多花费1点且只需要一次,1-9表示9把钥匙,只有当前有I号钥匙 ...
随机推荐
- 利用反射型XSS二次注入绕过CSP form-action限制
利用反射型XSS二次注入绕过CSP form-action限制 翻译:SecurityToolkit 0x01 简单介绍 CSP(Content-Security-Policy)是为了缓解XSS而存在 ...
- 如何禁止Linux内核的-O2编译选项【转】
转自:http://blog.csdn.net/larryliuqing/article/details/8674274 http://lenky.info/2013/03/10/%E5%A6%82% ...
- from setuptools import setup ImportError: No module named setuptools【转】
转自:http://www.cnblogs.com/chinacloud/archive/2010/12/24/1915644.html from setuptools import setupImp ...
- aarch64_fc26_url
http://linux.yz.yamagata-u.ac.jp/pub/linux/fedora-projects/fedora-secondary/releases/26/Everything/a ...
- python使用twisted搭建的一个socket服务
服务端 # -*- coding: utf-8 -*- # @Time : 2018/9/19 21:41 # @Author : cxa # @File : tsTservTW.py # @Soft ...
- C#.NET调用WSDL接口及方法
1.首先需要清楚WSDL的引用地址 如:http://XX.XX.4.146:8089/axis/services/getfileno?wsdl 上述地址的构造为 类名getfileno. 2.在.N ...
- Github授权新的设备ssh接入
为Mac生成公钥 步骤: 检查本机是否已有公钥 ls -la ~/.ssh 将原来的公钥删除 rm -rf ~/.ssh 生成新的公钥(填自己的邮箱),然后除了密码,一路默认 ssh-keygen - ...
- 30 C? Go? Cgo!
C? Go? Cgo! 17 March 2011 Introduction Cgo lets Go packages call C code. Given a Go source file writ ...
- java 添加一组元素
在java包中的Arrays和Collection类中都有很多实用方法,可以在一个Collection中添加一组元素,Array.asList()方法接受一个数组或是一个用逗号分隔的元素列表(使用可变 ...
- const 和 #define区别_fenglovel_新浪博客
const 和 #define区别 (2012-12-11 14:14:07) 转载▼ 标签: 杂谈 (1) 编译器处理方式不同 define宏是在预处理阶段展开. const常量是编译运行阶段使 ...