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+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}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,,n-)p[i]=i;
rep(i,,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 *printf("Impossible after %d statements\n",i);
}
}
int ca;
rep(i,,n-)
{
int fa=find(i);
if(i==)b[fa]=-a[i],ca=fa;
else if(fa==ca)continue;
else if(a[i]<)b[fa]=max(b[fa],-a[i]);
}
rep(i,,n-)
{
int fa=find(i);
if(a[i]+b[fa]<||a[i]+b[fa]>1e9)return *printf("Impossible after %d statements\n",m);
}
puts("Possible");
rep(i,,n-)printf("%lld\n",a[i]+b[find(i)]);
//system("Pause");
return ;
}

ural1701 Ostap and Partners的更多相关文章

  1. 1701. Ostap and Partners(并查集-关系)

    1701 又是类似食物链的这一类题 这题是找与根节点的和差关系 因为0节点是已知的 为0  那么所有的都可以转换为与0的和差关系 可以规定合并的两节点 由大的指向小的 然后再更新和差关系 有可能最后有 ...

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

    我的1520之前总是无法解锁,提示:IpOverUsbSvc服务没有开启什么的. 根据网上网友的各种解决方案: 1. 把手机时间设置为当前时间,并且关闭“自动设置” 2. 确保手机接入了互联网 3.确 ...

  3. Codeforces Round #382 (Div. 2) A. Ostap and Grasshopper bfs

    A. Ostap and Grasshopper 题面 On the way to Rio de Janeiro Ostap kills time playing with a grasshopper ...

  4. Codeforces Round #382 (Div. 2)E. Ostap and Tree

    E. Ostap and Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  5. Codeforces735A Ostap and Grasshopper 2016-12-13 11:53 78人阅读 评论(0) 收藏

    A. Ostap and Grasshopper time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

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

    有好几位朋友在公众号后台给我留言询问SAP C/4HANA和S/4HANA集成的方案. 尽管我给这些朋友推送了一个方案:打通C/4HANA和S/4HANA的一个原型开发:智能服务创新案例,然而我得到的 ...

  7. Codeforces 735 E Ostap and Tree

    Discription Ostap already settled down in Rio de Janiero suburb and started to grow a tree in his ga ...

  8. 【64.22%】【codefoces round 382A】Ostap and Grasshopper

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. [Ccodeforces 736C] Ostap and Tree - 树形DP

    给定一个n个点的树,把其中一些点涂成黑色,使得对于每个点,其最近的黑点的距离不超过K. 树形DP. 设置状态f[i][j]: 当j <= K时: 合法状态,表示i的子树中到根的最近黑点距离为j的 ...

随机推荐

  1. .NET技术+25台服务器怎样支撑世界第54大网站

    摘要:同时使用Linux和Windows平台产品,大量使用静态的方法和类,Stack Overflow是个重度性能控.同时,取代横向扩展,他们坚持着纵向扩展思路,因为“硬件永远比程序员便宜”. Sta ...

  2. linux的pvtrace环境配置

    1.查看当前ubuntu版本号 froid@ubuntu:~/Desktop$ lsb_release -aNo LSB modules are available.Distributor ID:   ...

  3. c语言-转义序列

    字符组合是由反斜杠 (\) 后接字母或位组合构成的字符组合.若要显示换行符,单引号或某些其他字符在字符串末尾,必须使用转义序列. 转义序列被视为单个字符,因此,它是有效的字符常数. 转义序列通常用于指 ...

  4. MFC模态和非模态对话框编程

    MFC中对话框有两种形式,一个是模态对话框(model dialog box),一个是非模态对话框(modeless dialog box). 一.模态对话框(model dialog box) 在程 ...

  5. Oracle小技术集锦

  6. hdu_3001_Travelling(状压DP)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3001 题意:给你N个点,M条边,每个点最多走两次,问走完N个点最短的路程为多少. 题解:注意这题有重边 ...

  7. hdu_1253_胜利大逃亡(bfs+剪枝)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1253 题意:三维BFS,不解释 题解:DFS+剪枝会超时,裸BFS会超时,BFS+剪枝才能AC,有点伤 ...

  8. mvc Area相关技术

    ASP.NET MVC中,是依靠某些文件夹以及类的固定命名规则去组织model实体层,views视图层和控制层的.如果是大规模的应用程序,经常会由不同功能的模块组成,而每个功能模块都由MVC中的三层所 ...

  9. phpstudy vhost 配置

    server { listen 80 ; server_name user.fudaobank.com; index index.html index.htm index.php; root /ali ...

  10. SharePoint 2010 应用url参数过滤列表视图数据(应用get办法过滤列表数据)

    名人名言:读活书,活读书,读书活.——郭沫若 题目其实不知道如何称呼才干合适大师的搜刮习惯.以便有类似题目经由过程百度或google可以搜刮到,其实就是在url后面添加参数过滤显示我们想要的成果,有人 ...