ural1701 Ostap and Partners
Ostap and Partners
Memory limit: 64 MB
“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!”
Input
Output
Samples
| input | output |
|---|---|
5 6 |
Possible |
3 5 |
Impossible after 3 statements |
3 2 |
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的更多相关文章
- 1701. Ostap and Partners(并查集-关系)
1701 又是类似食物链的这一类题 这题是找与根节点的和差关系 因为0节点是已知的 为0 那么所有的都可以转换为与0的和差关系 可以规定合并的两节点 由大的指向小的 然后再更新和差关系 有可能最后有 ...
- Windows Phone 8 解锁提示IpOverUsbSvc问题——IpOverUsbEnum返回No connected partners found解决方案
我的1520之前总是无法解锁,提示:IpOverUsbSvc服务没有开启什么的. 根据网上网友的各种解决方案: 1. 把手机时间设置为当前时间,并且关闭“自动设置” 2. 确保手机接入了互联网 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 ...
- 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 ...
- 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 ...
- 一些SAP Partners能够通过二次开发实现打通C/4HANA和S/4HANA的方法介绍
有好几位朋友在公众号后台给我留言询问SAP C/4HANA和S/4HANA集成的方案. 尽管我给这些朋友推送了一个方案:打通C/4HANA和S/4HANA的一个原型开发:智能服务创新案例,然而我得到的 ...
- 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 ...
- 【64.22%】【codefoces round 382A】Ostap and Grasshopper
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- [Ccodeforces 736C] Ostap and Tree - 树形DP
给定一个n个点的树,把其中一些点涂成黑色,使得对于每个点,其最近的黑点的距离不超过K. 树形DP. 设置状态f[i][j]: 当j <= K时: 合法状态,表示i的子树中到根的最近黑点距离为j的 ...
随机推荐
- C语言介绍
以下东东转自百度百科 C语言是一种计算机程序设计语言,它既具有高级语言的特点,又具有汇编语言的特点.它由美国贝尔实验室的Dennis M. Ritchie于1972年推出,1978年后,C语言已先后被 ...
- 在线协作沟通工具DesignBoard帮助设计团队更有效地进行沟通与版本管理
设计稿呈现缺乏整体性.远程沟通效率低.多版本管理混乱,这可能是很多创业团队都面临的问题,并且这些问题都将直接影响到产品开发进度.国内创业团队彩程设计也曾面临同样的问题,后来他们在做某个新项目时尝试把设 ...
- java IO类图
- java编码问题
工作中经常遇到java编码问题,由于缺乏研究,总是无法给出确切的答案,这个周末在网上查了一些资料,在此做些汇总. 问题一:在java中读取文件时应该采用什么编码? Java读取文件的方式总体可以分为两 ...
- 微信接口开发1--向微信发送请求--获取access_token
//随便放置一个php文件在服务器上.执行该方法--调用模拟get提交---到微信-->获得微信返回的access_token 不建议自己编写模拟get提交方法. 建议直接导入微信框架LaneW ...
- Python使用os.listdir()函数来获得目录中的内容
摘自:http://it.100xuexi.com/view/otdetail/20130423/057606dc-7ad1-47e4-8ea6-0cf75f514837.html 1.在Pyth ...
- 中国内地、台湾、香港、澳门和国外DNS服务器地址列表
中国内地.台湾.香港.澳门和国外DNS服务器地址列表 详细内容 作者:网路岗 来源:局域网监控软件 DNS(Domain Name System)是域名解析服务器的意思,它在互联网的作用是把域名转换成 ...
- NTP服务器时间同步
CentOS 配置服务器NTP同步 1 查看是否安装 rpm -aq | grep ntp 2 安装 yum -y install ntp 3 配置 /etc/ntp.conf restrict 访问 ...
- A*搜寻算法(A星算法)
A*搜寻算法[编辑] 维基百科,自由的百科全书 本条目需要补充更多来源.(2015年6月30日) 请协助添加多方面可靠来源以改善这篇条目,无法查证的内容可能会被提出异议而移除. A*搜索算法,俗称A星 ...
- 全选js实现
前端全选框 <input type='checkbox' name='allSelect' onclick='sel(this)'></input> //有i个选择框 < ...