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. 使用jsTree动态加载节点

    因为项目的需要,需要做一个树状菜单,并且节点是动态加载的,也就是只要点击父节点,就会加载该节点下的子节点. 大致的效果实现如下图: 以上的实现就是通过jsTree实现的,一个基于JQuery的树状菜单 ...

  2. Html获取经纬度

    if (navigator.geolocation) { navigator.geolocation.getCurrentPosition( function success(pos) {alert( ...

  3. 《java版进制转换》

    import java.util.Scanner; class 十进制转成十六进制_2 { public static void main(String[] args) { int num = 0; ...

  4. C语言学习笔记之成员数组和指针

    成员数组和指针是我们c语言中一个非常重要的知识点,记得以前在大学时老师一直要我们做这类的练习了,但是最的还是忘记了,今天来恶补一下.     单看这文章的标题,你可能会觉得好像没什么意思.你先别下这个 ...

  5. App跳转至系统Settings

    很多著名和非著名的App有在App内通过某种方式跳转到系统Settings的功能.不论初心和交互,某认为这个功能用的好确实是很方便的,Control Center功能有限,Home键点击起来很累,至于 ...

  6. C#基础--面向过程计算器

    //面向过程计算器 //思路: 需要注意的是: 两个数相除 除数不能为0: //1.提示用户输入 //2.进行运算 //3.得到结果 Console.WriteLine("请输入第一个数字: ...

  7. PHP+MySql字符问题原理分析

    假如数据库已经设置了utf-8 ,php文件也设置了utf-8 ,但在php文件的查询语句中未添加了 mysql_query("set names utf8")语句,此时php页面 ...

  8. druid连接池配置

    本人使用的是springMVC框架,以下是我的配置: step1,配置数据源(applicationContext-resource.xml中): <bean id="cc_ds&qu ...

  9. C++中的数组与指针

    数组与指针看起来很像 int a[] = {1, 2 ,3}; int *p = a; 如此,我们可以p[0], p[1], p[2] 看起来,与直接使用数组名没什么两样,但是看这段代码 sizeof ...

  10. 二、XML约束

    XML约束有dtd约束和Schema约束两种 dtd约束:可以在xml内部写dtd约束也可以在xml中引用外部dtd文件 book.dtd<!ELEMENT 书架 (书+)>    < ...