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

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

#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. oracle tablespace pctfree pctused

    二.oracle pctfree和pctused详解 一.建立表时候,注意PCTFREE参数的作用 PCTFREE:为一个块保留的空间百分比,表示数据块在什么情况下可以被insert,默认是10,表示 ...

  2. postgresQL 服务器端守护进程

  3. web-linux-shell实现 阿里方案canvas+wss。

    wss://shell.aliyun.com/terminals?cols=92&rows=35 [root@webshell ~]# python Python 3.6.8 (default ...

  4. eclipse连接夜神模拟器方法

    用eclipse 进行安卓开发的时候我们会遇到安卓自带的模拟器启动时间过长,反应慢等的问题,这个时候我们就希望使用别的安卓模拟器,而我自己喜欢使用夜神模拟器.1.首先我们启动eclipse 和夜神模拟 ...

  5. 廖雪峰Git教程2

    转自:https://www.liaoxuefeng.com/wiki/896043488029600 [远程仓库] 到目前为止,我们已经掌握了如何在Git仓库里对一个文件进行时光穿梭,你再也不用担心 ...

  6. typeScript中的函数

    // 函数的定义 //es5定义函数的方法 /* //函数声明法 function run(){ return 'run'; } //匿名函数 var run2=function(){ return ...

  7. openresty开发系列28--openresty中操作mysql

    openresty开发系列28--openresty中操作mysql Mysql客户端   应用中最常使用的就是数据库了,尤其mysql数据库,那openresty lua如何操作mysql呢?   ...

  8. shell编程系列17--文本处理三剑客之awk动作中的表达式用法

    shell编程系列17--文本处理三剑客之awk动作中的表达式用法 awk动作表达式中的算数运算符 awk动作中的表达式用法总结: 运算符 含义 + 加 - 减 * 乘 / 除 % 模 ^或** 乘方 ...

  9. ros 学习 array 的添加

    array的添加,使用 arry[i]赋值时会出现段错误,需要使用array.push_back添加到数据中,在ros中array数组是以vector方式存储的. 例如: 包含数组的msg定义为: h ...

  10. 【437】Binary search algorithm,二分搜索算法

    Complexity: O(log(n)) Ref: Binary search algorithm or 二分搜索算法 Ref: C 版本 while 循环 C Language scripts b ...