POJ 1182 食物链 [并查集 带权并查集 开拓思路]
Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u
System Crawler (2015-01-27)
Description
现有N个动物,以1-N编号。每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种。
有人用两种说法对这N个动物所构成的食物链关系进行描述:
第一种说法是"1 X Y",表示X和Y是同类。
第二种说法是"2 X Y",表示X吃Y。
此人对N个动物,用上述两种说法,一句接一句地说出K句话,这K句话有的是真的,有的是假的。当一句话满足下列三条之一时,这句话就是假话,否则就是真话。
1) 当前的话与前面的某些真的话冲突,就是假话;
2) 当前的话中X或Y比N大,就是假话;
3) 当前的话表示X吃X,就是假话。
你的任务是根据给定的N(1 <= N <= 50,000)和K句话(0 <= K <= 100,000),输出假话的总数。
Input
以下K行每行是三个正整数 D,X,Y,两数之间用一个空格隔开,其中D表示说法的种类。
若D=1,则表示X和Y是同类。
若D=2,则表示X吃Y。
Output
Sample Input
100 7
1 101 1
2 1 2
2 2 3
2 3 3
1 1 3
2 3 1
1 5 5
Sample Output
3
先吐槽一下关于多组样例的问题,闲话也不多说,这题网上大牛各种神奇的题解都有,第一份代码是常规带权并查集的做法,不多谈,我主要想谈一下第二份代码的思路。
如果我们换一个思路,把1--n看成与自身同类的集合,1+n--2*n看成自己吃的集合,1+2*n--3*n看成吃自己的集合,那么问题便简单多了~~~
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<string> #define N 50005
#define M 105
#define mod 1000000007
//#define p 10000007
#define mod2 1000000000
#define ll long long
#define LL long long
#define eps 1e-6
#define inf 100000000
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int n,m;
int f[N];
int r[N];
int ans; void ini()
{
ans=;
int i;
for(i=;i<=n;i++){
f[i]=i;
r[i]=;
}
} int find(int x)
{
int fa;
if(f[x]!=x)
{
fa=find(f[x]);
r[x]=(r[x]+r[ f[x] ])%;
f[x]=fa;
}
return f[x];
} void merge(int x,int y,int d)
{
int a,b;
a=find(x);
b=find(y);
if(a==b){
return;
}
f[b]=a;
r[b]=(-r[y]+r[x]+d)%;
} void solve()
{
int d,x,y;
int a,b;
int re;
int i;
for(i=;i<=m;i++){
scanf("%d%d%d",&d,&x,&y);
if(x>n || y>n){
ans++;continue;
}
if(d== && x==y){
ans++;continue;
}
a=find(x);
b=find(y);
re=(-r[x]+r[y])%;
if(a==b && re!=d-){
ans++;continue;
}
//printf(" i=%d\n",i);
merge(x,y,d-);
}
} void out()
{
printf("%d\n",ans);
} int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
//scanf("%d",&T);
//for(int ccnt=1;ccnt<=T;ccnt++)
//while(T--)
//while(scanf("%d%d",&n,&m)!=EOF)
scanf("%d%d",&n,&m);
{
ini();
solve();
out();
}
return ;
}
如果我们换一个思路,把1--n看成与自身同类的集合,1+n--2*n看成自己吃的集合,1+2*n--3*n看成吃自己的集合,那么问题便简单多了~~~
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<string> #define N 50005
#define M 105
#define mod 1000000007
//#define p 10000007
#define mod2 1000000000
#define ll long long
#define LL long long
#define eps 1e-6
#define inf 100000000
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int n,m;
int f[*N];
int ans; void ini()
{
ans=;
int i;
for(i=;i<=*n;i++){
f[i]=i;
}
} int find(int x)
{
int fa;
if(f[x]!=x)
{
fa=find(f[x]);
f[x]=fa;
}
return f[x];
} void merge(int x,int y)
{
int a,b;
a=find(x);
b=find(y);
if(a==b){
return;
}
f[b]=a;
} void solve()
{
int d,x,y;
int a,b;
int fx,sx;
int i;
for(i=;i<=m;i++){
scanf("%d%d%d",&d,&x,&y);
if(x>n || y>n){
ans++;continue;
}
if(d== && x==y){
ans++;continue;
}
a=find(x);
b=find(y);
fx=find(x+n);
sx=find(x+*n);
if(d==){
if(b==fx || b==sx){
ans++;continue;
}
else{
merge(x,y);
merge(x+n,y+n);
merge(x+*n,y+*n);
}
}
else{
if(b==a || b==sx){
ans++;continue;
}
else{
merge(x+n,y);
merge(x+*n,y+n);
merge(x,y+*n);
}
}
}
} void out()
{
printf("%d\n",ans);
} int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
//scanf("%d",&T);
//for(int ccnt=1;ccnt<=T;ccnt++)
//while(T--)
//while(scanf("%d%d",&n,&m)!=EOF)
scanf("%d%d",&n,&m);
{
ini();
solve();
out();
}
return ;
}
POJ 1182 食物链 [并查集 带权并查集 开拓思路]的更多相关文章
- poj 1182 食物链(高级的带权并查集)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 76486 Accepted: 22742 Description ...
- 浅谈并查集&种类并查集&带权并查集
并查集&种类并查集&带权并查集 前言: 因为是学习记录,所以知识讲解+例题推荐+练习题解都是放在一起的qvq 目录 并查集基础知识 并查集基础题目 种类并查集知识 种类并查集题目 并查 ...
- 【POJ 1984】Navigation Nightmare(带权并查集)
Navigation Nightmare Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40 ...
- Poj1182 食物链(并查集/带权并查集)
题面 Poj 题解 这里采用并查集的补集. \(x\)表示同类集合,\(x+n\)表示敌人集合,\(x+n\times2\)表示敌人的敌人集合. 如果当前给出的是一对同类关系,就判断\(x\)是否吃\ ...
- POJ 1984 Navigation Nightmare 【经典带权并查集】
任意门:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K To ...
- [poj 2912] Rochambeau 解题报告 (带权并查集)
题目链接:http://poj.org/problem?id=2912 题目: 题目大意: n个人进行m轮剪刀石头布游戏(0<n<=500,0<=m<=2000) 接下来m行形 ...
- poj 1733 Parity game【hash+带权并查集】
hash一下然后用带权并查集做模2下的前缀和 #include<iostream> #include<cstdio> #include<map> #include& ...
- POJ 2492 A Bug's Life 带权并查集
题意: 思路: mod2 意义下的带权并查集 如果两只虫子是异性恋,它们的距离应该是1. 如果两只虫子相恋且距离为零,则它们是同性恋. (出题人好猥琐啊) 注意: 不能输入一半就break出来.... ...
- POJ 2492 A Bug's Life (带权并查集 && 向量偏移)
题意 : 给你 n 只虫且性别只有公母, 接下来给出 m 个关系, 这 m 个关系中都是代表这两只虫能够交配, 就是默认异性, 问你在给出的关系中有没有与异性交配这一事实相反的, 即同性之间给出了交配 ...
随机推荐
- vue2.0动画
相对于vue1.0来说,vue2.0的动画变化还是挺大的, 在1.0中,直接在元素中加 transition ,后面跟上名字. 而在vue2.0中,需要把设置动画的元素.路由放在<transit ...
- 苹果电脑macbook怎样强制关闭软件
尝试快捷键Command+Q 选择当前处于界面最前端的应用,同时按住Command+Q退出程序,并不代表强制退出应用,主要用于一些假死的应用. 2 通过快捷键Command+option+Shift+ ...
- PAT (Basic Level) Practise (中文)-1029. 旧键盘(20)
PAT (Basic Level) Practise (中文)-1029. 旧键盘(20) http://www.patest.cn/contests/pat-b-practise/1029 旧键盘上 ...
- javascript的offset、client、scroll使用方法
offsetTop 指元素距离上方或上层控件的位置,整型,单位像素. offsetLeft 指元素距离左方或上层控件的位置,整型,单位像素. offsetWidth 指元素控件自身的宽度,整型,单位像 ...
- 674. Longest Continuous Increasing Subsequence@python
Given an unsorted array of integers, find the length of longest continuous increasing subsequence (s ...
- JS面试题--使用for循环打印1-10,每个数字出现的间隔约500ms
又来刷面试题啦,哈哈. 要求:使用for循环打印1-10,每个数字出现的间隔约500ms. 分析:考察点--闭包,块级作用域 方式一.使用闭包+立即执行函数,自己当时的思路也是这样想的,但是,结果却没 ...
- Java 局部变量未初始化会报错,局部变量没有初始值,成员变量有初始值
Java 成员变量有初始值,而局部变量没有初始值. 如下所示,代码不能编译通过 public static void main(String[] args){ String s; Sy ...
- Ubuntu 下的aegisub安装
大家用开源的软件用习惯了.推荐大家使用一下字幕编辑软件 分享的是Ubuntu下的安装教程: $ sudo add-apt-repository ppa:djcj/aegisub $ sudo apt- ...
- 浮动的label
在web项目中,有一个很重的模块就是登陆/注册模块,这个模块的主体部分就是一个form表单,这个form表单包含两个重要input组(用户名/密码),每个input组都包含label和input,而关 ...
- CSS3的-字体 @font-face
http://www.w3cplus.com/content/css3-font-face http://www.w3cplus.com/css3/web-icon-with-font-face ht ...