POJ-1182-食物链 [种类并查集]
来源:互联网 发布:使命召唤14二战 知乎 编辑:程序博客网 时间:2024/06/05 17:55
题目传送门
思路:
分类并查集,分三个类,主要处理好三个类之间的关系就好了。挑战程序设计竞赛例题。
#include <algorithm>#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>#include <cmath>#include <queue>using namespace std;int fa[200000+100], N, K;void init(){ for (int i = 1; i <= 200000; i++) fa[i] = i; return;}int find(int x){ while (x!=fa[x]) x = fa[x]; return x;}bool same(int x, int y){ return find(x)==find(y);}void unite(int x, int y){ x = find(x); y = find(y); if (x != y) fa[x] = y;}int main(void){ //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); scanf("%d %d", &N, &K); int ans = 0; init(); while (K--) { int x, y, s; scanf("%d %d %d", &s, &x, &y); if (x>N || y>N || x<1 || y<1) { ans++; } else if (s==1) { if (same(x, y+N) || same(x, y+2*N)) ans++; else { unite(x, y); unite(x+N, y+N); unite(x+2*N , y+2*N); } } else if (s==2) { if (same(x, y) || same(x, y+2*N)) ans++; else { unite(x, y+N); unite(x+N, y+2*N); unite(x+2*N, y); } } } printf("%d\n", ans); return 0;}
阅读全文
0 0
- poj 1182 食物链(种类并查集)
- POJ 1182 食物链 种类并查集
- POJ 1182 食物链(种类并查集)
- poj 1182 食物链 (种类并查集)
- POJ 1182 食物链(种类并查集)
- poj 1182 食物链 种类并查集
- poj 1182食物链【种类并查集】
- 食物链 - POJ 1182 种类并查集
- POJ 1182 食物链 (种类并查集)
- POJ 1182 食物链(种类并查集)
- POJ-1182-食物链 [种类并查集]
- poj 1182 食物链(经典!种类并查集)
- POJ 1182 食物链 (种类并查集)
- poj 1182 食物链 (种类并查集经典题)
- POJ 1182 食物链 (三态种类并查集)
- POJ 1182 食物链——种类并查集
- POJ-1182 食物链 (种类并查集)
- POJ 1182 食物链 (种类并查集)
- javaSE--集合框架
- Servlet--ServletContext(二)共享信息
- 善待自己
- dirname和basename用法
- 用SQL数据库做多表关联应怎样设计库结构
- POJ-1182-食物链 [种类并查集]
- 这是个序
- 笔试题2——判断链表是否有环,若有则返回环的第一个交点
- CodeForces
- 产品经理如何编写产品PK测试方案
- iOS 多线程的部分使用
- QT串口通信实例
- Python3基础-字符串
- 16.Scala中包的定义、包对象、包的引用、包的隐式引用代码实战