题意:中文题。

析:对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 食物链 (破题)的更多相关文章

  1. poj 1182 食物链 (带关系的并查集)

      食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44835 Accepted: 13069 Description 动 ...

  2. POJ 1182 食物链(种类并查集)

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 63592   Accepted: 18670 Description ...

  3. POJ 1182 食物链 [并查集 带权并查集 开拓思路]

    传送门 P - 食物链 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit  ...

  4. poj 1182:食物链(种类并查集,食物链问题)

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44168   Accepted: 12878 Description ...

  5. POJ 1182 食物链

    G - 食物链 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Stat ...

  6. POJ 1182 食物链(经典带权并查集 向量思维模式 很重要)

    传送门: http://poj.org/problem?id=1182 食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: ...

  7. POJ 1182——食物链——————【种类并查集】

    食物链 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status P ...

  8. 【原创】poj ----- 1182 食物链 解题报告

    题目地址: http://poj.org/problem?id=1182 题目内容: 食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submi ...

  9. POJ 1182 食物链 【并查集】

    解题思路:首先是没有思路的----然后看了几篇解题报告 http://blog.csdn.net/ditian1027/article/details/20804911 http://poj.org/ ...

随机推荐

  1. python3短信接口使用

    import http.client from urllib import parse host = "106.ihuyi.com" sms_send_uri = "/w ...

  2. 2016年最值得新手程序猿阅读的书:《增长project师指南》

    这本书的来源于根据我在<Repractise简单介绍篇:Web开发的七天里>中所说的 Web 开发的七个步骤而展开的电子书.当然它也是一个 APP.它一本关于怎样成为增长project师的 ...

  3. 01 json方式封装通信接口

    新建一个json_api.php<?php class Response{ /** *按json方式输出通信 *@param integet $code 状态码 *@param string $ ...

  4. SQL Cursor生命周期

      阅读导航 1 Cursor Step 1.1 Create cursor 1.2 Parse statement 1.3 descript and define 1.4 Bind variable ...

  5. 第 2 章 第 1 题 同位词问题 下问 Multimap实现

    问题分析 输入:一个任意的单词和一个内含多个乱序单词的字典文件 输出:该单词在字典中的所有同位词 约束:允许事先对字典进行预处理 解决思路 上问的程序有个缺点 - 我们必须遍历完整个字典文件才能输出所 ...

  6. MongoDB 操作手冊CRUD 事务 两步提交

    运行两步提交 概述 这部分提供了多记录更新或者多记录事务.使用两步提交来完毕多记录写入的模板. 另外.能够扩展此方法来提供rollback-like功能. 背景 MongoDB对于单条记录的操作是原子 ...

  7. 使用Entity Framework和WCF Ria Services开发SilverLight之6:查找指定字段

    对数据库表指定字段的查找,又是实际工作中的一项必要工作.SL客户端仅获取实际需要的指定的字段,好处很多,比如:有助于减少网络流量. 有两类这样的使用场景. 1:联表查询不需要外键表 在上一篇中,我们使 ...

  8. 【R】R语言生成随机数

    1.概述 作为一种语言进行统计分析,R有一个随机数生成各种统计分布功能的综合性图书馆.R语言可以针对不同的分布,生成该分布下的随机数.其中,有许多常用的个分布可以直接调用.本文简单介绍生成常用分布随机 ...

  9. Python升级已经安装的第三方库

    Python升级已经安装的第三方库 主要两步操作,查看需要升级库,升级库.如下: pip list # 列出安装的库 pip list --outdated # 列出有更新的库 pip install ...

  10. ALSA lib调用实例

    1. Display Some PCM Types and Formats 2. Opening PCM Device and Setting Parameters /* This example o ...