并查集的迭代优化-食物链
来源:互联网 发布:虚拟机ubuntu如何分区 编辑:程序博客网 时间:2024/06/16 08:25
#include<cstdio>
#include<iostream>
using namespace std;
int set[300001];
int findx(int a) {
if (set[a]==a) return a;
int t=a;
while (set[a]!=a) {
a=set[a];
}
while (set[t]!=t) {
int t2=t;
t=set[t];
set[t2]=a;
}
return a;
}
bool same(int x,int y){
return findx(x)==findx(y);
}
int unionn(int x,int y){
int fx=findx(x);
int fy=findx(y);
set[fx]=fy;
}
int main(){
int n;
int k;
cin>>n>>k;
for(int i=1;i<=3*n;i++){
set[i]=i;
}
int a,b,c;
int cnt=0;
for(int i=1;i<=k;i++){
cin>>a>>b>>c;
if(b>n||c>n){
cnt++;
continue;
}
if(a==1){
if(same(b,c+n)||same(b,c+2*n)){
cnt++;
continue;
}
else{
unionn(b,c);
unionn(b+n,c+n);
unionn(b+2*n,c+2*n);
}
}
else if(same(b,c)||same(b,c+2*n)){
cnt++;
continue;
}
else{
unionn(b,c+n);
unionn(b+n,c+2*n);
unionn(b+2*n,c);
}
}
cout<<cnt<<endl;
}
#include<iostream>
using namespace std;
int set[300001];
int findx(int a) {
if (set[a]==a) return a;
int t=a;
while (set[a]!=a) {
a=set[a];
}
while (set[t]!=t) {
int t2=t;
t=set[t];
set[t2]=a;
}
return a;
}
bool same(int x,int y){
return findx(x)==findx(y);
}
int unionn(int x,int y){
int fx=findx(x);
int fy=findx(y);
set[fx]=fy;
}
int main(){
int n;
int k;
cin>>n>>k;
for(int i=1;i<=3*n;i++){
set[i]=i;
}
int a,b,c;
int cnt=0;
for(int i=1;i<=k;i++){
cin>>a>>b>>c;
if(b>n||c>n){
cnt++;
continue;
}
if(a==1){
if(same(b,c+n)||same(b,c+2*n)){
cnt++;
continue;
}
else{
unionn(b,c);
unionn(b+n,c+n);
unionn(b+2*n,c+2*n);
}
}
else if(same(b,c)||same(b,c+2*n)){
cnt++;
continue;
}
else{
unionn(b,c+n);
unionn(b+n,c+2*n);
unionn(b+2*n,c);
}
}
cout<<cnt<<endl;
}
0 0
- 并查集的迭代优化-食物链
- 并查集--食物链
- 【并查集】食物链
- 【并查集】食物链
- 【并查集】食物链
- 【并查集】食物链
- 食物链-并查集
- 食物链 并查集
- 食物链 并查集
- 食物链 【并查集】
- 【并查集】食物链
- 【并查集】食物链
- 食物链 -- 并查集
- 并查集的应用:食物链
- 简单的并查集应用食物链
- poj1182 食物链 带权值的并查集
- 食物链 POJ1182 -- 并查集
- poj_1182 食物链、并查集
- swift中用UIAlertController包装一个UIdatePicker
- ucos-II中的一些基本概念②
- 身份证号码验证算法
- 用两个栈实现队列
- linux的chmod用法详解
- 并查集的迭代优化-食物链
- 2017届中兴综合面试
- 中国剩余
- [数据库]数据库索引
- 第一行代码酷欧天气开发(三)
- Linux CentOS中Redis安装
- Lua简明快速入门(1)
- TCP的三次握手+四次挥手,是怎么回事?
- SPWM波在单片机上的实现与调节(基于MSP430F5529)