poj 1703 Find them, Catch them

点击打开链接poj 1703

思路: 简单带权并查集
分析:
1 题目要询问给定的x和y是否是同一个动物
2 我们假设如果不同,那么权值为1,否则为0。因此对于给定的x和y,那么如果x和y不在一个集合里面那么就是不确定,否则就可以根据rank来判断是什么关系

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int MAXN = 1e5+10;

int father[MAXN];
int rank[MAXN];

void init(int n){
    memset(rank , 0 , sizeof(rank));
    for(int i = 1 ; i <= n ; i++)
        father[i] = i;
}

int find(int x){
    if(x != father[x]){
        int fa = father[x];
        father[x] = find(fa);
        rank[x] += rank[fa];
        rank[x] %= 2;
    }
    return father[x];
}

void output(int x , int y , int fx , int fy){
    if(fx != fy)
        puts("Not sure yet.");
    else{
        if(rank[x] == rank[y])
            puts("In the same gang.");
        else
            puts("In different gangs.");
    }
}

int main(){
    char ch;
    int x , y;
    int cas , n , m;
    scanf("%d" , &cas);
    while(cas--){
        scanf("%d%d%*c" , &n , &m);
        init(n);
        while(m--){
            scanf("%c %d %d%*c" , &ch , &x , &y);
            int fx = find(x);
            int fy = find(y);
            if(ch == 'A'){
                output(x , y , fx , fy);
            }
            else{
                if(fx != fy){
                    father[fx] = fy;
                    rank[fx] = (rank[y]+1-rank[x]+2)%2;
                }
            }
        }
    }
    return 0;
}
时间: 2024-09-20 09:27:10

poj 1703 Find them, Catch them的相关文章

poj 1703 Find them, Catch them:种类并查集

链接: http://poj.org/problem?id=1703 题目: Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22289   Accepted: 6648 Description The police office in Tadu City decides to say ends to the chaos, as launch actions to root up

并查集专题【完结】

个人整理 并查集 [poj] 第一题 poj 1182 食物链 点击打开链接poj 1182 思路: 带权并查集 分析: 1 典型的带权并查集的题目 2 如果x和y是同类的关系认为是0,如果是x吃y那么关系认为是1,那么x和y的关系为2就说明y吃x 3 那么如果x和y同类则rank[x] == rank[y] , 如果x吃y那么rank[x] == (rank[y]+1)%3 , 注意mod3的使用 点击查看代码 第二题 poj 1308 Is It A Tree? 点击打开链接 poj 130

数据结构专题

打星号的表示个人认为比较经典,或是算法比较好的题目   1195 Mobile phones 树状数组 1455 1521 Entropy huffman 1703 Find them, Catch them 并查集 1785 Binary Search Heap Construction 1794 Castle Walls 逆序对 1961 Period KMP重复因子 1984* Navigation Nightmare 并查集+坐标平移 1986* Distance Queries LCA

poj 3278 Catch That Cow

点击打开链接 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 62146   Accepted: 19460 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤

POJ题目分类

初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      (4)递推.      (5)构造法.(poj3295)      (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996) 二.图算法:      (1)图的深度优先遍历和广度优先遍历.      (2)最短路径算法(dijkstra,bellman-ford

POJ 2385 Apple Catching (DP)

Description It is a little known fact that cows love apples. Farmer John has two apple trees (which are conveniently numbered 1 and 2) in his field, each full of apples. Bessie cannot reach the apples when they are on the tree, so she must wait for t

UVa 757 / POJ 1042 Gone Fishing: 枚举&amp;amp;贪心&amp;amp;想法题&amp;amp;优先队列

757 - Gone Fishing Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=698 http://poj.org/problem?id=1042 John is going on a fishing trip. He has h hours available ( ), and t

poj分类

初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      (4)递推.      (5)构造法.(poj3295)      (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996) 二.图算法:      (1)图的深度优先遍历和广度优先遍历.      (2)最短路径算法(dijkstra,bellman-ford

有return的情况下try catch finally的执行顺序

背景:          昨天一个朋友出去面试,遇到这么一道题:"C#  catch里有return,finally里还执行吗?" 个人实践小结:         1.不管有木有出现异常,finally块中代码都会执行.         2.当try和catch中有return时,finally仍然会执行.     具体案例如下(此处以没有返回值的函数进行验证):                 3.如果是值传递,finally中改变的值对try或catch块中return返回的值无影