Ostap and Partnerstime limit:2.0 second
Memory limit:64 Mbworkman Ivan lost his job. Not because of truancy or being late, and not because the plant where he had been working is left without orders. The reason for dismissal is the stale aspic he had presented to its boss for his birthday. After one more than fruitless job search he dropped in a supermarket near his home. He had a little money left and so he visited the liquor department. Waiting in the line to the cashier's desk, he recognized the man buying a bottle of expensive brandy. That is his old acquaintance Vassily. After loud greetings and a argument about the most variety of brandy, they went out into the street at last. " Well, what is you, my friend? " Vassily asked.
"Looking for a job," Ivan answered wearily.
"You know, I am also looking for a job not so long ago, and I found an excellent one!" Vassily was excited. "It ' s nearby and they promised to pay well! And you can join us too! "
"What's this job like?" No wonder, jobless Ivan became interested.
"You are heard about the company
Ostap and Partners? They ' ve been producing horns and hoofs for a number of years already. And now I'm a hoof picker of the third class with them! "answered Vassily proudly.
"How much does they pay you?" Ivan wanted to know.
"They don t pay me yet," answered he friend with disappointment. "It's the first month I work there and I ' m a probationer. And the guys in the Brigade don ' t tell me their wages, it's the company's policy. " He paused and lowered his voice to a whisper. "But I know the foreman drives a mercedes!"
"Ah, I would like to know how the much money they get," Ivan said Dreamily imagining himself driving a Merc.
"I can learn it after all!" Vassily had a sudden inspiration. "The guys like to brag at smoking breaks that their wages is greater than someone else ' s. For example, Stepan said recently the he was getting the rubles more than Fyodor. And Fyodor once complained that he is getting 5500 rubles less than the foreman. "
"Collect then as many such comparisons as can, and we'll know all the wages!" Ivan rejoiced.
"OK, I ll do that!" In a week, Vassily brought a notebook with a number of records about the comparisons of the workmen ' s wages. So they started calculations ... Inputthe First line contains the number
NOf the workmen in the brigade and the number
mof records in the Notebook (1≤
N,
m≤50000). Each of the following
mLines contains three integers:
I,
J, and
D, which mean that the wage of the
I-th Workman is greater than the wage of the
J-th Workman by
DRubles (0≤
I,
J≤
N−1; |
D| ≤20000). The workmen is enumerated from 0 to
N−1 starting from Vassily, whose wage is zero. It is known this no workman gets more than 109 rubles. Outputif It is possible to find amounts of wages, lie in the given range and satisfy all the comparisons from the note Book, output ' Possible ' in the first line and then output
NIntegers each on the separate line which is the possible amounts in the ascending order of the workmen ' s numbers. If Several answers is possible, output any one of them. If There is no answer, output the-line with the words ' impossible after
IStatements ", where the number
IIs the number of the first record in the notebook such that considering only the preceding records it's possible to find An answer and with the addition of this record it becomes impossible. The records is enumerated starting from the number one in the order in which they is given. Samples
input |
Output |
5 63 4 12004 1-55002 3 43003 0 82000 4-70002 1 0 |
Possible0125001250082007000 |
3 51 2 50 2 01 0-51 2 52 2 0 |
Impossible after 3 statements |
3 21 0 8711 2 903 |
Impossible after 2 statements |
Analysis: and check the merger;
Code:
#include <iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<algorithm>#include<climits>#include<cstring>#include<string>#include<Set>#include<map>#include<queue>#include<stack>#include<vector>#include<list>#defineRep (I,m,n) for (i=m;i<=n;i++)#defineRSP (It,s) for (Set<int>::iterator It=s.begin (); It!=s.end (); it++)#defineMoD 1000000007#defineINF 0x3f3f3f3f#defineVI vector<int>#definePB Push_back#defineMP Make_pair#defineFi first#defineSe Second#definell Long Long#definePi ACOs (-1.0)#definePII pair<int,int>#defineLson L, Mid, rt<<1#defineRson mid+1, R, rt<<1|1Const intmaxn=5e4+Ten;using namespacestd;ll gcd (ll p,ll q) {returnq==0? P:GCD (q,p%q);} ll Qpow (ll p,ll q) {ll F=1; while(q) {if(q&1) f=f*p;p=p*p;q>>=1;}returnF;}intN,m,k,t,p[maxn],flag;ll A[MAXN],B[MAXN];intFindintx) { if(X==p[x])returnx; Else { intFa=P[x]; P[X]=find (p[x]); A[X]+=A[FA]; } returnp[x];}intMain () {inti,j; scanf ("%d%d",&n,&m); Rep (I,0, N-1) p[i]=i; Rep (I,1, M) { intc,d,e; scanf ("%d%d%d",&c,&d,&e); intFa=find (c), fb=find (d); if(fa!=FB) {P[FA]=FB; A[FA]=-a[c]+a[d]+e; } Else { if(a[c]-a[d]!=e)return 0*printf ("impossible after%d statements\n", i); } } intCA; Rep (I,0, N-1) { intHasfind (i); if(i==0) b[fa]=-a[i],ca=A; Else if(FA==CA)Continue; Else if(a[i]<0) B[fa]=max (b[fa],-A[i]); } Rep (I,0, N-1) { intFa=find (i); if(a[i]+b[fa]<0|| A[I]+B[FA]>1E9)return 0*printf ("impossible after%d statements\n", M); } puts ("Possible"); Rep (I,0, N-1) printf ("%lld\n", a[i]+B[find (i)]); //System ("Pause"); return 0;}
Ostap and Partners