POJ 1703解题思路【并查集】+ poj 1182 食物链

版权声明:个人笔记,仅供复习 https://blog.csdn.net/weixin_42373330/article/details/84591491
题目链接 http://poj.org/problem?id=1703d

这个题目用并查集处理,union的是后要是用些方法:比如

  • D 1 2 和D 2,3,D 1 2就将1和2+n合并为一个集合,然后再将1+n和2合并为另一个集合,对于D 2 3同样,此时一个集合为(1, 2+n, 3),另一个集合为(1+n, 2, 3+n)。 对于A x y来说,我们只要查找x和y的根节点,若相同说明在同一个集合,若查找x+n 与y的根节点相同,说明x和y是不在同一个集合里(可以想想union的操作)

这里附上另一种想法的博客 典型并查集:判断在不同的集合


所谓带权并查集,指的是在合并时不仅要将区间合并,而且要记录,更新,维护某些信息,是子与父间的一种关系,分别在find和union里面进行更新.

题目链接:poj 1182 食物链

食物链
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 95419 Accepted: 28803
Description

动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形。A吃B, B吃C,C吃A。
现有N个动物,以1-N编号。每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种。
有人用两种说法对这N个动物所构成的食物链关系进行描述:
第一种说法是"1 X Y",表示X和Y是同类。
第二种说法是"2 X Y",表示X吃Y。
此人对N个动物,用上述两种说法,一句接一句地说出K句话,这K句话有的是真的,有的是假的。当一句话满足下列三条之一时,这句话就是假话,否则就是真话。
1) 当前的话与前面的某些真的话冲突,就是假话;
2) 当前的话中X或Y比N大,就是假话;
3) 当前的话表示X吃X,就是假话。
你的任务是根据给定的N(1 <= N <= 50,000)和K句话(0 <= K <= 100,000),输出假话的总数。
Input

第一行是两个整数N和K,以一个空格分隔。
以下K行每行是三个正整数 D,X,Y,两数之间用一个空格隔开,其中D表示说法的种类。
若D=1,则表示X和Y是同类。
若D=2,则表示X吃Y。
Output

只有一个整数,表示假话的数目。
Sample Input

100 7
1 101 1
2 1 2
2 2 3
2 3 3
1 1 3
2 3 1
1 5 5
Sample Output

3
Source

Noi 01


下面代码中unite函数中的图:

rr
关系1
关系2
fx与fy的关系
x
y
fx
fy

fx到fy的关系为

-1*关系1
rr
关系2
fx
x
y
fy

所以fx到fy的关系为r[fx]=(r[y]-r[x]+rr+3)%3;(加3是为了防止负数)

扫描二维码关注公众号,回复: 4499857 查看本文章

ac的c++语言程序如下:

#pragma comment(linker, "/STACK:10240000,10240000")
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
const int N=5e4+10;
int fa[N];
int r[N];//r[i]表示i与其父节点的关系 等于0 - 同类;1 - 吃; 2 - 被吃


int find(int x)//查找节点
{
    if(fa[x]==x) return x;
    else
    {
        int t=fa[x];
        fa[x]=find(fa[x]);
        r[x]=(r[x]+r[t])%3;
        /*
        ----------------------------------------------------
        |任意的两者的关系只有三种:同类(0);吃(1);被吃(2);|
        ----------------------------------------------------
        r[t]表示的是x的父节点t与t的父根节点的关系
        r[x]表示的是x与x的父节点t的关系
        故x与t的父根节点的关系为r[x]=(r[x]+r[t])%3;
        */
        return fa[x];

    }

}
void unite(int x,int y,int rr)
{
    int fx=find(x),fy=find(y);
    if(fx!=fy)
        fa[fx]=fy;
    r[fx]=(r[y]-r[x]+rr+3)%3;
    /*
    ----------------------------------------------------
    |任意的两者的关系只有三种:同类(0);吃(1);被吃(2);|
    ----------------------------------------------------
    x与y的关系是rr的值
    x与fx的关系是r[x]
    y与fy的关系是r[y]
    则fx与fy的关系r[fx]=(r[y]-r[x]+rr+3)%3;
    ---
    图:
     x---->y
     |     |
     |     |
     \/    \/
     fx--->fy
     故r[fx]=(r[y]-r[x]+rr+3)%3;
    */


}

int main()
{
    int  x, y;
    int c;
    int n, k;
    scanf("%d%d",&n, &k);


    for(int i=1;i<=n;i++)
    {
       fa[i]=i;
       r[i]=0;//自己与自己是同类
    }
    int ans=0;
    while(k--)
    {
        scanf("%d%d%d",&c, &x, &y);

        if(x<=0||y<=0||x>n||y>n)//对应条件2
            ans++;
        else
        {
            if(c==1)
            {
                if(find(x)==find(y))
                {

                    if(r[x]!=r[y]) ans++;
                }
                else
                {
                    unite(x,y,0);
                }


            }
            else
            {
                if(x==y) ans++;
                else if(find(x)==find(y))
                {
                    if((r[x]-r[y]+3)%3==1)  continue;
                    else ans++;
                }
                else
                {
                    unite(x,y,1);
                }

            }

        }

    }

    printf("%d\n",ans);
    return 0;
}

题目链接: How Many Answers Are Wrong

How Many Answers Are Wrong
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 16425 Accepted Submission(s): 5761

Problem Description
TT and FF are … friends. Uh… very very good friends -________-b

FF is a bad boy, he is always wooing TT to play the following game with him. This is a very humdrum game. To begin with, TT should write down a sequence of integers-_-!!(bored).

Then, FF can choose a continuous subsequence from it(for example the subsequence from the third to the fifth integer inclusively). After that, FF will ask TT what the sum of the subsequence he chose is. The next, TT will answer FF’s question. Then, FF can redo this process. In the end, FF must work out the entire sequence of integers.

BoringBoringa very very boring game!!! TT doesn’t want to play with FF at all. To punish FF, she often tells FF the wrong answers on purpose.

The bad boy is not a fool man. FF detects some answers are incompatible. Of course, these contradictions make it difficult to calculate the sequence.

However, TT is a nice and lovely girl. She doesn’t have the heart to be hard on FF. To save time, she guarantees that the answers are all right if there is no logical mistakes indeed.

What’s more, if FF finds an answer to be wrong, he will ignore it when judging next answers.

But there will be so many questions that poor FF can’t make sure whether the current answer is right or wrong in a moment. So he decides to write a program to help him with this matter. The program will receive a series of questions from FF together with the answers FF has received from TT. The aim of this program is to find how many answers are wrong. Only by ignoring the wrong answers can FF work out the entire sequence of integers. Poor FF has no time to do this job. And now he is asking for your help~(Why asking trouble for himself~~Bad boy)

Input
Line 1: Two integers, N and M (1 <= N <= 200000, 1 <= M <= 40000). Means TT wrote N integers and FF asked her M questions.

Line 2…M+1: Line i+1 contains three integer: Ai, Bi and Si. Means TT answered FF that the sum from Ai to Bi is Si. It’s guaranteed that 0 < Ai <= Bi <= N.

You can assume that any sum of subsequence is fit in 32-bit integer.

Output
A single line with a integer denotes how many answers are wrong.

Sample Input
10 5
1 10 100
7 10 28
1 3 32
4 6 41
6 6 1

Sample Output
1

Source
2009 Multi-University Training Contest 13 - Host by HIT

这题做法和上面的那题差不多

ac的c++语言程序如下:


/* 带权并查集 */
#pragma comment(linker, "/STACK:10240000,10240000")
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
const int N=2e5+10;
const int mod=1e9+7;
#define ll long long
int fa[N];
int r[N];
int find(int x)
{
    if(fa[x]==x) return x;
    else
    {
        int tem=fa[x];
        fa[x]=find(fa[x]);
        r[x]=(r[x]+r[tem]);// x到x爷爷的距离=x到父亲的距离+父亲到爷爷的距离
        return fa[x];
    }
}
int ans;
void unite(int x, int y, int w)
{
    int fx=find(x);
    int fy=find(y);
    if(fx==fy)//说明x,y同根,若r[x]+w!=r[y] 说明是假话,ans++
    {
        if(r[x]+w!=r[y])
            ans++;
        return ;
    }
    else
    {
        fa[fy]=fx;
        r[fy]=r[x]+w-r[y];
        //fy到fx的距离等于fy到y的距离(-r[y]) + y到x的距离(w) + x到fx的距离(r[x]); 画画图就可理解了
    }

}
int main()
{
    int n, k;
    int x, y, w;
    while(~scanf("%d%d",&n,&k))
    {
        ans=0;
        for(int i=0;i<=n+1;i++)
        {
            fa[i]=i;
            r[i]=0;
        }
        while(k--)
        {
            scanf("%d%d%d",&x,&y, &w);
            unite(x,y+1,w); //更新x,y间(含x和y)的距离
        }
        printf("%d\n",ans);
    }





    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42373330/article/details/84591491