https://ac.nowcoder.com/acm/contest/368/D

题意

小T有n个点,每个点可能是黑色的,可能是白色的。
小T对这张图的定义了白连通块和黑连通块:
白连通块:图中一个点集V,若满足所有点都是白点,并且V中任意两点都可以只经过V中的点互相到达,则称V中的点构成了一个白连通块。
黑连通块:类似白连通块的定义。
小T对这n个点m次操作。
1、在两个点之间连一条边。
2、询问白(黑)连通块个数。
3、给出x,y两个点,保证同色(为了方便描述,x,y都是白点,黑色同理)。询问存在多少个黑点,将它改变颜色后,x,y所在的白连通块会合并为一个。如果x,y已经在一个白连通块内了,输出-1。(注意:这里不会对点的颜色改变,只统计个数)

思路

感觉这个并查集还是很妙的,颜色相同的想要合并就按照正常的并查集来。如果颜色不同,直接合并没有意义,用bitset记录下来,两个白点如果bitset中有相同的黑点,那么这个点就是第三问中的一个合法点。

#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert> /* ⊂_ヽ
  \\ Λ_Λ 来了老弟
   \('ㅅ')
    > ⌒ヽ
   /   へ\
   /  / \\
   レ ノ   ヽ_つ
  / /
  / /|
 ( (ヽ
 | |、\
 | 丿 \ ⌒)
 | |  ) /
'ノ )  Lノ */ using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define boost ios::sync_with_stdio(false);cin.tie(0)
#define rep(a, b, c) for(int a = (b); a <= (c); ++ a)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c); const ll oo = 1ll<<;
const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = 1e9+;
const double esp = 1e-;
const double PI=acos(-1.0);
const double PHI=0.61803399; //黄金分割点
const double tPHI=0.38196601; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} inline void cmax(int &x,int y){if(x<y)x=y;}
inline void cmax(ll &x,ll y){if(x<y)x=y;}
inline void cmin(int &x,int y){if(x>y)x=y;}
inline void cmin(ll &x,ll y){if(x>y)x=y;} /*-----------------------showtime----------------------*/ const int maxn = 5e4+;
int col[maxn],fa[maxn];
bitset<> b[maxn],tmp;
int ans[];
int find(int x){
if(fa[x] == x) return x;
return fa[x] = find(fa[x]);
}
void uni(int x,int y){
int fx = find(x), fy = find(y);
if(col[x] == col[y]){
if(fx != fy){
fa[fx] = fy;
ans[col[x]]--;
b[fy] |= b[fx];
}
}
else b[fx].set(y), b[fy].set(x);
} void cal(int x,int y){
int fx = find(x), fy = find(y);
if(fx == fy) puts("-1");
else {
tmp = b[fx] & b[fy];
printf("%d\n", (int)tmp.count());
}
}
int main(){
int n,m;
scanf("%d%d", &n, &m);
rep(i, , n) scanf("%d", &col[i]), fa[i] = i, ans[col[i]] ++;
while(m--){
int op; scanf("%d", &op);
if(op == ) {
int x, y; scanf("%d%d", &x, &y);
uni(x, y);
}
else if(op == ) {
int x; scanf("%d", &x);
printf("%d\n", ans[x]);
}
else {
int x,y; scanf("%d%d", &x, &y);
cal(x, y);
}
}
return ;
}

最新文章

  1. linux下使用shell 自动执行脚本文件
  2. [杂谈]冲NOIP一等奖。。
  3. JAVA中的字符串操作
  4. Android 软件开发之如何使用Eclipse Debug调试程序详解及Eclipse常用快捷键(转)
  5. DBAccess
  6. Java基础之线程——派生自Thread类的子类(TryThread)
  7. 使用WITH AS 的ROW_NUMBER分页
  8. 写一个MyORM--利用反射的方法
  9. 引入工程报包导入异常:import javax.servlet.annotation.WebFilter;
  10. mongodb操作:利用javaScript封装db.collection.find()后可调用函数源码解读
  11. 【转】15款Java程序员必备的开发工具
  12. Lamp环境搭建Linux CentOS6.5编译安装mysql5.6
  13. MySQL集群(一)之主从复制
  14. Vue源码后记-钩子函数
  15. 教你如何实现微信小程序与.net core应用服务端的无状态身份验证
  16. 浴室沉思:聊聊DAL和Repository
  17. iOS移动端直连数据库
  18. git修改已push的commit信息
  19. Cookie-parser
  20. halcon 动态阈值分割之偏移值

热门文章

  1. 【iOS】containsString iOS7 报错
  2. GitHub 用户排行榜
  3. openGL基本概念
  4. 现代c++与模板元编程
  5. Transformations 方块转换 USACO 模拟 数组 数学 耐心
  6. 最小化docker镜像
  7. Java ActionListenner类的一些理解
  8. zookeeper中的分布式一致性协议
  9. 全球十大OTA 谁能有一席之地?
  10. 常用的python内置方法