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

input output
5 6
3 4 1200
4 1 -5500
2 3 4300
3 0 8200
0 4 -7000
2 1 0
Possible
0
12500
12500
8200
7000
3 5
1 2 5
0 2 0
1 0 -5
1 2 5
2 2 0
Impossible after 3 statements
3 2
1 0 871
1 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|1
const 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;
}
时间: 2024-08-25 08:49:26

Ostap and Partners的相关文章

Windows Phone 8 解锁提示IpOverUsbSvc问题——IpOverUsbEnum返回No connected partners found解决方案

我的1520之前总是无法解锁,提示:IpOverUsbSvc服务没有开启什么的. 根据网上网友的各种解决方案: 1. 把手机时间设置为当前时间,并且关闭“自动设置” 2. 确保手机接入了互联网 3.确保IpOverUsb服务开启 4. 在设备管理器中卸载“便携设备”中的相应手机 5. 重启手机.重启电脑 都不管用,抱着死马当活马医的态度进行最后的尝试.偶然看到: 我尝试后发现 IpOverUsbEnum.exe执行结果为 No connected partners found 而不是类似这样:

一些SAP Partners能够通过二次开发实现打通C/4HANA和S/4HANA的方法介绍

有好几位朋友在公众号后台给我留言询问SAP C/4HANA和S/4HANA集成的方案. 尽管我给这些朋友推送了一个方案:打通C/4HANA和S/4HANA的一个原型开发:智能服务创新案例,然而我得到的反馈是:在这个创新案例里,需要在C/4HANA里的服务云做一些后台开发,即下图红色方框标注的C4C API endpoint.因为是云产品,这种后台开发只有SAP能做,并没有对Partners开放. 因此这篇文章我会介绍一些Partners能够进行的二次开发方式,通过这些方式也能实现C/4HANA和

L416 Why Do People Cheat on Their Partners?

Why Do People Cheat on Their Partners? There is a pretty girl in a school and there is a nerd. The nerd is infatuated with the pretty girl but never gathered courage in his life to speak to this girl. This pretty girl was involved with many boys and

2017.10.28 Fujian MS partners power bi training

This training was a free of charge.Power BI has been released in china. I am a speaker. I taught them visualize data with Power BI. Power BI Desktop provides a free-form canvas for drag-and-drop data exploration as well as an extensive library of int

CF735E Ostap and Tree

比较毒瘤的树形DP,子状态难想.这是主要是搬运一篇题解. 用\(f[i][j]\)表示\(i\)的子树中离\(i\)最近黑点的距离为\(j\),且距离超过\(j\)的点都被满足的方案数.转移时新建一个临时数组\(tmp\)保存转移后的\(f[x]\).设\(y\)是\(x\)的子结点,枚举\(f[x][i]\)和\(f[y][j]\),转移如下: 若\(i+j≤2k\),则此时\(min(i,j+1)≤k\),对于长度为\(i+j+1\)的链上的所有点都可以找到一边距离\(≤k\),因此状态合并

2015《财富》世界500强发布

财富中文网于北京时间2015年7月22日晚与全球同步发布了最新的<财富>世界500强排行榜.中国上榜公司数量继续增长,今年达到了106家.中石化在榜上排名上升至第二位.5家中国内地公司首次上榜,分别是:陕西煤业化工集团.中国光大集团.中国航天科技集团公司.中国保利集团.海航集团. 今年,世界500强的入围门槛提高至237.2亿美元:总收入达31.2万亿美元,比上一年增长了0.49%:但总利润比去年减少了14.76%,为1.67万亿美元. 1 沃尔玛 营业收入(百万美元):485,651 2 中

FDA Approves First Hepatitis B Viral Load Test on 2007

FDA Approves First Hepatitis B Viral Load Test Another Roche first in TaqMan real-time PCR testing for the diagnostic lab The U.S. Food & Drug Administration (FDA) has approved the Roche COBAS TaqManHBV Test, the first assay for quantitating hepatiti

如何成为一名合格的CTO?(转)

不会走出去公众演说的的攻城狮不是好CTO. 本文来源于微信公众号"线性资本"(ID:LinearVenture) 成为一名合格 CTO 我们投过很多技术型的公司,对于什么是合格的 CTO 有过自己的一些思考.最近关于什么是一名合格的 CTO 有些争论,我们以Q&A 的方式分享一些我们的看法.这些看法一方面有时候会影响到我们投不投一家公司,一方面也是从被投公司的历程中总结出来的. CTO 的定义是什么? 根据其定义,Chief Technology Officer,就是技术方面的

2017年世界500强榜单,500强亏损公司,强最赚钱的50家公司

2017年世界500强榜单发布:腾讯阿里首次登榜 2017年07月20日 20:03:51 财富中文网于北京时间2017年7月20日晚与全球同步发布了最新的<财富>世界500强排行榜. 沃尔玛连续四年排名第一位,2016年营业收入达4,858.7亿美元,同比提升0.8%.前三阵营中的其它两家为中国公司--国家电网和中石化.中石油和丰田汽车分列第四和第五.唯一新进入前十阵营的是沃伦巴菲特掌管的保险和投资集团伯克希尔-哈撒韦公司.如今伯克希尔收入中近四分之三来自经营业务而非财务投资,在挣脱巴菲特光