URAL 1291 Gear-wheels (BFS)

Source: Internet
Author: User

Gear-wheelstime limit:1.0 Second
Memory limit:64 mb-arny! What is happened with coordinator? Bad working coordinator was the everlasting trouble of their spaceship. Arny already had been working under this trouble while he not very attentive and responsible mate just noticed the Breaka Ge. Judging by schematics The broken module of coordinator consists of the "set of special gears connected with each other in O Rder to transfer the traction from the kinetic generator to the lot of antenna driving engines. Despite the extraterrestrial origin of these gears, they is connected by usual terrestrial method:the cogs of one gear-w Heel get into the slots between cogs of another gear-wheel. So the rotation of the first gear-wheel are transmitted to the second Gear-wheel. After the multiple Arny ' s revisions, no unnecessary gears stayed in the coordinator. It means that there are no cycles in gears connection graph. The only problem are to check the-all the gears has right directions and speeds of rotation. Inputfirst Line of input Contains the number of gear-wheels in mechanism N(1≤ N≤1000). The next NLines 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 is connected to the I-th one. Zero ends the list. The last line of input contains the number of Gear-wheel, which is connected to the Kinetic-generator and the Rotation V(1≤ V≤1000). This gear-wheel rotates in counter-clockwise direction. Outputoutput should contain NLines. In the I-th line There are a speed of rotation of I-th Gear-wheel in the form of irreducible fraction. Numerator and denominator of this fraction should is separated by the sign '/'. If speed is negative, it's 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 is equal 0 and denominator should is equal to 1. It is guaranteed this neither numerator nor denominator of all speeds would be greater than 106.Sample
input Output
410 2 3 020 1 040 1 4 0100 3 01 6
6/1-3/1-3/23/5
problem Author:Pavel Egorov "Analysis" gives you n gears, and then the following n lines, the first number of each line is the number of teeth gear, followed by his linked gear number, the last line to the starting position of the gear number and speed, the speed of the rest of the gear, with the simplest fraction expressed.
#include <iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<cmath>#include<string>#include<map>#include<stack>#include<queue>#include<vector>#defineINF 0x3f3f3f3f#defineMet (b) memset (a,b,sizeof a)#definePB Push_backtypedefLong Longll;using namespacestd;Const intN = 1e3+Ten;Const intM =124750+Ten;Const intmod=1e9+7;intn,m,k,tot=0, s,t,v;intHead[n],vis[n],speed[n],sum[n];structans{intF,s;} Ans[n];structedg{intTo,next;} edg[n*N];voidAddintUintv) {edg[tot].to=v;edg[tot].next=head[u];head[u]=tot++;}intgcdintXinty) {    intxx=x,yy=y; X=Max (XX,YY); Y=min (yy,xx); if(x%y==0)returny; returnGCD (xy,y);}voidBFs () {Queue<int>Q; Q.push (s); Vis[s]=1; ans[s].f=t;ans[s].s=1;  while(!Q.empty ()) {        intU=q.front (); Q.pop ();//printf ("!!! %d%d%d\n ", U,ANS[U].F,ANS[U].S); System (" pause ");         for(inti=head[u];i!=-1; i=Edg[i].next) {            intv=edg[i].to; if(!Vis[v]) {Vis[v]=1; intG=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); }        }    }}intMain () {Met (ans,0); Met (head,-1); intu,v; scanf ("%d",&N);  for(intu=1; 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(intI=1; i<=n;i++){        if(ans[i].f>0) {printf ("%d/%d\n", ANS[I].F,ANS[I].S); }Else if(ans[i].f==0) {printf ("0/1\n"); }Else{printf ("-%d/%d\n",-ans[i].f,ans[i].s); }    }    return 0;}

URAL 1291 Gear-wheels (BFS)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.