大意: 动态添边, 询问是否是二分图.

算是个线段树按时间分治入门题, 并查集维护每个点到根的奇偶性即可.

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<',';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head const int N = 1e6+50;
int n, q, fa[N], sz[N], dis[N];
map<pii,vector<int> > g; vector<pii> tr[N<<2];
void add(int o, int l, int r, int ql, int qr, pii v) {
if (ql<=l&&r<=qr) return tr[o].pb(v);
if (mid>=ql) add(ls,ql,qr,v);
if (mid<qr) add(rs,ql,qr,v);
} int Find(int x) {while (fa[x]!=x) x=fa[x];return x;}
vector<pair<int*,int> > tag[30];
void build(int o, int l, int r, int d) {
tag[d].clear();
for (auto &t:tr[o]) {
int z = 1, x = t.x, y = t.y;
while (fa[x]!=x) z^=dis[x],x=fa[x];
while (fa[y]!=y) z^=dis[y],y=fa[y];
if (x==y) {
if (z&1) {
REP(i,l,r) puts("NO");
for (auto &t:tag[d]) *t.x=t.y;
return;
}
}
if (sz[x]<sz[y]) swap(x,y);
tag[d].pb({&sz[x],sz[x]});
tag[d].pb({&fa[y],fa[y]});
tag[d].pb({&dis[y],dis[y]});
sz[x] += sz[y];
fa[y] = x, dis[y] = z;
}
if (l==r) puts("YES");
else build(ls,d+1),build(rs,d+1);
for (auto &t:tag[d]) *t.x = t.y;
} int main() {
scanf("%d%d", &n, &q);
REP(i,1,q) {
int u ,v;
scanf("%d%d", &u, &v);
g[pii(u,v)].pb(i);
}
for (auto &t:g) {
for (int i=0; i<t.y.size(); ++i) {
if (i+1==t.y.size()) add(1,1,q,t.y[i],q,t.x);
else {
add(1,1,q,t.y[i],t.y[i+1]-1,t.x);
++i;
}
}
}
REP(i,1,n) fa[i] = i, sz[i] = 1;
build(1,1,q,0);
}

Bipartite Checking CodeForces - 813F (线段树按时间分治)的更多相关文章

  1. 【CF576E】Painting Edges 线段树按时间分治+并查集

    [CF576E]Painting Edges 题意:给你一张n个点,m条边的无向图,每条边是k种颜色中的一种,满足所有颜色相同的边内部形成一个二分图.有q个询问,每次询问给出a,b代表将编号为a的边染 ...

  2. 【BZOJ-4184 】 Shallot 线段树按时间分治 + 线性基

    4184: shallot Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 356  Solved: 180[Submit][Status][Discu ...

  3. 【bzoj4311】向量 线段树对时间分治+STL-vector维护凸包

    题目描述 你要维护一个向量集合,支持以下操作: 1.插入一个向量(x,y) 2.删除插入的第i个向量 3.查询当前集合与(x,y)点积的最大值是多少.如果当前是空集输出0 输入 第一行输入一个整数n, ...

  4. BZOJ_4025_二分图_线段树按时间分治+并查集

    BZOJ_4025_二分图_线段树按时间分治+并查集 Description 神犇有一个n个节点的图.因为神犇是神犇,所以在T时间内一些边会出现后消失.神犇要求出每一时间段内这个图是否是二分图.这么简 ...

  5. BZOJ_4311_向量_线段树按时间分治

    BZOJ_4311_向量_CDQ分治+线段树按时间分治 Description 你要维护一个向量集合,支持以下操作: 1.插入一个向量(x,y) 2.删除插入的第i个向量 3.查询当前集合与(x,y) ...

  6. BZOJ_4184_shallot_线段树按时间分治维护线性基

    BZOJ_4184_shallot_线段树按时间分治维护线性基 Description 小苗去市场上买了一捆小葱苗,她突然一时兴起,于是她在每颗小葱苗上写上一个数字,然后把小葱叫过来玩游戏. 每个时刻 ...

  7. Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论

    Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变 ...

  8. Codeforces 938G 线段树分治 线性基 可撤销并查集

    Codeforces 938G Shortest Path Queries 一张连通图,三种操作 1.给x和y之间加上边权为d的边,保证不会产生重边 2.删除x和y之间的边,保证此边之前存在 3.询问 ...

  9. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem E (Codeforces 831E) - 线段树 - 树状数组

    Vasily has a deck of cards consisting of n cards. There is an integer on each of the cards, this int ...

随机推荐

  1. 008 BOM

    一:说明 1.说明 浏览器对象模型 2.顶级对象 浏览器中的顶级对象是window 页面中的顶级对象是document 因此: 变量属于window的,函数也是window的. 就可以使用window ...

  2. Jenkins自动化版本构建

    1.拉取代码 2.更新父版本 更新依赖版本 3.打包并推送到maven私库 4.版本控制后提交代码并打成docker镜像 PS:修改pom.xml项目版本,这里我没使用插件,直接使用脚本进行修改,这样 ...

  3. PDF生成类库

    from:https://blog.csdn.net/plean/article/details/8097015 最近忙了两个星期的任务了     iTextSharp.dll是个开源的用于生成pdf ...

  4. python中修改列表元素的方法

    一.在for循环中直接更改列表中元素的值不会起作用: 如: l = list(range(10)[::2]) print (l) for n in l: n = 0 print (l) 运行结果: [ ...

  5. 使用HSQLDB 客户端(jvm自带数据库使用技巧)

    数据库连接jar包 http://how2j.cn/frontdownload?bean.id=1169 hsqldb.jarservlet-2_3-fcs-classfiles.zipsqltool ...

  6. 最基础的Python的socket编程入门教程

    最基础的Python的socket编程入门教程 本文介绍使用Python进行Socket网络编程,假设读者已经具备了基本的网络编程知识和Python的基本语法知识,本文中的代码如果没有说明则都是运行在 ...

  7. Spring4.X整合redis

    包和版本的依赖关系很严重 我的配置 spring-data-redis-1.6.6.RELEASE.jar spring-tx-4.2.5.RELEASE.jar redis-2.7.2.jar co ...

  8. Python第一阶段06

    1.面向对象编程: class Dog: def __init__(self, name): self.name = name def bulk(self): print("%s 汪汪汪.. ...

  9. MySQL修改表名示例

    首先,我们新建一个名为test_table的表: drop table if exists test_table; create table test_table select TABLE_SCHEM ...

  10. 原生Js页面滚动延迟加载图片

    原理和过程1.页面滚动加载事件2.获取元素在页面里的top值 根据滚动条的位置 判断何时显示图片3.获取元素集合 加载过的图片从集合里删除 效果预览:http://jsfiddle.net/dtdxr ...