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号钥匙 ...
随机推荐
- 【逆向知识】PE ASLR
1.知识点 微软从windows vista/windows server 2008(kernel version 6.0)开始采用ASLR技术,主要目的是为了防止缓冲区溢出 ASLR技术会使PE文件 ...
- 2 - django-urls路由系统基本使用
目录 1 路由系统(urls控制) 1.1 正则字符串参数 1.2 url的分组 1.2.1 无名分组 1.2.2 有名分组 1.3 URLconf 在什么上查找 1.4 include(路由分发) ...
- 最详细的block底层
主要讲述的要点: block 干什么用的 block 语法 block 底层实现 block 变量捕捉 block 的种类.在存储空间中的存储位置 block 循环引用 __block 在ARC 中 ...
- Django BoundField
一.BoundField from django.forms.boundfield import BoundField BoundField是一个将字段添加数据的一个类,给对应的form字段封装上数据 ...
- html- 头部元素
一:HTML <head> 元素 <head> 元素是所有头部元素的容器.<head> 内的元素可包含脚本,指示浏览器在何处可以找到样式表,提供元信息,等等. 以下 ...
- (二) Log4j 配置详解
第一节: rootLogger 根配置 Log4j 根配置语法 log4j.rootLogger = [ level ] , appenderName, appenderName, … 指代 把指定级 ...
- Kubernetes 概述和搭建(多节点)
一.Kubernetes整体概述和架构 Kubernetes是什么 Kubernetes是一个轻便的和可扩展的开源平台,用于管理容器化应用和服务.通过Kubernetes能够进行应用的自动化部署和扩缩 ...
- Struts 2 - Environment Setup
Our first task is to get a minimal Struts 2 application running. This chapter will guide you on how ...
- ***codeigniter操作xml(Simplexml第三方扩展)
This Simplexml class provides an alternative implementation of the SimpleXML API that works under PH ...
- SSIS 学习之旅 第一个SSIS 示例(二)
这一章还是继上一章例子 进行一些小的知识扩展.主要是为了让大家更快的上手SSIS. 概要设计: 1.按用户组生成CSV文件到Pending目录下, 2.移动Pending目录下的CSV文件 ...