题目
传送门 to CF:下面是另一种等价题意。
题目背景
“哈哈,这种老式挂锁,能难倒我吗”,
D
D
G
\sf DDG
DDG 的语气中无不透露着得意,“再会!”
题目描述
O
n
e
I
n
D
a
r
k
\sf OneInDark
OneInDark 家的宠物不见了!
为了把它找回来, O n e I n D a r k \sf OneInDark OneInDark 决定修一个最好的狗链。具体而言, O n e I n D a r k \sf OneInDark OneInDark 画了一个 n n n 个点 m m m 条边的简单无向图,他希望找出所有的 1 ⩽ l ⩽ r ⩽ n 1\leqslant l\leqslant r\leqslant n 1⩽l⩽r⩽n,使得:
- 只保留编号在 [ l , r ] [l,r] [l,r] 内的点,以及它们两两之间的边,其他的点、边都删掉,剩下的是一条链。
O n e I n D a r k \sf OneInDark OneInDark 相信,这种具有特殊意义的 ⟨ l , r ⟩ \langle l,r\rangle ⟨l,r⟩ 可以为他的狗链赋能。请你告诉他,有多少个 l , r l,r l,r 满足上面的条件吗?
注:“链” 必须是一个连通图。更严谨的定义,大家都懂。
数据范围与提示
max
(
n
,
m
)
⩽
2.5
×
1
0
5
\max(n,m)\leqslant 2.5\times 10^5
max(n,m)⩽2.5×105 。图不保证连通。
思路
图是一条 “链” 等价于 图连通 且每个点度数 ⩽ 2 \leqslant 2 ⩽2 且不构成环。
枚举右端点。后两者都可以 双指针,且第三个要用 L C T \tt LCT LCT 来判断。“图连通” 则不能双指针。因为双指针的本质是 可行解构成区间,而 “图连通” 并不对应一个区间,所以它肯定需要额外维护。
判断连通性,可以想到并查集,看看成功连接两个连通块的边的数量即可。而第三个条件成立,每条边都是合法的!所以此时只要边足够多,即 ∣ E ∣ = ∣ V ∣ − 1 |E|=|V|-1 ∣E∣=∣V∣−1 即可。
查询任意值难,查询最值平凡。一般都这个套路。注意到 ∣ V ∣ ⩾ ∣ E ∣ + 1 |V|\geqslant|E|+1 ∣V∣⩾∣E∣+1,那么线段树维护一下 ∣ V ∣ − ∣ E ∣ |V|-|E| ∣V∣−∣E∣ 最小值,与其数量即可。
时间复杂度 O ( n log n ) \mathcal O(n\log n) O(nlogn),瓶颈在于 L C T \tt LCT LCT 的常数。
代码
#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long int_;
# define rep(i,a,b) for(int i=(a); i<=(b); ++i)
# define drep(i,a,b) for(int i=(a); i>=(b); --i)
inline int readint(){
int a = 0; char c = getchar(), f = 1;
for(; c<'0'||c>'9'; c=getchar())
if(c == '-') f = -f;
for(; '0'<=c&&c<='9'; c=getchar())
a = (a<<3)+(a<<1)+(c^48);
return a*f;
}
inline void writeint(int x){
if(x > 9) writeint(x/10);
putchar((x-x/10*10)^48);
}
const int MaxN = 250005;
struct Edge{
int to, nxt;
Edge(){ }
Edge(int T,int N){
to = T, nxt = N;
}
};
Edge e[MaxN<<1];
int head[MaxN], cntEdge;
void addEdge(int a,int b){
e[cntEdge] = Edge(b,head[a]);
head[a] = cntEdge ++;
}
int n; /// length of segment tree
namespace SgTree{
const int MaxM = MaxN<<2;
int val[MaxM], cnt[MaxM], tag[MaxM];
# define __ROOT int o=1,int l=1,int r=n
void pushDown(int o){
rep(d,0,1){
val[o<<1|d] += tag[o];
tag[o<<1|d] += tag[o];
}
tag[o] = 0; // released
}
void pushUp(int o){
val[o] = min(val[o<<1],val[o<<1|1]);
rep(d,cnt[o]=0,1)
if(val[o<<1|d] == val[o])
cnt[o] += cnt[o<<1|d];
}
# define LSON o<<1,l,(l+r)>>1
# define RSON o<<1|1,((l+r)>>1)+1,r
void range_add(int ql,int qr,int qv,__ROOT){
if(qr < l || r < ql) return ;
if(ql <= l && r <= qr) return
val[o] += qv, void(tag[o] += qv);
pushDown(o); range_add(ql,qr,qv,LSON);
range_add(ql,qr,qv,RSON); pushUp(o);
}
int query(int ql,int qr,__ROOT){
if(qr < l || r < ql) return 0;
if(ql <= l && r <= qr)
return (val[o] == 1) ? cnt[o] : 0;
pushDown(o); return query(ql,qr,LSON)
+ query(ql,qr,RSON); // recurse
}
void build(__ROOT){
val[o] = 0, cnt[o] = r-l+1;
if(l != r) build(LSON), build(RSON);
}
}
namespace LCT{ // declaration
bool connected(int,int);
bool link(int,int);
bool cut(int,int);
}
void erase_node(int x){
for(int i=head[x]; ~i; i=e[i].nxt)
LCT::cut(x,e[i].to);
}
int cnt_bad_node, deg[MaxN];
int main(){
n = readint();
SgTree::build();
int m = readint();
memset(head+1,-1,n<<2);
for(int a,b; m; --m){
a = readint(), b = readint();
addEdge(a,b), addEdge(b,a);
}
int deg_l = 1, circ_l = 1;
int_ ans = 0; // final answer
for(int i=1; i<=n; ++i){
for(int j=head[i]; ~j; j=e[j].nxt)
if(deg_l <= e[j].to && e[j].to < i){
++ deg[e[j].to], ++ deg[i];
if(deg[e[j].to] == 3) ++ cnt_bad_node;
if(deg[i] == 3) ++ cnt_bad_node;
}
for(; cnt_bad_node; ++deg_l){
if(deg[deg_l] > 2) -- cnt_bad_node;
for(int j=head[deg_l]; ~j; j=e[j].nxt)
if(deg_l < e[j].to && e[j].to <= i){
-- deg[e[j].to];
if(deg[e[j].to] == 2)
-- cnt_bad_node;
}
}
for(int j=head[i]; ~j; j=e[j].nxt)
if(circ_l <= e[j].to && e[j].to < i){
while(LCT::connected(i,e[j].to)){
erase_node(circ_l ++);
if(e[j].to < circ_l) break;
}
if(circ_l <= e[j].to)
LCT::link(i,e[j].to);
}
for(int j=head[i]; ~j; j=e[j].nxt)
if(e[j].to < i) // one more edge
SgTree::range_add(1,e[j].to,-1);
SgTree::range_add(1,i,1); // one more node
ans += SgTree::query(max(circ_l,deg_l),i);
}
printf("%lld\n",ans);
return 0;
}
inline void swapInt(int &a,int &b){
a ^= b ^= a ^= b;
}
namespace LCT{
int fa[MaxN], son[MaxN][2];
bool tag[MaxN];
int sonId(int o){
if(son[fa[o]][0] == o) return 0;
if(son[fa[o]][1] == o) return 1;
return -1; // virtual edge
}
# define pushUp(o) void(0) /// should be replaced
void flip(int o){
if(o == 0) return ;
swapInt(son[o][0],son[o][1]);
tag[o] = not tag[o];
}
void pushDown(int o){
if(tag[o]){ // expand loop
flip(son[o][0]), flip(son[o][1]);
tag[o] = false; // released
}
}
void addEdge(int a,int d,int b){
if(d != -1 and a != 0) son[a][d] = b;
if(b != 0) fa[b] = a; // real edge
}
void rotate(int o){
const int k = fa[o];
int d = (son[k][1] == o);
pushDown(k), pushDown(o);
addEdge(k,d,son[o][d^1]);
addEdge(fa[k],sonId(k),o);
addEdge(o,d^1,k); // 3 edges
pushUp(k), pushUp(o);
}
void push_down_all(int o){
if(sonId(o) != -1)
push_down_all(fa[o]);
pushDown(o); // pushdown the chain
}
void splay(int o){
push_down_all(o); // avoid mistake
while(sonId(o) != -1)
if(sonId(fa[o]) == -1) rotate(o);
else if(sonId(o) == sonId(fa[o]))
rotate(fa[o]), rotate(o);
else rotate(o), rotate(o);
}
void access(int x){
for(int y=0; x!=0; y=x,x=fa[x])
splay(x), son[x][1] = y, pushUp(x);
}
void makeRoot(int o){
access(o), splay(o), flip(o);
}
int findRoot(int x){
access(x), splay(x); pushDown(x);
while(son[x][0] != 0)
pushDown(x = son[x][0]);
splay(x); return x;
}
bool connected(int x,int y){
makeRoot(x);
return findRoot(y) == x;
}
bool link(int x,int y){
if(connected(x,y)) return false;
/// connected::makeRoot::splay(x)
fa[x] = y; return true;
}
bool cut(int x,int y){
if(not connected(x,y))
return false; // no edge to cut
// connected::makeRoot(x)
access(x); splay(y);
fa[y] = 0; return true;
}
}