POJ1182 食物链 【并查集变种】
挺简单的
N个元素扩展为 3*N个
i-A i-B i-C
A吃B吃C吃A
挑战程序设计的89面
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
int N,K;
const int MAX_N=333333;
//并查集
int par[MAX_N];
int rank[MAX_N];//树的高度
void init(int n)
{
for(int i=0;i<n;i++)
{
par[i]=i;
rank[i]=0;
}
}
int find(int x)//
{
if(par[x]==x)
return x;
else
return par[x]=find(par[x]);
}
//合并x和y所属的集合
void unite(int x,int y)
{
x=find(x);
y=find(y);
if(x==y)
return;
if(rank[x]<rank[y])
par[x]=y;
else
{
par[y]=x;
if(rank[x]==rank[y])
rank[x]++;
}
}
//推断x和y是否同属于一个集合
bool same(int x, int y)
{
return find(x)==find(y);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("G:/1.txt","r",stdin);
freopen("G:/2.txt","w",stdout);
#endif
scanf("%d%d",&N,&K);
init(3*N);
int kind,a,b;
int ans=0;
for(int i=0;i<K;i++)
{
scanf("%d%d%d",&kind,&a,&b);
int x=a-1;
int y=b-1;
if(x<0||x>=N||y<0||y>=N)
{
ans++;
continue;
}
if(kind==1)
{
if(same(x,y+N)||same(x,y+2*N))
{
ans++;
continue;
}
unite(x,y);
unite(x+N,y+N);
unite(x+2*N,y+2*N);
}
else
{
if(same(x,y)||same(x+N,y+N)||same(x+2*N,y+2*N)||same(x,y+2*N))
{
ans++;
continue;
}
unite(x,y+N);
unite(x+N,y+2*N);
unite(x+2*N,y);
}
}
printf("%d\n",ans);
return 0;
}
POJ1182 食物链 【并查集变种】的更多相关文章
- POJ-1182 食物链 并查集(互相关联的并查集写法)
题目链接:https://cn.vjudge.net/problem/POJ-1182 题意 中文题目,就不写了哈哈 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃 ...
- [poj1182]食物链(并查集+补集)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 64841 Accepted: 19077 Description ...
- POJ-1182 食物链---并查集(附模板)
题目链接: https://vjudge.net/problem/POJ-1182 题目大意: 中文题,不多说. 思路: 给每个动物创建3个元素,i-A, i-B, i-C i-x表示i属于种类x,并 ...
- poj1182食物链--并查集
动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种.有人用两种说 ...
- POJ1182 食物链 并查集
#include<iostream>#include<stdio.h>#include<string.h>using namespace std;const int ...
- 编程算法 - 食物链 并查集 代码(C)
食物链 并查集 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 有N仅仅动物, 分别编号为1,2,...,N. 全部动物都属于A,B,C中的一种 ...
- POJ1182:食物链(并查集)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 94930 Accepted: 28666 Description ...
- 【poj1182】食物链--并查集扩展域
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 109341 Accepted: 33191 Description 动物 ...
- POJ-1182 分组并查集
今天刚发现,之前做的并查集只是贴模板基本就能过,题意改变一点,自己还是不懂,其实我还没入门呢... 题意:食物链,A吃B,B吃C,C吃A,输入m组数据: 1 a b:a 和 b 是同一类 2 a b: ...
随机推荐
- Why Python?
Python is object-oriented Structure supports such concepts as polymorphism , operation overloading , ...
- Linux经常使用命令(十二) - less
less 工具也是对文件或其他输出进行分页显示的工具.应该说是linux正统查看文件内容的工具.功能极其强大. less 的使用方法比起 more 更加的有弹性.使用了 less 时.更easy用来查 ...
- Linux内核源代码解析之——sock's buffer参数
本文原创为freas_1990,转载请标明出处:http://blog.csdn.net/freas_1990/article/details/11539695 关于socket与sock的关系再简单 ...
- 3.跟我学solr---使用solrj加入索引
上一章讲了怎么使用solr admin向solrserver加入索引,Solr 是一个独立的企业级搜索应用server.它对外提供类似于 Web-service 的 API 接口. 用户能够通过 ht ...
- Windows Azure使用VS 2010的云应用开发过程
原文 Windows Azure使用VS 2010的云应用开发过程 作为技术人员,如果在2010还不知道云计算,那么你已经“OUT”了:作为Visual Studio平台的使用者,如果你不知道VS 2 ...
- 基于visual Studio2013解决面试题之0409判断一个栈是否另外一个栈的弹出序列
题目
- 1.1.6-学习Opencv与MFC混合编程之---播放WAV音乐和 alpha融合功能
源代码:http://download.csdn.net/detail/nuptboyzhb/3961698 Alpha融合菜单项 1. 增加alpha融合菜单项,修改相应的属性,建立类向导 ...
- hive udaf 用maven打包运行create temporary function 时报错
用maven打包写好的jar,在放到hive中作暂时函数时报错. 错误信息例如以下: hive> create temporary function maxvalue as "com. ...
- C# - ref
The ref keyword causes an argument to be passed by reference, not by value. The effect of passing by ...
- NX-bridge,可以实现无线XBee控制的Arduino板
”今天Elecfreaks Studio给你介绍一个新的.很实用的朋友:带有一些奇幻色彩的神秘设备.它是什么呢?它可以完成什么功能呢?它对我们的生活有哪些促进呢?非常感兴趣吧?别着急,我们这就给您详细 ...