//思路详见课本 P 214 页

思路:直接用并查集,set [ k ]  存 k 的朋友所在集合的代表元素,set [ k + n ] 存 k  的敌人 所在集合的代表元素。

#include<iostream>
#include<cstring>
using namespace std;
const int maxn=2 *10000 +100;
int set[maxn];
int n;
int set_find(int d)
{
if(set[d]<0)
return d;
return set[d]=set_find(set[d]);
}
int arefriends(int x,int y)
{
if( ( set_find(x)!=set_find(y) ) &&( set_find(x)!=set_find(y+n) ) ) // ---------- 不确定关系----- 返回 -1
return -1;
else if(set_find(x)==set_find(y)) // ---------- 是朋友----- 返回 1
return 1;
return 0; //---------- 是敌人----- 返回 0
}
int areenemies(int x,int y)
{
if(arefriends(x,y)==-1) // ---------- 不确定关系----- 返回 -1
return -1;
else if(arefriends(x,y)==1) // ---------- 是朋友----- 返回 0
return 0;
return 1; // ---------- 是敌人----- 返回 1
}
void setfriends(int x,int y)//----------是敌人------输出 -1 {
if(arefriends(x,y)==0)
cout<<-1<<endl;
else if( arefriends(x,y)==-1 )//---当时我把括号写错了 -- -->else if( arefriends(x,y==-1) ),出现了 runtime error
{
set[set_find(x)]=set_find(y);
set[set_find(x+n)]=set_find(y+n);
}
}
void setenemies(int x,int y)
{
if(arefriends(x,y)==1) //---------是朋友----------输出 -1
cout<<-1<<endl;
else if(arefriends(x,y)==-1)
{
set[set_find(x+n)]=set_find(y);
set[set_find(x)]=set_find(y+n);
}
}
int main()
{
memset(set,-1,sizeof(set));
cin>>n; int c,x,y;
while(cin>>c>>x>>y)
{
if(c==0 && x==0 &&y==0)
break;
if(c==1)
setfriends(x,y);
else if(c==2)
setenemies(x,y);
else if(c==3)
cout<< ( arefriends(x,y)==1?1:0 ) <<endl;
else if(c==4)
cout<< ( areenemies(x,y)==1?1:0 ) <<endl;
}
return 0;
}

最新文章

  1. 亡命之徒aaaaaa.......chao
  2. α-β剪枝算法的java语言实现(非常实用)
  3. C# SaveFileDialog的用法(转载)
  4. [OpenCV] Samples 01: drawing
  5. JavaWeb之jsp编译为java源码的文件地址
  6. [CareerCup] 16.5 Semphore 信号旗
  7. xcode 出现the file couldn&#39;t be opened 怎么解决
  8. BED format
  9. POJ 2127 Greatest Common Increasing Subsequence -- 动态规划
  10. (Deep) Neural Networks (Deep Learning) , NLP and Text Mining
  11. Android 隐式意图 让用户选择一个浏览器访问网址
  12. 淺析LED、LED背光、OLED的技術原理與區別
  13. cursor的形状
  14. RQNOJ PID2 / 开心的金明
  15. &ldquo;.Net 社区大会&rdquo;(dotnetConf) 2017 Day 1 Keynote: .NET Everywhere
  16. 在Linux上的虚拟机上启动Oracle上报ORA-00845: MEMORY_TARGET not supported on this system的问题解决
  17. 14 Fragment的V4包的使用
  18. 踩坑之mongodb配置文件修改
  19. C博客作业01--分支,顺序结构
  20. 移动APP外挂攻防实战

热门文章

  1. php7.0 出现 curl_setopt(): Disabling safe uploads is no longer supported in 报错!
  2. Visual Studio的 Apache Cordova 插件CTP3.0发布!
  3. e.target与e.currentTarget对比
  4. Java 异常介绍
  5. ASP.NET动态网站制作(18)-- jq作业讲解及知识补充
  6. ios - 使用@try、catch捕获异常:
  7. PHP-Manual的学习----【入门指引】
  8. Lumen开发:结合Redis实现消息队列(1)
  9. Android源码及repo下载——亲自测试下载源码成功!
  10. Nearest Common Ancestors(LCA)