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/ ...
随机推荐
- angular 绑定数据时添加HTML标签被识别的问题
由于安全性,angular本身会对绑定的HTML标签属性进行转义,所以有些情况下我们需要用到绑定的数据里面传入html标签的时候, 需要用到一个服务:$sce $sce 服务下面的一个 $sce.tr ...
- Swift高阶函数介绍(闭包、Map、Filter、Reduce)
Swift语言有非常多函数式编程的特性.常见的map,reduce,filter都有,初看和python几乎相同,以下简介下 闭包介绍: 闭包是自包括的功能代码块,能够在代码中使用或者用来作为參数传值 ...
- php逻辑操作符中&和&&的异同
php有5种算术操作符(+ - * / %),6种赋值操作符(+= -= *= /= %= .=),8种比较操作符(=== < > <= >= != <> !==) ...
- PHP debug_backtrace() 函数
PHP Error 和 Logging 函数 实例 生成 PHP backtrace: <?php function a($txt) { b("Glenn"); } func ...
- 【BZOJ3782】上学路线 组合数+容斥+CRT
[BZOJ3782]上学路线 Description 小C所在的城市的道路构成了一个方形网格,它的西南角为(0,0),东北角为(N,M).小C家住在西南角,学校在东北角.现在有T个路口进行施工,小C不 ...
- EasyDarwin自动停止推流
原文转自:http://blog.csdn.net/ss00_2012/article/details/51441753 我们使用EasyDarwin的推流转发来进行媒体直播的时候,有时会有这样一个需 ...
- Vue使用axios
main.js------------------- import axios from "axios"; import qs from "qs"; imp ...
- 20181125 test
1.对一些不确定性的东西,还是需要一些勇气进行尝试 2.多阅读blog上的大咖的文章肯定会有提高的
- Maximum Memory and CPU Limitations for Linux Server
grep NR_CPUS /boot/config-`uname -r` [webdev@test apache-flume-1.8.0-bin]$ grep NR_CPUS /boot/config ...
- assign,copy,strong,weak,nonatomic的具体理解
例子: NSString *houseOfMM = [[NSString alloc] initWithString:'MM的三室两厅']; 上面一段代码会执行以下两个动作: 1 在堆上分配一段内存 ...