[CSAcademy]Cycle Tree
[CSAcademy]Cycle Tree
题目大意:
定义环树是一张无向连通的简单图,它的生成方式如下:
- \(2\)个点\(1\)条边的图是环树;
- 对任意一个环树,加入\(k\)个点\(a_{1\sim k}\),加入方式为从原图中选择一条边\((u,v)\),连接\((u,a_1),(a_1,a_2)\ldots(a_{k-1},a_k),(a_k,v)\),得到的图也是环树。
给定一个\(n(n\le5\times10^4)\)个点,\(m(m\le10^5)\)条边的环树,求最大独立集大小。
思路:
每次选取一个度数为\(2\)的点,将这条边删掉,将相邻的两点间连上虚点。不断进行这样的操作,最后只会剩下\(2\)个点。
\(f[0/1][0/1][i][j]\)表示对于边\((i,j)\),是否选取\(i/j\)的最大独立集,按照删点的顺序进行DP即可。
每个点最多被删一次,因此时间复杂度\(\mathcal O(n+m)\)。
源代码:
#include<map>
#include<set>
#include<queue>
#include<cstdio>
#include<cctype>
#include<climits>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
const int N=5e4+1;
bool inq[N];
std::queue<int> q;
std::set<int> e[N],set[N];
std::map<int,int> f[2][2][N];
inline void add_edge(const int &u,const int &v) {
e[u].emplace(v);
e[v].emplace(u);
}
inline void add(int &x,const int &y) {
x+=y;
}
inline void up(int &x,const int &y) {
x=std::max(x,y);
}
int main() {
const int n=getint(),m=getint();
for(register int i=0;i<m;i++) {
const int u=getint(),v=getint();
add_edge(u,v);
f[0][0][u][v]=f[0][0][v][u]=0;
f[0][1][u][v]=f[1][0][v][u]=1;
f[1][0][u][v]=f[0][1][v][u]=1;
f[1][1][u][v]=f[1][1][v][u]=INT_MIN;
set[u].insert(v);
set[v].insert(u);
}
int ans=1;
for(register int i=1;i<=n;i++) {
if(e[i].size()==2) {
q.push(i);
inq[i]=true;
}
}
while(!q.empty()) {
const int x=q.front();
q.pop();
if(e[x].size()!=2) continue;
const int u=*e[x].begin(),v=*e[x].rbegin();
add(f[0][0][u][v],std::max(f[0][0][x][u]+f[0][0][x][v],f[1][0][x][u]+f[1][0][x][v]-1));
add(f[0][1][u][v],std::max(f[0][0][x][u]+f[0][1][x][v],f[1][0][x][u]+f[1][1][x][v]-1)-!!f[0][1][u][v]);
add(f[1][0][u][v],std::max(f[0][1][x][u]+f[0][0][x][v],f[1][1][x][u]+f[1][0][x][v]-1)-!!f[1][0][u][v]);
if(!set[u].count(v)) add(f[1][1][u][v],std::max(f[0][1][x][u]+f[0][1][x][v],f[1][1][x][u]+f[1][1][x][v]-1)-2*!!f[1][1][u][v]);
up(ans,f[0][0][v][u]=f[0][0][u][v]);
up(ans,f[1][0][v][u]=f[0][1][u][v]);
up(ans,f[0][1][v][u]=f[1][0][u][v]);
up(ans,f[1][1][v][u]=f[1][1][u][v]);
e[x].clear();
e[u].erase(x);
e[v].erase(x);
e[u].insert(v);
e[v].insert(u);
if(e[u].size()==2&&!inq[u]) {
q.push(u);
inq[u]=true;
}
if(e[v].size()==2&&!inq[v]) {
q.push(v);
inq[v]=true;
}
}
printf("%d\n",ans);
return 0;
}
[CSAcademy]Cycle Tree的更多相关文章
- [CSAcademy]Connected Tree Subgraphs
题目大意: 给你一棵n个结点的树,求有多少种染色方案,使得染色过程中染过色的结点始终连成一块. 思路: 树形DP. 设f[x]表示先放x时,x的子树中的染色方案数,y为x的子结点. 则f[x]=pro ...
- 4105: [Thu Summer Camp 2015]平方运算
首先嘛这道题目只要知道一个东西就很容易了:所有循环的最小公约数<=60,成一条链的长度最大为11,那么我们就可以用一个很裸的方法.对于在链上的数,我们修改直接暴力找出并修改.对于在环上的数,我们 ...
- Leetcode: Graph Valid Tree && Summary: Detect cycle in undirected graph
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- [CSAcademy]Find the Tree
[CSAcademy]Find the Tree 题目大意: 交互题. 有一棵\(n(n\le2000)\)个结点的树,但是你并不知道树的形态.你可以调用\({\rm query}(x,y,z)\)( ...
- [CSAcademy]Virus on a Tree
[CSAcademy]Virus on a Tree 题目大意: 给你一棵\(n(n\le10^5)\)个点的树,一开始点\(1\)有病毒,可以沿着边扩散.你可以事先切掉若干条边,使得病毒扩散不超过\ ...
- Terminating app due to uncaught exception 'CALayerInvalid', reason: 'layer <CALayer: 0x7fda42c66e30> is a part of cycle in its layer tree'
iOS App里面所有的View构成一个组件树,这个树里面如果有了闭环就会出现这个报错,最常见的你不小在某UIViewController里面写了这样的代码: someView.addSubView( ...
- [LeetCode] Convert Sorted List to Binary Search Tree 将有序链表转为二叉搜索树
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- 学习RxJS:Cycle.js
原文地址:http://www.moye.me/2016/06/16/learning_rxjs_part_two_cycle-js/ 是什么 Cycle.js 是一个极简的JavaScript框架( ...
- Graph Valid Tree
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
随机推荐
- Anaconda 安装tensorflow(GPU)
1.安装 如果是安装CPU模式的tensorflow,只要输入一下代码就可以了 pip3 install tensorflow #python3pip install tensorflow #pyth ...
- .net 下的集合
集合的操作在编码的时候很常见.但是由于经常使用几种集合.而忽略了一些不常用的集合.在这里我整理下. 首先先了解下接口: 1.IEnumerable,返回一个循环访问集合的枚举器. 2.IEnumera ...
- 使用Scrapy命令行工具【导出JSON文件】时编码设置
Windows 10家庭中文版,Python 3.6.4,virtualenv 16.0.0,Scrapy 1.5.0, 使用scrapy命令行工具建立了爬虫项目(startproject),并使用s ...
- Python开发环境(3):使用Eclipse+PyDev插件创建Django项目
OS:Windows 10家庭中文版,Python:3.6,Eclipse:Oxygen.1a Release (4.7.1a), PyDev:6.3.2,Django:2.0.3 本文展示了怎么使用 ...
- 牛客红包OI赛 B 小可爱序列
Description 链接:https://ac.nowcoder.com/acm/contest/224/B 来源:牛客网 "我愿意舍弃一切,以想念你,终此一生." " ...
- 使用html+css+js实现3D相册
使用html+css+js实现3D相册,快来上传的照片吧 效果图: 代码如下,复制即可用: <!DOCTYPE html> <html lang="en"> ...
- AdvStringGrid 删除数据
unit Unit6; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...
- TObjectList
AOwnsObjects = true 就是 objectlist释放的时候,里面的对象一并释放. TObjectList对象的创建方法有一个参数:constructor TObjectList.C ...
- 关于SizeOf、Length
结论: 到底什么时候用Length,SizeOf呢,我总结下使用Length,Sizeof的场景 1.Length(静态数组或动态数组)----没有问题 2.Length(string/shortst ...
- ROS新动态获取网址汇总
ROS新动态获取网址汇总 1 planet ROS http://planet.ros.org/ 2 ROS news http://www.ros.org/news/ 3 ROS-Industria ...