Gear-wheels

Time limit: 1.0 second
Memory limit: 64 MB
— Arny! What happened with coordinator?
Bad
working coordinator was the everlasting trouble of their spaceship.
Arny already had been working under this trouble while his not very
attentive and responsible mate just noticed the breakage.
Judging by schematics the broken module of coordinator consists of the
set of special gears connected with each other in order to transfer the
traction from the kinetic generator to the lot of antenna driving
engines.
Despite
the extraterrestrial origin of these gears, they are connected by usual
terrestrial method: the cogs of one gear-wheel get into the slots
between cogs of another gear-wheel. So the rotation of the first
gear-wheel is transmitted to the second gear-wheel.
After
the multiple Arny’s revisions, no unnecessary gears stayed in the
coordinator. It means that there is no cycles in gears connection graph.
The only problem now is to check that all the gears have right
directions and speeds of rotation.

Input

First line of input contains the number of gear-wheels in mechanism N (1 ≤ N ≤ 1000). The next N lines contain the information about the gear-wheels. i-th line contains K (1 ≤ K ≤ 1000) — the number of cogs on the i-th gear-wheel followed by the list of gears, that are connected to the i-th one. Zero ends the list.
The
last line of input contains the number of gear-wheel, that is connected
to the kinetic-generator and the speed of its rotation V (1 ≤ V ≤ 1000). This gear-wheel rotates in counter-clockwise direction.

Output

Output should contain N lines. In the i-th line there is a speed of rotation of i-th
gear-wheel in the form of irreducible fraction. Numerator and
denominator of this fraction should be separated by the sign ‘/’. If
speed is negative, it is assumed that the gear-wheel rotates in
clockwise direction (in this case the minus sign should be displayed
before numerator). Otherwise the gear-wheel rotates in counter-clockwise
direction. If speed equals zero than numerator should be equal 0 and
denominator should be equal to 1. It is guaranteed that neither
numerator nor denominator of all speeds will be greater than 106.

Sample

input output
4
10 2 3 0
20 1 0
40 1 4 0
100 3 0
1 6
6/1
-3/1
-3/2
3/5
Problem Author: Pavel Egorov
【分析】给你n个齿轮,然后下面n行,每行第一个数为齿轮的齿数,后面为与他链接的齿轮编号,最后一行给出发动机所在齿轮编号及转速,求其余齿轮的速度,用最简分数表示。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
typedef long long ll;
using namespace std;
const int N = 1e3+;
const int M = +;
const int mod=1e9+;
int n,m,k,tot=,s,t,v;
int head[N],vis[N],speed[N],sum[N];
struct ANS{int f,s;}ans[N];
struct EDG{int to,next;}edg[N*N];
void add(int u,int v){
edg[tot].to=v;edg[tot].next=head[u];head[u]=tot++;
}
int gcd(int x,int y){
int xx=x,yy=y;
x=max(xx,yy);
y=min(yy,xx);
if(x%y==)return y;
return gcd(x-y,y);
}
void bfs(){
queue<int>q;
q.push(s);vis[s]=;ans[s].f=t;ans[s].s=;
while(!q.empty()){
int u=q.front();q.pop();//printf("!!!%d %d %d\n",u,ans[u].f,ans[u].s);system("pause");
for(int i=head[u];i!=-;i=edg[i].next){
int v=edg[i].to;
if(!vis[v]){
vis[v]=;
int g=gcd(abs(sum[u]*ans[u].f),sum[v]*ans[u].s);
ans[v].f=-sum[u]*ans[u].f/g;ans[v].s=sum[v]*ans[u].s/g;
q.push(v);
}
}
}
}
int main()
{
met(ans,);met(head,-);
int u,v;
scanf("%d",&n);
for(int u=;u<=n;u++){
scanf("%d",&sum[u]);
while(~scanf("%d",&v)&&v){
add(u,v);add(v,u);
}
}
scanf("%d%d",&s,&t);
bfs();
for(int i=;i<=n;i++){
if(ans[i].f>){
printf("%d/%d\n",ans[i].f,ans[i].s);
}else if(ans[i].f==){
printf("0/1\n");
}else {
printf("-%d/%d\n",-ans[i].f,ans[i].s);
}
}
return ;
}

URAL 1291 Gear-wheels(BFS)的更多相关文章

  1. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  2. 【算法导论】图的广度优先搜索遍历(BFS)

    图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...

  3. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现

    1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...

  4. 【BZOJ5492】[HNOI2019]校园旅行(bfs)

    [HNOI2019]校园旅行(bfs) 题面 洛谷 题解 首先考虑暴力做法怎么做. 把所有可行的二元组全部丢进队列里,每次两个点分别向两侧拓展一个同色点,然后更新可行的情况. 这样子的复杂度是\(O( ...

  5. 深度优先搜索(DFS)和广度优先搜索(BFS)

    深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...

  6. 图的 储存 深度优先(DFS)广度优先(BFS)遍历

    图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...

  7. 数据结构与算法之PHP用邻接表、邻接矩阵实现图的广度优先遍历(BFS)

    一.基本思想 1)从图中的某个顶点V出发访问并记录: 2)依次访问V的所有邻接顶点: 3)分别从这些邻接点出发,依次访问它们的未被访问过的邻接点,直到图中所有已被访问过的顶点的邻接点都被访问到. 4) ...

  8. POJ 1502 MPI Maelstrom / UVA 432 MPI Maelstrom / SCU 1068 MPI Maelstrom / UVALive 5398 MPI Maelstrom /ZOJ 1291 MPI Maelstrom (最短路径)

    POJ 1502 MPI Maelstrom / UVA 432 MPI Maelstrom / SCU 1068 MPI Maelstrom / UVALive 5398 MPI Maelstrom ...

  9. 层层递进——宽度优先搜索(BFS)

    问题引入 我们接着上次“解救小哈”的问题继续探索,不过这次是用宽度优先搜索(BFS). 注:问题来源可以点击这里 http://www.cnblogs.com/OctoptusLian/p/74296 ...

  10. HDU.2612 Find a way (BFS)

    HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...

随机推荐

  1. BAT文件执行完成后如何删除自身的解决办法

    在BAT文件的最后加上一句 del %0,就可以在处理完后,删除自己了

  2. java static

    一. static代表着什么 在Java中并不存在全局变量的概念,但是我们可以通过static来实现一个“伪全局”的概念,在Java中static表示“全局”或者“静态”的意思,用来修饰成员变量和成员 ...

  3. (转)如何学好C语言,一个成功人士的心得!

    zidier111发表于 2013-1-26 08:59:05   今 天,我能够自称是一个混IT的人,并能以此谋生,将来大家能一次谋生,都要感谢两个人:克劳德.香农和约翰.冯.诺依曼,是他们发现了所 ...

  4. php的查询数据

    php中 连接数据库,通过表格形式输出,查询数据.全选时,下面的分选项都选中;子选项取消一个时,全选按钮也取消选中. <!DOCTYPE html PUBLIC "-//W3C//DT ...

  5. Ubuntu 14.10 下设置静态IP

    修改 /etc/network/interfaces 文件 sudo nano /etc/network/interfaces 修改为 # 前面的不变auto eth0 iface eth0 inet ...

  6. (转)phonegap 数据库详解

    原文:http://firepix.iteye.com/blog/1618343 phonegap 数据库详解          博客分类: web App phonegap  今天就把之前使用pho ...

  7. python3爬虫再探之EXCEL

    在爬取数据之后,数据的保存就成为一个新的问题,一般不太大的的数据存储到EXCEL就可以了.这里介绍一个python的第三方库——xlsxwriter. 这个库的安装就不介绍了,pip就可以,不用FQ. ...

  8. android onConfigurationChanged讲解

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 相信大家对这个属性已经耳熟能详,如果大家受过转屏的折磨的话! 老规矩,先讲讲官方文档是怎么说的.为什 ...

  9. 郝斌老师的SQL教程

    时隔两年,重拾数据库编程.郝斌老师的sql教程通俗易懂,用作复习简直不能太赞.

  10. array_intersect() php筛选两个数组共有的元素

    我们已经讲过如何筛选出连个数组中不共有的元素,今天就来看看php如何筛选出两个数组中共有的元素,例如筛选$array1和$array2共有的元素. 函数名:array_intersect(): 调用方 ...