首页 > 其他分享 >POJ 1182 食物链(种类并查集)

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

时间:2023-04-13 17:41:55浏览次数:55  
标签:bin f1 f2 int 查集 1182 rank POJ include

题目地址:POJ 1182

一道很经典的种类并查集的题目。利用互相之间的关系来进行权值的维护。

代码如下:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm>

using namespace std;
int bin[60000], rank[60000];
int find1(int x)
{
    int y;
    if(bin[x]!=x)
    {
        y=bin[x];
        bin[x]=find1(bin[x]);
        rank[x]=(rank[x]+rank[y])%3;
    }
    return bin[x];
}
int main()
{
    int n, k, p, x, y, i, flag, f1, f2, cnt=0;
    scanf("%d%d",&n,&k);
    for(i=1;i<=n;i++)
    {
        bin[i]=i;
        rank[i]=0;
    }
    while(k--)
    {
        flag=0;
        scanf("%d%d%d",&p,&x,&y);
        if(x>n||y>n||(p==2&&x==y))
        {
            cnt++;
            continue ;
        }
        f1=find1(x);
        f2=find1(y);
        if(p==1)
        {
            if(f1==f2)
            {
                if(rank[x]!=rank[y])
                    cnt++;
            }
            else
            {
                bin[f2]=f1;
                rank[f2]=(rank[x]+3-rank[y])%3;
            }
        }
        else
        {
            if(f1==f2)
            {
                if(rank[x]!=(rank[y]+1)%3)
                    cnt++;
            }
            else
            {
                bin[f2]=f1;
                rank[f2]=(rank[x]+2-rank[y])%3;
            }
        }
    }
    printf("%d\n",cnt);
    return 0;
}


标签:bin,f1,f2,int,查集,1182,rank,POJ,include
From: https://blog.51cto.com/u_16070138/6188246

相关文章

  • POJ 1703 Find them, Catch them(种类并查集)
    题目地址:POJ1703种类并查集水题。代码如下:#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<stdlib.h>#include<math.h>#include<ctype.h>#include<queue>#include<map>#includ......
  • POJ 1988 Cube Stacking (种类并查集)
    题目地址:POJ1988   这道题的查找合并的方法都能想的到,就是一点没想到,我一直天真的以为查询的时候,输入后能马上输出,这样的话在合并的时候就要所有的结点值都要算出来,但是经过路径压缩之后,没办法全部都处理到,如果不压缩妥妥的TLE。。于是看了看网上的题解。才发现自己是多......
  • HDU 1856 More is better(并查集+离散化)
    题目地址:HDU1856水题。由于标号范围太大,而数据数只有10w,所以要先进行离散化。然后就是裸的并查集了。代码如下:#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<stdlib.h>#include<math.h>#include<ctype.h>#include<que......
  • HDU 2473 Junk-Mail Filter(并查集的删除操作)
    题目地址:HDU2473这题以前碰到过,没做出来。。现在又做了做,还是没做出来。。、、这题涉及到并查集的删除操作。想到了设一个虚节点,但是我把虚节点设为了要删除的点的父节点,一直是栈溢出,目测是无限递归了。看了看别人的做法,原来只要建一个映射就可以了,虚节点是作为的新的映射,每......
  • HDU 2120 Ice_cream's world I(并查集)
    题目地址:HDU2120这题虽然字数不多,但就是看不懂。。意思是求最多有多少个被墙围起来的区域。显然就是求环的个数。然后用并查集求环个数就可以了。代码如下:#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<stdlib.h>#include<math......
  • Java常用实体类介绍:POJO、Domain、DO、DTO、VO
    POJOPOJO是PlainOldJavaObject的简称,它指的是一个没有限制或要求下的纯平对象。POJO用于表示没有任何框架或技术限制的纯数据对象。在Java开发中,POJO通常用于简化复杂对象和降低对象的耦合度,是面向对象编程中"高内聚、低耦合"设计思想的体现。示例代码:@Datapublic......
  • UVa 706 / POJ 1102 LCD Display (模拟)
    706-LCDDisplayTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=647http://poj.org/problem?id=1102Afriendofyouhasjustboughtanewcomputer.Untilno......
  • UVa 757 / POJ 1042 / East Central North America 1999 Gone Fishing (枚举&贪心&想
    757-GoneFishingTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=698http://poj.org/problem?id=1042Johnisgoingonafishingtrip.Hehas h hoursavailable( ),andther......
  • UVa 113 / POJ 2109 Power of Cryptography (使用double处理大整数&泰勒公式与误差分
    113-PowerofCryptographyTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=99&page=show_problem&problem=49http://poj.org/problem?id=2109题意:给出n和p,求出 ,但是p可以很大()如何存储p?不用大数可不可以?先看看double......
  • UVa 443 / POJ 2247 Humble Numbers (4因子-丑数&STL灵活运用)
    443-HumbleNumbersTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=384http://poj.org/problem?id=2247Anumberwhoseonlyprimefactorsare2,3,5or7isc......