[CSAcademy]Cycle Tree

题目大意:

定义环树是一张无向连通的简单图,它的生成方式如下:

  1. \(2\)个点\(1\)条边的图是环树;
  2. 对任意一个环树,加入\(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的更多相关文章

  1. [CSAcademy]Connected Tree Subgraphs

    题目大意: 给你一棵n个结点的树,求有多少种染色方案,使得染色过程中染过色的结点始终连成一块. 思路: 树形DP. 设f[x]表示先放x时,x的子树中的染色方案数,y为x的子结点. 则f[x]=pro ...

  2. 4105: [Thu Summer Camp 2015]平方运算

    首先嘛这道题目只要知道一个东西就很容易了:所有循环的最小公约数<=60,成一条链的长度最大为11,那么我们就可以用一个很裸的方法.对于在链上的数,我们修改直接暴力找出并修改.对于在环上的数,我们 ...

  3. 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), ...

  4. [CSAcademy]Find the Tree

    [CSAcademy]Find the Tree 题目大意: 交互题. 有一棵\(n(n\le2000)\)个结点的树,但是你并不知道树的形态.你可以调用\({\rm query}(x,y,z)\)( ...

  5. [CSAcademy]Virus on a Tree

    [CSAcademy]Virus on a Tree 题目大意: 给你一棵\(n(n\le10^5)\)个点的树,一开始点\(1\)有病毒,可以沿着边扩散.你可以事先切掉若干条边,使得病毒扩散不超过\ ...

  6. 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( ...

  7. [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 ...

  8. 学习RxJS:Cycle.js

    原文地址:http://www.moye.me/2016/06/16/learning_rxjs_part_two_cycle-js/ 是什么 Cycle.js 是一个极简的JavaScript框架( ...

  9. 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), ...

随机推荐

  1. C++的各种初始化方式

    C++小实验测试:下面程序中main函数里a.a和b.b的输出值是多少? #include <iostream> struct foo { foo() = default; int a; ...

  2. boost::bind的使用

    最近在几经波折之后,终于对于boost::bind有点理解了.对于习惯了其他语言的人来说,boost::bind是个挺神奇的东西,它可以将你的方法适配成任何其他的方法.其实这得益于c++的模板以及操作 ...

  3. Python raw_input和input总结 在版本2和版本3中的区别

    Python 2.3.4 (#1, Feb 2 2005, 11:44:13) [GCC 3.4.3 20041212 (Red Hat 3.4.3-9.EL4)] on linux2 Type &q ...

  4. vue中使用localStorage存储信息

    一 什么是localStorage 对浏览器来说,使用 Web Storage 存储键值对比存储 Cookie 方式更直观,而且容量更大,它包含两种:localStorage 和 sessionSto ...

  5. kernel 3.10内核源码分析--TLB相关--TLB概念、flush、TLB lazy模式 【转】

    转自:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&id=4808877&uid=14528823 一.概念及基本原理 TLB ...

  6. maven2 up to maven3的'version' contains an expression but should be a constant

    在Maven2时,为了保障版本一致,一般之前我们的做法时: Parent Pom中 <project xmlns="http://maven.apache.org/POM/4.0.0& ...

  7. 试用Redis

    Windows 10家庭中文版,运行于VirtualBox上的Ubuntu 18.04,Redis 4.0.10, Redis,久仰大名!因为没有从事互联网行业,所以一直没有使用过.近期找工作,也隐约 ...

  8. thinkphp5 url传参

    url('index/blog/read',['id'=>5,'name'=>'thinkphp']); 手册https://www.kancloud.cn/manual/thinkphp ...

  9. 【转】Python验证码识别处理实例

    原文出处: 林炳文(@林炳文Evankaka) 一.准备工作与代码实例 1.PIL.pytesser.tesseract (1)安装PIL:下载地址:http://www.pythonware.com ...

  10. 微信小程序地图模块

    微信小程序的地图模块官方提供的API比较少,详情请见   官方文档 以下为一个示例                               <!--pages/location/locati ...