[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), ...
随机推荐
- ORB_SLAM2 源码阅读 ORB_SLAM2::Initializer::ComputeF21 (OpenCV 细节)
ORB_SLAM2 计算 F21 的代码是这样的. cv::Mat Initializer::ComputeF21(const vector<cv::Point2f> &vP1,c ...
- 《廖雪峰Git教程》学习笔记
原文链接 一.创建版本库 ①初始化一个Git仓库:git init ②添加文件到Git仓库:1.git add<file> ; 2.git commit 二.时光机穿梭 ①查看工作区状态 ...
- Oracle 11.2.0.4在线(Online mode)打补丁14084247解决WRH$_ACTIVE_SESSION_HISTORY不会自动切割的问题
安装了Oracle Database Release 11.2.0.4之后,发现WRH$_ACTIVE_SESSION_HISTORY始终不会自动分割删除, 后来才发现需要应用补丁:14084 ...
- springquartz的LocalDataSourceJobStore
spring 为quartz 提供了一个 继承 JobStoreCMT的 LocalDataSourceJobStore,主要是为了和spring更好的集成. public class LocalDa ...
- mybatis比hibernate处理速度快的原因
mybatis:是面向结果集的.当要展示的页面需要几个字段时,springmvc会提供这几个字段并将其拼接成结果集,在转化为相应的对象. hibernate:是面向对象的.要展示的页面需要某些字段时, ...
- 33 Introducing the Go Race Detector
Introducing the Go Race Detector 26 June 2013 Introduction Race conditions are among the most insidi ...
- java基础66 JavaScript中的事件、localtion对象、screen对象(网页知识)
1.JavaScript中的事件注册 1.1.事件的注册方式 方式1:直接在html元素上注册 <body onLoad="ready()"></body > ...
- SQL Case when 的使用方法 (转)
Case具有两种格式.简单Case函数和Case搜索函数. --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' END ...
- Python 的內建模块
>>> import __builtin__>>> dir(__builtin__)['ArithmeticError', 'AssertionError', 'A ...
- Python之Selenium的爬虫用法
Selenium 2,又名 WebDriver,它的主要新功能是集成了 Selenium 1.0 以及 WebDriver(WebDriver 曾经是 Selenium 的竞争对手).也就是说 Sel ...