POJ1703 Find them, Catch them

【题解-POJ1703 Find them, Catch them WA,还在找BUG😩。 阅读详情

题目链接:http://poj.org/problem?id=1703

大概思路:题目要确认的有三种结果,所以不能直接找出a的不同集合再与b合并。由于事先并不知道a、b是哪一派别的,所以开一个2*maxn大的数组,分成两个派别:a属于a,a属于a+maxn。每次执行D操作其实就是合并(a,b+maxn)和(a+maxn,b),最后判断a,b是否同一派别,a,b+maxn是否同一派别即可。

TLE了一次,cin和scanf的差异啊!

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

const int MAXN=100005;
int par[MAXN*2],hei[MAXN*2];
int n,m,arr[MAXN];

bool same(int x,int y);
void unite(int x,int y);
int find_root(int x);
void init();
int main()
{
    int t,a,b;
    char ch;
    cin>>t;
    while(t--){
        cin>>n>>m;
        init();
        int i=0;
        while(m--){
            //cin>>ch>>a>>b;
            getchar();
            scanf("%c%d%d",&ch,&a,&b);
            if(ch=='D'){
                unite(a,b+MAXN);
                unite(b,a+MAXN);
            }
            if(ch=='A'){
                //cout<<find_root(a)<<","<<find_root(b)<<endl;
                if(same(a,b)) arr[i++]=0;
                else if(same(a,b+MAXN)) arr[i++]=1;
                else arr[i++]=2;
            }
        }
        //for(int j=0;j<i;j++) cout<<arr[j]<<endl;
        for(int j=0;j<i;j++){
            switch(arr[j]){
                case 0:cout<<"In the same gang.\n";break;
                case 1:cout<<"In different gangs.\n";break;
                case 2:cout<<"Not sure yet.\n";
            }
        }
    }

    return 0;
}
void init(){
    for(int i=1;i<=n+MAXN;i++){
        par[i]=i;
        hei[i]=0;
    }
}
int find_root(int x){
    while(par[x]!=x){
        x=par[x];
    }
    return x;
}
void unite(int x,int y){
    x=find_root(x);
    y=find_root(y);
    if(x==y) return;
    if(hei[x]<hei[y]) par[x]=y;
    else{
        par[y]=x;
        if(hei[x]==hei[y]) hei[x]++;
    }
}
bool same(int x,int y){
    return find_root(x)==find_root(y);
}


poj1703 Find them, Catch them 这是题目: 3:Find them, Catch them 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, G 阅读详情

相关推荐

美团mtgsig 3.1.0算法还原:从加密入口到d1生成的完整链路拆解

本文详细拆解了美团mtgsig 3.1.0签名算法的完整逆向过程。通过定位加密入口函数eH,理清a6、a5、a8到d1的生成依赖链,并运用插桩技术,在JSVMP混淆环境下逐步还原了核心参数的生成逻辑,为逆向工程实践提供了清晰的纯算还原方法论。

oo7890的博客 696

并查集POJ 1703 Find them, Catch them

POJ 1703 : Find them, Catch them 有两大帮派,现在不知道每个人的帮派情况。有两种操作,D x y 表示x和y在不同帮派,A x y查询x和y的帮派情况。 如果在同一帮派,输出In the same gang. 如果不在同一帮派,输出In different gangs. 不确定帮派情况则输出Not sure yet. 这里是POJ 11...

小酷Miki的专栏 225

(秋招复习)一些SLAM基础

秋招复习--SLAM基础知识篇

weixin_45432823的博客 736

POJ - 1703 Find them, Catch them并查集

                  POJ - 1703  Find them, Catch them并查集)          这道题与食物链那一道题是同一类型的,但稍微简单些,题意就不说了                   比普通的并查集多了一个relation [i]数组,记录与父亲节点的关系,在并查集里的元素都是已经它们之间的关系的,在路径压缩与集合合并时不断的更新relation数...

GYH0730的博客 434

poj 1703 Find them, Catch them(带权并查集)

题目链接:poj 1703 Find them, Catch them 题目大意:在城市中有两个大的帮派,Gang Dragon 和Gang Snake,先在有n个人被抓了,警察队他们进行m次操作,操作分为两种,询问A a b, 要求判断a和b是不是属于同一个团伙,D a b 表示说a b不属于同一个团伙。 解题思路:带权并查集的简单版,权值为1表示和前一个人不属于同一个团

不慌不忙、不急不躁 1108

poj1703 Find them Catch them

http://www.elijahqi.win/2018/01/12/poj1703-find-them-catch-them/ ‎ Description The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city

Elijahqi 381

POJ 1703 Find them, Catch them(种类并查集)

题目地址:POJ 1703 种类并查集水题。 代码如下: #include #include #include #include #include #include #include #include #include #include #include using namespace std; int bin[600000], rank[600000]; int f

scf0920 1273

POJ-1703-Find them, Catch them 解题报告

一道标准的关系型

JZQT_T的博客 1332

POJ 1703——Find them, Catch them并查集变形】

题目传送门 Description The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Gang Dragon and Gang Snake. However, the police first needs ...

NIRVANA REBIRTH 244

抽签问题

题目:每次从n个数里抽取一个数记录后并放回,问是否存在抽取的四个数之和为m的情况。 当n 下面的算法时间复杂度为O(n^2 log n)。 #include #include using namespace std; int n,m; int a[1005],b[1000005]; bool b_search(int x); void solve(); int main() {

sun_apple_123 938

迷宫的最短路径

题目:给定一个大小为N*M的迷宫。迷宫由通道和墙壁组成,每一步可以向邻接的上下左右四格的通道移动。请求出从起点到终点所需的最小步数。 #include #include using namespace std; const int INF=100000; int n,m,sx,sy,ex,ey; int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1}; int step

sun_apple_123 737

多项式相乘

//存储结构 struct PolyNode{ int coef;//系数 int expon;//指数 PolyNode * next;//指针域 } //多项式相乘 PolyNode* Multiply(PolyNode* L1,PolyNode* L2){ PolyNode *t1,*t2,*t,*rear,*L; int c,e; L=(...

sun_apple_123 721

Packets

Description A factory produces products packed in square packets of the same height h and of the sizes 1*1, 2*2, 3*3, 4*4, 5*5, 6*6. These products are always delivered to customers in the square

sun_apple_123 577

Factorial

Problem Description The most important part of a GSM network is so called Base Transceiver Station (BTS). These transceivers form the areas called cells (this term gave the name to the cellular phone)

sun_apple_123 524

Image Perimeters

Problem Description Technicians in a pathology lab analyze digitized images of slides. Objects on a slide are selected for analysis by a mouse click on the object. The perimeter of the boundary of

sun_apple_123 488

Tiling

Description In how many ways can you tile a 2xn rectangle by 2x1 or 2x2 tiles?  Here is a sample tiling of a 2x17 rectangle.  Input Input is a sequence of lines, each line co

sun_apple_123 453

1002 写出这个数 (20)

#include #include using namespace std; int main() { char str[105]; int num[105],len; while(cin>>str){ int i,j; len=strlen(str); for(i=0;i<len;i++){

sun_apple_123 426

POJ2139 Six Degrees of Cowvin Bacon

题目来源:http://poj.org/problem?id=2139 题意:有N只牛,在同一部电影Mi里的Ni只牛它们之间的距离为1,当两只牛不在同一部电影里,有第三只牛分别在那两只牛拍的电影里,则它们的距离为2。要求的是以其中一只牛为起点与其他牛之间的距离之和最短。 N #include #include #include using namespace std; const int

sun_apple_123 415

1007 素数对猜想

#include #include using namespace std; int main(){ int n; while(cin>>n){ int i,j,t=0,m=0,num=0; for(i=2;i<=n;i++){ bool flag=0; for(j=2;j<=sqrt(i);j++){

sun_apple_123 403

部分和问题

题目:给定整数a1,a2,..,an,判断是否可以从中选出若干数,使它们的和为k。n #include using namespace std; int n,k,a[25]; bool dfs(int i,int sum); void solve(); int main() { while(cin>>n){ for(int i=0;i<n;i++){

sun_apple_123 393

INCA_IF_ASAM-ASAP3_EN.rar_INCA ASAM-3MC插件_asam_differentaae_inca

ASAM3协议,以及如何通INCA通讯,非常全面的一个介绍资料

上一篇: POJ3253 Fence Repair
下一篇: POJ2139 Six Degrees of Cowvin Bacon
sun_apple_123
博客等级 码龄11年 4粉丝 65原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值