POJ-1182 食物链 并查集

并查集 食物链  http://poj.org/problem?id=1182

 题解 请移步http://blog.csdn.net/niushuai666/article/details/6981689

创新的亮点

1 在找爸爸的时候更新关系。

2 判断两者的关系运用的向量思维

3 关系的表达 同类0 吃1 被吃2 同时%3

 为什么TE了快哭了

#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
#define MAX 50005
#define INF 999999
using namespace std;
int fx,fy;
int fa[MAX];
short re[MAX];


int getfather(int x)
{
   if(fa[x]!=x)
   {
       int temp=fa[x];
       fa[x]=getfather(temp);
       re[x]=(re[x]+re[temp])%3;
   }

   return fa[x];
}


int main()
{
   int n,k,d,x,y,ans=0;
   cin>>n>>k;
   memset(re,0,sizeof(re));
   for(int i=1;i<=n;i++) fa[i]=i;
   while(k--)
   {
       cin>>d>>x>>y;
       if(x>n||y>n)
       {
           ans++;
           continue;
       }
       if(d==2&&x==y)
       {
           ans++;
           continue;
       }
      int fx=getfather(x);
      int fy=getfather(y);
      if(fx!=fy)
      {
        fa[fy]=fx;
        re[fy]=(re[x]+d-1-re[y]+3)%3;
      }
      else
      {
          if(d==1&&re[x]!=re[y])
          {ans++;
          continue;
          }
          if(d==2&&((3 - re[x] + re[y]) % 3 != d - 1))
          {
             ans++;
             continue;
          }
      }
   }
   cout<<ans<<endl;
    return 0;
}


二叉堆

merge函数的作用是:将两个有序的序列合并为一个有序的序列。函数参数:merge(first1,last1,first2,last2,result,compare);

转载于:https://www.cnblogs.com/LandingGuy/p/9280270.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值