POJ 1182 食物链(并查集)
经过宝哥的讲解,终于对这种问题有了进一步的理解。根据flag[x]和flag[y]求flag[tx]是最关键的了。
0吃1,1吃2,2吃0.
假设flag[tx] = X;
那么X + flag[x] = flag[y] + 2 (当x吃y的时候)
#include <cstring>
#include <cstdio>
#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int o[];
int flag[];
int ans;
int find(int x)
{
if (x == o[x]) return x;
int t = find(o[x]);
flag[x] = (flag[o[x]] + flag[x]) % ;
return o[x] = t;
}
void judge1(int x,int y)
{
int tx,ty;
tx = find(x);
ty = find(y);
if(tx != ty)
{
o[tx] = ty;
flag[tx] = (flag[y] - flag[x] + )%;
}
else if(tx == ty)
{
if(flag[x] != flag[y])
ans ++;
}
}
void judge2(int x,int y)//x吃y
{
int tx,ty;
tx = find(x);
ty = find(y);
if(tx != ty)
{
o[tx] = ty;
flag[tx] = (flag[y] - flag[x] + )%;
}
else if(tx == ty)
{
if((flag[x]-flag[y]+)% != )
ans ++;
}
}
int main()
{
int n,m,num,sv,ev,i;
scanf("%d%d",&n,&m);
ans = ;
for(i = ; i <= n; i ++)
{
o[i] = i;
flag[i] = ;
}
for(i = ; i < m; i ++)
{
scanf("%d%d%d",&num,&sv,&ev);
if(sv > n||ev > n)
{
ans ++;
continue;
}
if(num == )
{
judge1(sv,ev);
}
else
{
judge2(sv,ev);
}
}
printf("%d\n",ans);
return ;
}
POJ 1182 食物链(并查集)的更多相关文章
- POJ 1182 食物链 [并查集 带权并查集 开拓思路]
传送门 P - 食物链 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit ...
- poj 1182 食物链 并查集 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=1182 题解 可以考虑使用并查集解决 但是并不是简单的记录是否同一组的这般使用 每个动物都有三个并查集 自己 天敌 捕食 并查集 那么在获得 ...
- POJ 1182 食物链(并查集的使用)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 81915 Accepted: 24462 Description ...
- poj 1182 食物链 并查集的又一个用法
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 41584 Accepted: 12090 Descripti ...
- POJ 1182食物链(并查集)
食物链Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 85474 Accepted: 25549Description动物王国中有三 ...
- POJ - 1182 食物链 并查集经典
思路:设r(x)表示节点x与根结点的关系,px表示x的根结点.记录每个节点与其父节点的关系,就能很方便知道每个节点以及和它的父节点的关系. struct node{ int par; //父亲节点 i ...
- poj——1182食物链 并查集(提升版)
因为是中文题,题意就不说了,直接说思路: 我们不知道给的说法中的动物属于A B C哪一类,所以我们可以用不同区间的数字表示这几类动物,这并不影响结果,我们可以用并查集把属于一类的动物放在一块,举个例子 ...
- POJ 1182 食物链 (并查集)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 50601 Accepted: 14786 Description ...
- poj 1182 (关系并查集) 食物链
题目传送门:http://poj.org/problem?id=1182 这是一道关系型并查集的题,对于每个动物来说,只有三种情况:同类,吃与被吃: 所以可以用0,1,2三个数字代表三种情况,在使用并 ...
随机推荐
- shell的while循环
while循环用于不断执行一系列命令,也用于从输入文件中读取数据:命令通常为测试条件.其格式为: while command do Statement(s) to be executed if ...
- 【Luogu】P1383高级打字机
可持久化线段树模板题之一. 权当温习主席树模板 #include<cstdio> #include<cstdlib> #include<cctype> #defin ...
- 【leetcode dp】132. Palindrome Partitioning II
https://leetcode.com/problems/palindrome-partitioning-ii/description/ [题意] 给定一个字符串,求最少切割多少下,使得切割后的每个 ...
- maven配置中国下载源【转:http://www.cnblogs.com/libingbin/p/5949483.html】
修改 配置文件 maven 安装 路径 F:\apache-maven-3.3.9\conf 修改 settings.xml或者在.m2文件夹下新建一个settings.xml 阿里源 <mir ...
- android源码mk文件里的TARGET_OUT指向哪里?
android源码核心变量大都在build/core/envsetup.mk中建立 在该文件中,可以找到 TARGET_OUT := $(PRODUCT_OUT)/$(TARGET_COPY_OUT_ ...
- css解析规则
1.因为css对空格不敏感,因此在每个样式后都要加一个分号,不然会把写在后面的样式当成一个整体来解析,直到遇到分号为止. 2.当遇见不认识的属性或值时,将忽略这个属性,继续解析后面的属性. 3.对于复 ...
- Mybatis resultMap空值映射问题
参考博客:https://www.oschina.net/question/1032714_224673 http://stackoverflow.com/questions/22852383/how ...
- Day 2 操作系统基础
课前复习新知识 RAID(Redundant Arrays of Independent Disks)独立冗余磁盘阵列 定义:加州大学伯克利分校1987年提出,最初是为了组合小的廉价磁盘来代替大的昂贵 ...
- LCA rmq st model
LCA:倍增 memset(p,-,sizeof(p)); inline void dfs(int u) { ;i=e[i].next) { int v=e[i].v; ) { deep[v]=dee ...
- CODEVS 1245 最小的N个和 堆+排序
原题链接 http://codevs.cn/problem/1245/ 题目描述 Description 有两个长度为 N 的序列 A 和 B,在 A 和 B 中各任取一个数可以得到 N^2 个和,求 ...