2015多校第6场 HDU 5354 Bipartite Graph CDQ,并查集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5354
题意:求删去每个点后图是否存在奇环(n,m<=1e5)
解法:很经典的套路,和这题一样:http://www.cnblogs.com/spfa/p/7358672.html CDQ套并查集。 这题最开始是看了南神的代码才懂的,http://blog.csdn.net/hdu2014/article/details/47450709 因为要判断每一个点,而且一旦一个点之外的几个点形成了奇环的话这个点一定就是No,所以用分治来解。先判断每一段之外的点是否会成为奇环,如果是的话,这一段就全是No,反之就把这些点放到并查集里并记录,然后分治当前段,直到分治进行到单个点,分治结束后把并查集还原。真是神奇的分治。orz
//HDU 5354
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int>pi;
const int maxn = 400020;
vector<pi>E[maxn*2];
int n,m,f[maxn],sz[maxn],val[maxn],ans[maxn];
bool in(int a, int L, int R){
return a>=L&&a<=R;
}
void solve(int l, int r, int x, vector<pi>&tp);
pi find_set(int x){
int ret=x;
int w=0;
for(;f[ret]!=ret;ret=f[ret]) w^=val[ret];
w^=val[ret];
return pi(ret,w);
}
void CDQ(int l, int r, int x){
if(l == r){
ans[l]=1;
return;
}
E[x<<1].clear();
E[x<<1|1].clear();
vector<pi>tp[2];
int mid = (l+r)>>1;
for(int i=0; i<E[x].size(); i++){
int a=E[x][i].first,b=E[x][i].second;
if(in(a,l,mid)||in(b,l,mid)) E[x<<1].push_back(E[x][i]);
else tp[0].push_back(E[x][i]);
if(in(a,mid+1,r)||in(b,mid+1,r)) E[x<<1|1].push_back(E[x][i]);
else tp[1].push_back(E[x][i]);
}
solve(l,mid,x<<1,tp[0]);
solve(mid+1,r,x<<1|1,tp[1]);
}
void solve(int l, int r, int x, vector<pi>&tp)
{
vector<pi>res;
bool flag=0;
for(int i=0; i<tp.size(); i++){
int u=tp[i].first,v=tp[i].second;
pi fu = find_set(u), fv = find_set(v);
if(fu.first==fv.first){
if(!(fu.second^fv.second)){
flag = 1;
break;
}
}
else{
int t1=sz[fu.first]>sz[fv.first]?fu.first:fv.first;
int t2=fu.first+fv.first-t1;
int t3=fu.second^fv.second;
f[t2]=t1;
sz[t1]+=sz[t2];
res.push_back(pi(t2,t3));
val[t2]^=t3;
}
}
if(flag){
for(int i=l; i<=r; i++) ans[i]=0;
}
else CDQ(l,r,x);
for(int i=res.size()-1; i>=0; i--){
int u=res[i].first;
sz[f[u]]-=sz[u];
val[u]^=res[i].second;
f[u]=u;
}
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
E[1].clear();
scanf("%d%d",&n,&m);
for(int i=1; i<=n; i++) f[i]=i,sz[i]=1,val[i]=1;
for(int i=1; i<=m; i++){
int u,v;
scanf("%d%d",&u,&v);
if(u>v) swap(u,v);
E[1].push_back(pi(u,v));
}
CDQ(1,n,1);
for(int i=1; i<=n; i++) printf("%d",ans[i]);
printf("\n");
}
return 0;
}
2015多校第6场 HDU 5354 Bipartite Graph CDQ,并查集的更多相关文章
- 2015多校第6场 HDU 5361 并查集,最短路
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5361 题意:有n个点1-n, 每个点到相邻点的距离是1,然后每个点可以通过花费c[i]的钱从i点走到距 ...
- 2015多校第6场 HDU 5355 Cake 贪心,暴力DFS
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5355 题意:给你n个尺寸大小分别为1,2,3,…,n的蛋糕,要求你分成m份,要求每份中所有蛋糕的大小之 ...
- 2015多校第6场 HDU 5358 First One 枚举,双指针
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5358 题意:如题. 解法:观察式子发现,由于log函数的存在,使得这个函数的值域<=34,然后我 ...
- 2015多校第6场 HDU 5360 Hiking 贪心,优先队列
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5360 题意:给定n个人,现在要邀请这些人去远足,但每个人同意邀请的条件是当前已经同意去远足的人数c必须 ...
- 2015多校第6场 HDU 5353 Average 贪心,细节处理
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5353 题意:有n个人围城一个环,每一个人手里都有一些糖果,第i个人有ai块.现在有三种操作:第i个人给 ...
- 2015多校第9场 HDU 5405 Sometimes Naive 树链剖分
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5405 题意: 给你一棵n个节点的树,有点权. 要求支持两种操作: 操作1:更改某个节点的 ...
- 2015多校第8场 HDU 5382 GCD?LCM! 数论公式推导
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5382 题意:函数lcm(a,b):求两整数a,b的最小公倍数:函数gcd(a,b):求两整数a,b的最 ...
- 2015多校第7场 HDU 5378 Leader in Tree Land 概率DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5378 题意:一棵n个节点的树.对其节点进行标号(1~n).求恰好存在k个节点的标号是其节点所在子树的最 ...
- 2015多校第7场 HDU 5379 Mahjong tree 构造,DFS
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5379 题意:一颗n个节点n-1条边的树,现在要给每个节点标号(1~n),要求:(1)每一层的兄弟节点的 ...
随机推荐
- BZOJ4245 ONTAK2015 OR-XOR(贪心)
贪心的按位考虑.如果所有数在某一位上有奇数个为1,显然无论如何划分这一位最终都会为1:否则将每一部分都划分为偶数个1就能保证最终该位为0,可以标记上哪些位置可以作为划分点(当然也要满足之前可为0的位上 ...
- hdu 1848(Fibonacci again and again)(SG博弈)
Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...
- Android 手势识别
public class MyGesture extends SimpleOnGestureListener { private GestureDetector gd; // onGestureLis ...
- weakself的另一种写法
在不久前看AFNetworking的源码时候发现了这么一句: 1 2 3 4 5 6 7 8 9 10 // 不知道这行代码的使用场景的同学你该去自习看看ARC的注意事项和Block的使用了 // A ...
- hdu 5621
KK's Point Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- STM32串口发送第一个字符丢失解决之道
stm32用printf函数重定向到串口USART1发现第一个字符没打印出来具体如下: 刚开始修改fputc函数如下: int fputc(int ch,FILE *f){USART_SendData ...
- 优化Hadoop Balancer运行速度
(如果运行hbase的话建议为16384),指定用于在DataNode间传输block数据的最大线程数,老版本的对应参数为dfs.datanode.max.xcievers 2.修改dfs.datan ...
- HBase工具之监控Region的可用和读写延时状况
1.介绍HBase集群上region数目由于业务驱动而越来越多,由于服务器本身,网络以及hbase内部的一些不确定性bug等因素使得这些region可能面临着不可用或响应延时情况.通过对region的 ...
- rank() within group用法【转】
参考:http://www.itpub.net/thread-241824-1-1.html http://blog.itpub.net/13379967/viewspace-481811/ ) w ...
- Android 一些系统参数的获取
//获取网络类型 2G/3G/WIFI public String getNetworkType(){ String mNetWorkType = ""; Connectivity ...