Bipartite Checking CodeForces - 813F (线段树按时间分治)
大意: 动态添边, 询问是否是二分图.
算是个线段树按时间分治入门题, 并查集维护每个点到根的奇偶性即可.
#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 (线段树按时间分治)的更多相关文章
- 【CF576E】Painting Edges 线段树按时间分治+并查集
[CF576E]Painting Edges 题意:给你一张n个点,m条边的无向图,每条边是k种颜色中的一种,满足所有颜色相同的边内部形成一个二分图.有q个询问,每次询问给出a,b代表将编号为a的边染 ...
- 【BZOJ-4184 】 Shallot 线段树按时间分治 + 线性基
4184: shallot Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 356 Solved: 180[Submit][Status][Discu ...
- 【bzoj4311】向量 线段树对时间分治+STL-vector维护凸包
题目描述 你要维护一个向量集合,支持以下操作: 1.插入一个向量(x,y) 2.删除插入的第i个向量 3.查询当前集合与(x,y)点积的最大值是多少.如果当前是空集输出0 输入 第一行输入一个整数n, ...
- BZOJ_4025_二分图_线段树按时间分治+并查集
BZOJ_4025_二分图_线段树按时间分治+并查集 Description 神犇有一个n个节点的图.因为神犇是神犇,所以在T时间内一些边会出现后消失.神犇要求出每一时间段内这个图是否是二分图.这么简 ...
- BZOJ_4311_向量_线段树按时间分治
BZOJ_4311_向量_CDQ分治+线段树按时间分治 Description 你要维护一个向量集合,支持以下操作: 1.插入一个向量(x,y) 2.删除插入的第i个向量 3.查询当前集合与(x,y) ...
- BZOJ_4184_shallot_线段树按时间分治维护线性基
BZOJ_4184_shallot_线段树按时间分治维护线性基 Description 小苗去市场上买了一捆小葱苗,她突然一时兴起,于是她在每颗小葱苗上写上一个数字,然后把小葱叫过来玩游戏. 每个时刻 ...
- Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论
Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变 ...
- Codeforces 938G 线段树分治 线性基 可撤销并查集
Codeforces 938G Shortest Path Queries 一张连通图,三种操作 1.给x和y之间加上边权为d的边,保证不会产生重边 2.删除x和y之间的边,保证此边之前存在 3.询问 ...
- 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 ...
随机推荐
- CEF3设置cookie
#include "CEF3Helper.h" #include "../include/cef_app.h" #include "../includ ...
- GCN: Graph Convolutional Network
从CNN到GCN的联系与区别: https://www.zhihu.com/question/54504471/answer/332657604 更加详解Laplacian矩阵: https://ww ...
- CMU Database Systems - Query Optimization
查询优化应该是数据库领域最难的topic 当前查询优化,主要有两种思路, Rules-based,基于先验知识,用if-else把优化逻辑写死 Cost-based,试图去评估各个查询计划的cost, ...
- 关于Kubernetes Master高可用的一些策略
关于Kubernetes Master高可用的一些策略 Kubernetes高可用也许是完成了初步的技术评估,打算将生产环境迁移进Kubernetes集群之前普遍面临的问题. 为了减少因为服务器当机引 ...
- 网络通信技术中的中继器repeater
1. repeater的作用 对信号进行再生和还原 2. repeater的优点 延长通讯距离 提高可靠性 增加节点的最大数目 各个网段可以使用不同的通讯速率 3. repeater的缺点 增加了延时 ...
- Python3基础 global 在函数内部对全局变量进行修改
Python : 3.7.3 OS : Ubuntu 18.04.2 LTS IDE : pycharm-community-2019.1.3 ...
- Python3实战——爬虫入门
一.安装库 使用conda安装: conda install requests 如果出现解析环境问题,需要激活conda环境: https://www.cnblogs.com/jdemarryme/p ...
- Qt widget使用QML自定义导航栏
具体方法: https://www.cnblogs.com/judes/p/11359243.html qml: import QtQuick 2.0 import QtQuick 2.9 impor ...
- 利用postman生成各种编程语言的代码
原文来源:https://learning.getpostman.com/docs/postman/sending_api_requests/generate_code_snippets/ 在Post ...
- php调用webservice报错Class 'SoapClient' not found(转)
php在调用webservice时,报告如下类似错误: ( ! ) Fatal error: Class 'SoapClient' not found in E:/WebSrv/CI/system/l ...