(并查集 建立关系)食物链 -- POJ-- 1182
链接:
http://poj.org/problem?id=1182
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82830#problem/E
代码:
#include<stdio.h>
#include<queue>
#include<stack>
#include<string.h>
using namespace std; #define maxn 100005
#define oo 0xfffffff int n, T;
int f[maxn], r[maxn]; int Find(int x)
{
int k=f[x];
if(f[x]!=x)
{
f[x]=Find(f[x]);
r[x]=(r[x]+r[k])%;
}
return f[x];
} int main()
{
scanf("%d%d", &n, &T); int i, d, a, b, fa, fb, ans=; for(i=; i<=n; i++)
{
f[i]=i;
r[i]=;
} while(T--)
{
scanf("%d%d%d", &d, &a, &b);
fa=Find(a), fb=Find(b); if(a>n || b>n || (d==&&a==b))
ans++;
else if(fa==fb && (d-+r[b])%!=r[a])
ans++;
else if(fa!=fb)
{
f[fa]=fb;
r[fa]=((d-)-r[a]+r[b]+)%;
}
} printf("%d\n", ans);
return ;
}
(并查集 建立关系)食物链 -- POJ-- 1182的更多相关文章
- poj 1182:食物链(种类并查集,食物链问题)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44168 Accepted: 12878 Description ...
- poj 2912 并查集(食物链加强版)
题目:给出n个人玩剪刀石头布的游戏,其中有一个人是裁判,剩下的人分为3组,每一组的人只出某一种手型,裁判可以任意出.问是否能判断出哪个人是裁判 链接:点我 分分钟看吐血,先把食物链看懂吧 枚举裁判,然 ...
- 食物链 POJ - 1182 (并查集的两种写法)
这是一个非常经典的带权并查集,有两种写法. 1 边权并查集 规定一下,当x和y这条边的权值为0时,表示x和y是同类,当为1时,表示x吃y,当为2时,表示x被y吃. 一共有三种状态,如图,当A吃B,B吃 ...
- Rochambeau---poj2912||zoj2751(并查集类似于食物链)
题目链接:http://poj.org/problem?id=2912 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1751 ...
- 食物链 poj 1182
C - 食物链 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit Stat ...
- 洛谷 P2024 食物链 POJ 1182 Label:并查集Turbo
题目描述 动物王国中有三类动物 A,B,C,这三类动物的食物链构成了有趣的环形.A 吃 B,B 吃 C,C 吃 A. 现有 N 个动物,以 1 - N 编号.每个动物都是 A,B,C 中的一种,但是我 ...
- 食物链 POJ 1182(种类并查集)
Description 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到 ...
- (并查集 建立关系)Parity game -- POJ -1733
链接: http://poj.org/problem?id=1733 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82830#probl ...
- DisJSet:食物链(POJ 1182)
动物王国的食物链 这一题有两种思路,先介绍第一种: 题目是中文的,我就不翻译了,也很好理解,就是一个A-B-C-A的一个循环的食物链,给定一些条件,问你哪些条件是错的 这一题,是一道比较 ...
随机推荐
- FP-growth算法高效发现频繁项集(Python代码)
FP-growth算法高效发现频繁项集(Python代码) http://blog.csdn.net/leo_xu06/article/details/51332428
- makefile 中的赋值
1. 在makefile 中可以使用后面定义的变量,未定义的变量值为空 = 使用变量时执行赋值操作 := 立即执行赋值操作 ?= 如果没有赋值过,就赋予后面的值 += 将后面的值追加到原来的值后面 参 ...
- Genetics in geographically structured populations: defining, estimating and interpreting FST
摘要:Wright’s F‑statistics, and especially FST, provide important insights into the evolutionary proce ...
- Java的反射和代理以及注解
最近接触到java的反射和代理(接触的有点迟了...),还是有必要总结下 1. Java的反射 有的时候我们需要在程序运行的时候获取类.方法等信息用于动态运行,这个时候反射就能够帮我们找到类.方法.成 ...
- Python bytearray() 函数
Python bytearray() 函数 Python 内置函数 描述 bytearray() 方法返回一个新字节数组.这个数组里的元素是可变的,并且每个元素的值范围: 0 <= x < ...
- swift - 代码创建 pickerView 显示或隐藏横线
import UIKit class VC1: UIViewController { fileprivate lazy var pickerV : UIPickerView = { let v = U ...
- [leetcode]250. Count Univalue Subtrees统计节点值相同的子树
Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...
- 24-filter-拦截器
在web.xml里面配置,有两种形式: 然后编写filter类: @Override public void doFilter(ServletRequest arg0, ServletResponse ...
- 17-js 提交表单以及判空
js
- EditTex
<EditText android:layout_width="match_parent" android:layout_height="100dp" a ...