对于操作1,直接将其与虚根n+1连起来

对于操作2,则正常并起来

对于操作3,先把所有x与其fa 打包,按fa排序,

判断是否连续偶数个的fa一样,如果一样则可以求值,如果不是偶数个,则看其根是否为虚根n+1,是也可以求值,

两种都满足则OK,不然就是无法求

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <vector>
#include <iostream>
using namespace std;
typedef long long  ll;
const double pi=acos(-1.0);
double eps=0.000001;
int fa[21234];
int dis[21234];
int vis[21234];
struct node
{int x,rt;};
node tt[20];
int n,q;
int find(int x)
{if (x==fa[x]) return x;else{int tmp=find(fa[x]);dis[x]=dis[x]^dis[fa[x]];return fa[x]=tmp;}
}
void get_val(int x)
{int fx=find(x);if (vis[fx]==0) return ;vis[x]=vis[fx]^dis[x];
}
bool cmp(node a,node b)
{return a.rt<b.rt;
}
bool Union (int x,int y,int z)
{int fx=find(x);int fy=find(y);if(fx==fy){if ((dis[x]^dis[y])!=z) return false;}else{if (fx==n+1)fa[fy]=fx,dis[fy]=dis[x]^dis[y]^z;else fa[fx]=fy,dis[fx]=dis[x]^dis[y]^z;}return true;
}
int main()
{int t;int cnt=1;while(scanf("%d%d",&n,&q)!=EOF){if (!n&&!q)break;int facts=0;printf("Case %d:\n",cnt++);char ss[25];int x,y,z;for (int i=1; i<=n+1; i++) fa[i]=i,dis[i]=0;int flag=0;  //emdfor (int i=1; i<=q; i++){scanf("%s",ss);if (ss[0]=='I'){facts++;gets(ss);int ret=sscanf(ss,"%d%d%d",&x,&y,&z);if (flag)continue;if (ret==2){x++;swap(y,z);y=n+1;}elsex++,y++;if (Union(x,y,z)==false){flag=facts;continue;}}else{int k,x;scanf("%d",&k);node tmp;for (int j=1; j<=k; j++){scanf("%d",&x);x++;tmp.x=x;tmp.rt=find(x);tt[j]=tmp;}if (flag)continue;sort(tt+1,tt+1+k,cmp);int uncertain=0;int cun=1;int j=2;for ( ; j<=k; j++){if (tt[j].rt==tt[j-1].rt)cun++;else{if (cun%2&&tt[j-1].rt!=n+1){uncertain=1;break;}else cun=1;}}if (cun%2&&tt[j-1].rt!=n+1) uncertain=1;if (uncertain)printf("I don't know.\n");else{int ans=0;for (int j=1; j<=k; ){if (tt[j].rt==tt[j+1].rt&&j+1<=k){ans^=dis[tt[j].x]^dis[tt[j+1].x];j+=2;}else{find(tt[j].x);ans^=dis[tt[j].x];j++;}}printf("%d\n",ans);}}}if (flag)printf("The first %d facts are conflicting.\n",flag);printf("\n");}return 0;}

UVALive 4487 - Exclusive-OR -并查集 虚根相关推荐

  1. UVALive 4035 - Undetectable Tour(并查集)

    题意:给定一个 N * N(3 <= N <= 10000)的矩形区域,左下角为(0,0),右上角为(N,N),现在要从左下角走到右上角,但是有 k(k <= 100)个监视器,每个 ...

  2. UVALive 7456 Least Crucial Node (并查集)

    Least Crucial Node 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/C Description http://7 ...

  3. 51nod1307(暴力树剖/二分dfs/并查集)

    题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1307 题意: 中文题诶~ 思路: 解法1:暴力树剖 用一个数 ...

  4. 2018/8/9 MultiU 6 并查集+dfs,反向建边提高查询效率 !!! / 最大字段和n维(降维)/ 状压+中途相遇法...

    hdu6370 Werewolf http://acm.hdu.edu.cn/showproblem.php?pid=6370 题意:村民只能说真话,狼人"可以"撒谎,每个人说一句 ...

  5. 并查集 删除节点 求集合的个数set hdu 2473

    题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=2473 Junk-Mail Filter Time Limit: 15000/8000 MS (Java ...

  6. 2017乌鲁木齐ICPC: I. A Possible Tree(带权并查集)

    I. A Possible Tree Alice knows that Bob has a secret tree (in terms of graph theory) with n nodes wi ...

  7. 并查集c++代码_[Leetcode 每日精选](本周主题-并查集) 547. 朋友圈

    题目难度: 中等 原题链接 今天继续来做并查集的问题, 这道题仍然比较基础, 而且也是个比较接近现实的问题了. 大家在我的公众号"每日精选算法题"中的聊天框中回复 并查集 就能看到 ...

  8. HDU1811 Rank of Tetris 拓扑排序+并查集 OR 差分约束最短路+并查集

    题目链接 题意:就是给你一堆关系,看能不能排出个确定的顺序 做法: 1. 拓扑排序+并查集 应该很容易想到的一种思路,大于小于建立单向边.对于相等的呢,就把他们缩成一个点.就用并查集缩成一个点就行了 ...

  9. HDU 2586 How far away ? LCA ---tanjar+并查集 离线算法

    tanjar算法离线求LCA的思想主要是利用并查集的思想. 求距离的话就是d[start[i]]+end[en[i]]-2*d[lca[i]]; 首先从根节点dfs,在深度遍历的回溯的过程中不断的更新 ...

最新文章

  1. 数据结构乐智教学百度云_网易云课堂 - 我的职业课堂
  2. cordova 发布 android release 签名打包
  3. cassandra数据备份_Cassandra中的数据建模
  4. 为什么机器学习算法要与人类水平比较
  5. 【教程分享】Jmeter入门教程
  6. 命令发送广播_那些你不知道的ping命令参数
  7. python的xml.dom学习笔记
  8. Polymer元素和AngularJS指令有什么区别?
  9. JAVA项目实训struts2_JavaWeb学习:Struts2与Spring的IOC练习
  10. 项目后台运行关闭_iOS到底有没有必要上滑强制关闭APP?
  11. 【有问不答】空间随机四面体体积计算(MATLAB)
  12. Windows XP默认用户自动登录
  13. MSCNN论文解读-A Unified Multi-scale Deep Convolutional Neural Network for Fast Object Detection
  14. FT232H USB转串口,I2C,JTAG高速芯片
  15. 为什么分布电容会缩短信号的传输距离
  16. 14考虑电动汽车可调度潜力的充电站两阶段市场投标策略
  17. Java 操作excel 插入删除列,插入删除图片
  18. “上云 用数 赋智”,这说的不是数字中台吗?
  19. ImageGear for .NET扫描打印等图形图像处理控件介绍使用手册
  20. 计算机智能科学与技术专业大学排名,全国智能科学与技术专业大学排名

热门文章

  1. Github头像上传不了、加载不出来
  2. 使图片充满整个ImageView的方法
  3. Linux基础篇之DNS服务的部署
  4. 人脸检测技术即matlab的实现,人脸检测研究及MATLAB实现.doc
  5. java 同比数据怎么算的_有当日数据和去年全年数据,如何通过公式做每日同比?...
  6. 表格的应用——日历表
  7. android 连接不上手机,安卓手机连接不上电脑怎么办
  8. 计算机cpu的功能和作用是什么意思,cpu的功能有哪些
  9. 【第三方登录】第三方登录 Part1 —— QQ登录(2016-09最新版)
  10. 公安联勤指挥调度实战应用系统软件平台解决方案