首页 > 代码库 > Ostap and Partners

Ostap and Partners

Ostap and Partners

Time limit: 2.0 second
Memory limit: 64 MB
Workman Ivan lost his job. Not because of truancy or being late, and not because the plant where he had been working was left without orders. The reason for dismissal was the stale aspic he had presented to his boss for his birthday.
After one more day of fruitless job search he dropped in a supermarket near his home. He had a little money left, 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 was his old acquaintance Vassily. After loud greetings and an argument about the best variety of brandy, they went out into the street at last.
“Well, how are you, my friend?” Vassily asked. 
“Looking for a job,” Ivan answered wearily. 
“You know, I was 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 that job like?” no wonder, jobless Ivan became interested. 
“Have you 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 do they pay you?” Ivan wanted to know. 
“They don‘t pay me yet,” answered his 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 that our foreman drives a Mercedes!” 
“Ah, I would like to know how 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 are greater than someone else‘s. For example, Stepan said recently that he was getting 1200 rubles more than Fyodor. And Fyodor once complained that he was getting 5500 rubles less than the foreman.” 
“Collect then as many such comparisons as you can, and we will 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…

Input

The first line contains the number n of the workmen in the brigade and the number m of records in the notebook (1 ≤ nm ≤ 50000). Each of the following m lines contains three integers: ij, andd, which mean that the wage of the i-th workman is greater than the wage of the j-th workman by drubles (0 ≤ ij ≤ n−1; |d| ≤ 20000). The workmen are enumerated from 0 to n−1 starting from Vassily, whose wage is zero. It is known that no workman gets more than 109 rubles.

Output

If it is possible to find amounts of wages that lie in the given range and satisfy all the comparisons from the notebook, output “Possible” in the first line and then output n integers each in the separate line which are the possible amounts in the ascending order of the workmen‘s numbers. If several answers are possible, output any one of them.
If there is no answer, output the only line with the words “Impossible after i statements”, where the number i is the number of the first record in the notebook such that considering only the preceding records it is possible to find an answer and with the addition of this record it becomes impossible. The records are enumerated starting from the number one in the order in which they are given.

Samples

inputoutput
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

 

分析:并查集合并;

代码:

#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>#define rep(i,m,n) for(i=m;i<=n;i++)#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)#define mod 1000000007#define inf 0x3f3f3f3f#define vi vector<int>#define pb push_back#define mp make_pair#define fi first#define se second#define ll long long#define pi acos(-1.0)#define pii pair<int,int>#define Lson L, mid, rt<<1#define Rson mid+1, R, rt<<1|1const int maxn=5e4+10;using namespace std;ll gcd(ll p,ll q){return q==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;}return f;}int n,m,k,t,p[maxn],flag;ll a[maxn],b[maxn];int find(int x){    if(x==p[x])return x;    else    {        int fa=p[x];        p[x]=find(p[x]);        a[x]+=a[fa];    }    return p[x];}int main(){    int i,j;    scanf("%d%d",&n,&m);    rep(i,0,n-1)p[i]=i;    rep(i,1,m)    {        int c,d,e;        scanf("%d%d%d",&c,&d,&e);        int fa=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);        }    }    int ca;    rep(i,0,n-1)    {        int fa=find(i);        if(i==0)b[fa]=-a[i],ca=fa;        else if(fa==ca)continue;        else if(a[i]<0)b[fa]=max(b[fa],-a[i]);    }    rep(i,0,n-1)    {        int fa=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