【原创】poj ----- 1182 食物链 解题报告
题目地址:
http://poj.org/problem?id=1182
题目内容:
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 48791 | Accepted: 14222 |
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
Source
#include <stdio.h> int animal[ ];
int relation[ ];
int n,k;
// 1表示被根节点吃,0表示跟根节点同类,2表示吃根节点 int find_root( int child )
{
if ( animal[ child ] == )
return child;
int tmp = animal[ child ];
animal[ child ] = find_root( animal[ child ] );
relation[ child ] = ( relation[ child ] + relation[ tmp ] ) % ;
return animal[ child ];
} int union_set_and_judge( int one, int two, int rel )
{
if ( rel == )
rel = ;
int fat1 = find_root( one );
int fat2 = find_root( two );
if ( fat1 != fat2 ) {
animal[ fat1 ] = fat2;
int fat2one = ( - relation[ one ] ) % ;
int fat2two = ( fat2one + rel ) % ;
int fat2fat = ( fat2two + relation[ two ] ) % ;
relation[ fat1 ] = fat2fat; return ;
}
int fat2two = ( - relation[ two ] ) % ;
int one2two = ( relation[ one ] + fat2two ) % ; return one2two == rel ? : ;
} int main( void )
{
scanf( "%d%d", &n, &k );
int count = ;
for ( int i = ; i < k; i ++ ) {
int d,x,y;
scanf( "%d%d%d", &d, &x, &y );
if ( x > n || y > n ) {
count ++;
continue;
}
int flag = union_set_and_judge( x, y, d );
if ( flag == ) {
count ++;
}
}
printf( "%d\n", count );
return ;
}
【原创】poj ----- 1182 食物链 解题报告的更多相关文章
- POJ 1182 食物链 -- 解题报告
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 70529 Accepted: 20875 Description ...
- poj 1182 食物链 (带关系的并查集)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44835 Accepted: 13069 Description 动 ...
- POJ 1182——食物链——————【种类并查集】
食物链 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit Status P ...
- POJ 1182 食物链 【并查集】
解题思路:首先是没有思路的----然后看了几篇解题报告 http://blog.csdn.net/ditian1027/article/details/20804911 http://poj.org/ ...
- poj 1182:食物链(种类并查集,食物链问题)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44168 Accepted: 12878 Description ...
- POJ 1182 食物链
G - 食物链 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit Stat ...
- POJ 1182 食物链(种类并查集)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 63592 Accepted: 18670 Description ...
- POJ 2002 Squares 解题报告(哈希 开放寻址 & 链式)
经典好题. 题意是要我们找出所有的正方形.1000点,只有枚举咯. 如图,如果我们知道了正方形A,B的坐标,便可以推测出C,D两点的坐标.反之,遍历所有点作为A,B点,看C,D点是否存在.存在的话正方 ...
- POJ 1182 食物链(经典带权并查集 向量思维模式 很重要)
传送门: http://poj.org/problem?id=1182 食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: ...
随机推荐
- HealthKit教程 Swift版:锻炼信息
原文:HealthKit Tutorial with Swift: Workouts 作者:Ernesto García 译者:Mr_cyz ) 欢迎回到我们的HealthKit系列教程! 在我们系列 ...
- 在防火墙的例外中注册程序(Windows7和XP),改写注册表
在写程序的时候,经常遇到被防火墙拦截的情况,尤其是一些网络程序,不管是对外访问还是外部连接,都会被拦截. 在大多情况下,Windows会静默拦截外部对内的连接访问,而内部对外的访问会提示用户信息. 现 ...
- jquery 设置select的默认值
<select id="sel" > <option value="s1" > aaaa </option> <opt ...
- 14.4.8 Configuring the InnoDB Master Thread IO Rate 配置InnoDB Master Thread I/O Rate
14.4.8 Configuring the InnoDB Master Thread IO Rate 配置InnoDB Master Thread I/O Rate 主的master thread ...
- Python Object Graphs — objgraph 1.7.2 documentation
Python Object Graphs - objgraph 1.7.2 documentation Python Object Graphs¶ objgraph is a module that ...
- 公司内网Debian Server上的openvpnserver搭建并通过SSH隧道转发到广域网
因为戴维营公司在长沙的学员,研发人员和北京的研发人员须要协同研发,故须要让北京的兄弟们增加到长沙办公室的内网,訪问版本号 管理server,于是採用在内网server上搭建一个OpenVPN服务,并把 ...
- Loser tree in Python | Christan Christens
Loser tree in Python | Christan Christens Loser tree in Python I am taking an Advanced Data Structur ...
- android中设置TextView/Button 走马灯效果
在Android的ApiDemo中,有Button的走马灯效果,但是换作是TextView,还是有一点差异. 定义走马灯(Marquee),主要在Project/res/layout/main.xml ...
- mysql 高可用方案MHA介绍
概述 MHA是一位日本MySQL大牛用Perl写的一套MySQL故障切换方案,来保证数据库系统的高可用.在宕机的时间内(通常10—30秒内),完成故障切换,部署MHA,可避免主从一致性问题,节约购买新 ...
- scala 函数编程
scala 函数编程 Effective Scala.pdf: http://www.t00y.com/file/76767869 Functional_Programming_in_Scal ...