POJ 1182 食物链 (破题)
题意:中文题。
析:对POJ 真是无语,有的题用G++过不了,C++能过,有的题不写EOF(题目明明说就一组的数据的)不过,有的题写EOF也不过,
这个题就是写EOF就过不了。。。
这个题用的是加权并查集,用一个r[i] 表示子结点和根结点的关系,为0,表示为一类,为1表示是能吃根结点,为2表示被根结点吃。
这个题目还有一个非常重要的一个条件,A吃B, B吃C,C吃A。如果给定了两组关系了,那么第三组是一定成立的。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<double, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 10;
const int mod = 1e6;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
int p[maxn], r[maxn]; int Find(int x){
if(x == p[x]) return x;
int tmp = p[x];
p[x] = Find(p[x]);
r[x] = (r[x] + r[tmp]) % 3;
return p[x];
} int main(){
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; ++i) p[i] = i, r[i] = 0;
int ans = 0;
while(m--){
int op, a, b;
scanf("%d %d %d", &op, &a, &b);
if(a > n || b > n){ ++ans; continue; }
if(op == 2 && a == b){ ++ans; continue; }
int x = Find(a);
int y = Find(b);
if(op == 1){
if(x == y){
if(r[a] != r[b]) ++ans;
}
else {
p[y] = x;
r[y] = (3 + r[a] - r[b]) % 3;
}
}
else{
if(x == y){
if((r[a] - r[b] + 3) % 3 != 1) ++ans;
}
else{
p[y] = x;
r[y] = (5 + r[a] - r[b]) % 3;
}
}
}
printf("%d\n", ans);
return 0;
}
POJ 1182 食物链 (破题)的更多相关文章
- poj 1182 食物链 (带关系的并查集)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44835 Accepted: 13069 Description 动 ...
- POJ 1182 食物链(种类并查集)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 63592 Accepted: 18670 Description ...
- POJ 1182 食物链 [并查集 带权并查集 开拓思路]
传送门 P - 食物链 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit ...
- 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 食物链(经典带权并查集 向量思维模式 很重要)
传送门: http://poj.org/problem?id=1182 食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: ...
- POJ 1182——食物链——————【种类并查集】
食物链 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit Status P ...
- 【原创】poj ----- 1182 食物链 解题报告
题目地址: http://poj.org/problem?id=1182 题目内容: 食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submi ...
- POJ 1182 食物链 【并查集】
解题思路:首先是没有思路的----然后看了几篇解题报告 http://blog.csdn.net/ditian1027/article/details/20804911 http://poj.org/ ...
随机推荐
- 用python模拟TCP3次握手连接及发送数据
源码如下: from scapy.all import * import logging logging.getLogger('scapy.runtime').setLevel(logging.ERR ...
- On ROWNUM and Limiting Results
This issue's Ask Tom column is a little different from the typical column. I receive many questions ...
- mnesia练习及基本操作
Mnesia基本用法 查看表结构 查看mnesia表的结构: mnesia:info(). 查看此表的基本信息: mnesia:table_info(<tableName>, all). ...
- 游戏开发之基础图像---3d图像处理
http://dev.gameres.com/Program/Visual/3D/3Darit.htm float 类型数据有效数字是小数点后面6位 单精度 doluble 类型数据是至少10位,双精 ...
- jquery 效果网址分享
http://www.lanrentuku.com/js/ http://www.baidu.com/link?url=2nuImAliKGCKyDeJ7ln2DR_2if5uKgr-em6a3dx ...
- sql server t-sql脚本转成oracle plsql
将一份SQL SERVER数据库生成的T-SQL脚本,转成ORACLE的PL/SQL,其复杂繁琐程度,远远出乎我的意料. 这份SQL SERVER脚本,里面有表,有视图,还有存储过程,以及一些自定义函 ...
- php 中 isset 和empty 的区别
昨天终于开始学习php了,这个对于我来说听着很熟悉,但是学起来很陌生的东西,尤其是课上能听明白 但是真正写起了手生,都不知道手该往哪里放了,天哪~~~ 其中课上有讲到 isset和empty的区别,现 ...
- bc - An arbitrary precision calculator language
bc(1) General Commands Manual bc(1) NAME bc - An arbitrary precision calculator language SYNTAX bc [ ...
- Nodejs通过Thrift操作hbase卡住原因分析及与javascript的垃圾回收机制的关系
在最近使用Nodejs通过Thrift操作hbase的时候写了个脚本,不断发送http请求,从而取得hbase下所需的数据,但是在run的过程中for循环并没有执行完全,在执行一部分后会卡住,就再也进 ...
- git命令行删除远程分支
先查看远程分支 git branch -r 使用下面两条命令来删除远程分支 git branch -r -d origin/branch-name git push origin :branch-na ...