[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. VM虚拟机上连接usb无反映

    主机的usb连接又是正常的,排除了usb3.0的接口原因后,突然想到了是不是虚拟机的什么服务没有开?进入到控制面板->管理工具->服务,找到 V开头的,发现原来确实是虚拟机有关usb的服务 ...

  2. vue实现结算淘宝购物车效果

    实现单选时的价格,全选时价格 单选效果图 全选效果图 html <template> <!-- 淘宝结算购物车 --> <div class="settleme ...

  3. scala中“_”的用法

    参见链接 http://blog.csdn.net/i6448038/article/details/50017427

  4. 利用vw+rem实现移动web适配布局

    基本概念 1.单位 Px(CSS pixels) 像素 (px) 是一种绝对单位(absolute units), 因为无论其他相关的设置怎么变化,像素指定的值是不会变化的 其实是相对于某个设备而言的 ...

  5. 使用SQL语句查询某表中所有的主键、唯一索引以及这些主键、索引所包含的字段(转)

    SELECT 索引名称 = a.name , 表名 = c.name , 索引字段名 = d.name , 索引字段位置 = d.colid FROM sysindexes a JOIN sysind ...

  6. linux nc命令使用详解(转)

    linux nc命令使用详解 功能说明:功能强大的网络工具 语 法:nc [-hlnruz][-g<网关...>][-G<指向器数目>][-i<延迟秒数>][-o& ...

  7. BFC 从了解到放弃

    最近工作中我突然产生了一个想法,就如我们人类面临的终极问题一般,我从哪里来?我到哪里去?在撸代码进行CSS布局的时候,我会去想,我为什么这么做?,为什么浮动的元素要用overflow?,为什么要用cl ...

  8. 出现丢包解决方法(ping: sendmsg: Operation not permitted)

    故障排查: 早上突然收到nagios服务器check_icmp的报警,报警显示一台网站服务器的内网网络有问题.因为那台服务器挂载了内网的NFS,因此内网的网络就采用nagios的check_icmp来 ...

  9. android发布版本的几个命令

    ./build_native.sh /opt/software/apache-ant-1.8.2/bin/ant clean #/opt/software/apache-ant-1.8.2/bin/a ...

  10. day5模块学习--random模块

    Python中的random模块用于生成随机数 下面具体介绍random模块的功能:   1.random.random() #用于生成一个0到1的浮点数   随机浮点数:0<= n < ...