echo_mind 2016-08-06 14:18 采纳率: 0%
浏览 1227

poj的一道题MLE了,求教。。

poj的1703,一道并查集的题。
http://poj.org/problem?id=1703

实在想不出哪里会造成超内存。。求教。。

以下是代码

 #include <iostream>
#include <cstring>
#include <stdio.h>
using namespace std;
const int N=100005;
char ch[2];
int fa[N];
int r[N];//0表示和父亲是同伙,1表示不是

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

void Combine(int a, int b)
{
    int rootb = find(b);
    fa[rootb] = b;
    r[rootb] = r[b];//rootb和b的关系可逆
    fa[b] = a;
    r[b] = 1;//a和b是敌人
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=1; i<=n; i++)
        {
            fa[i] = i;
            r[i] = 0;
        }
        while(m--)
        {
            int a,b;
            scanf("%s%d%d",ch,&a,&b);
            //printf("%c %d %d\n",ch[0],a,b);
            if(ch[0]=='D')
                Combine(a,b);
            else
            {
                if(find(a)!=find(b))
                    printf("Not sure yet.\n");
                else if(r[a]==r[b])
                    printf("In the same gang.\n");
                else
                    printf("In different gangs.\n");

            }
        }
    }
    return 0;
}

  • 写回答

1条回答 默认 最新

  • devmiao 2016-08-06 15:52
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler