[2017SEERC]Divide and Conquer
https://www.zybuluo.com/ysner/note/1308834
题面
一个有\(n\)个点的图,上面有有两棵不同的生成树。问至少切断几条边,可以使原图不联通。并输出方案数。
- \(n\leq10^6\)
解析
或许是道树上差分模板题?
首先,由于只有\(2n-2\)条边,故所有点的最小度数只能为\(2\)或\(3\)(若度数为\(4\),需要\(2n\)条边)。
所以答案也只能是\(2\)或\(3\)。
那么,肯定在某一棵生成树上只割了一条边。
一开始想法是枚举这条边是哪个,再查一查切断这条边后,该边两个端点被几条线段连接。
然而复杂度不对,差分\(O(n^2)\),树链剖分+线段树\(O(nlog^2n)\)。
实际上把一棵树上的所有边都用来覆盖另一棵树(即覆盖两端点的树上路径),再统计一下哪条边被覆盖次数最少,\(+1\)(这棵树上只切一条边)就是答案。(代码中\(/2\)是因为边是双向的,都加了两次)
这个可以用差分来完成。
如果答案是\(3\),则可能在另一棵树上只割一条边。反向覆盖+差分来统计方案数即可。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#define ll long long
#define re register
#define il inline
#define fp(i,a,b) for(re int i=a;i<=b;i++)
#define fq(i,a,b) for(re int i=a;i>=b;i--)
using namespace std;
const int N=2e6+100;
int n,m,h[2][N],cnt[2],f[N],s[N],lca[N],vis[N],mn=1e9,ans;
struct Edge{int to,nxt;}e[2][N<<1];
il void add(re int u,re int v){e[m][++cnt[m]]=(Edge){v,h[m][u]};h[m][u]=cnt[m];}
il int find(re int x){return x==f[x]?x:f[x]=find(f[x]);}
il ll gi()
{
re ll x=0,t=1;
re char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') t=-1,ch=getchar();
while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
return x*t;
}
il void dfs(re int u)
{
vis[u]=1;
for(re int i=h[m][u];i+1;i=e[m][i].nxt)
{
re int v=e[m][i].to;
if(vis[v]) continue;
dfs(v);
f[v]=u;
}
for(re int i=h[m^1][u];i+1;i=e[m^1][i].nxt)
{
re int v=e[m^1][i].to;
if(vis[v]==2) lca[i>>1]=find(v);
}
vis[u]=2;
}
il void calc(re int u,re int fa)
{
for(re int i=h[m][u];i+1;i=e[m][i].nxt)
{
re int v=e[m][i].to;
if(v==fa) continue;
calc(v,u);
s[u]+=s[v];
}
if(!fa) return;
if((s[u]>>1)+1<mn) mn=(s[u]>>1)+1,ans=1;
else if((s[u]>>1)+1==mn) ++ans;
}
il void solve()
{
memset(s,0,sizeof(s));memset(vis,0,sizeof(vis));
fp(i,1,n) f[i]=i;
dfs(1);
fp(u,1,n)
for(re int i=h[m^1][u];i+1;i=e[m^1][i].nxt)
{
re int v=e[m^1][i].to;
++s[u];++s[v];s[lca[i>>1]]-=2;
}
calc(1,0);
}
int main()
{
freopen("xianjian.in","r",stdin);
freopen("xianjian.out","w",stdout);
memset(h,-1,sizeof(h));cnt[0]=cnt[1]=1;
n=gi();
fp(i,1,2*n-2)
{
if(i==n) m=1;
re int u=gi(),v=gi();
add(u,v);add(v,u);
}
m=0;solve();
if(mn==3) m=1,solve();
printf("%d %d\n",mn,ans);
fclose(stdin);
fclose(stdout);
return 0;
}
[2017SEERC]Divide and Conquer的更多相关文章
- [LeetCode] 236. Lowest Common Ancestor of a Binary Tree_ Medium tag: DFS, Divide and conquer
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- [LeetCode] 系统刷题4_Binary Tree & Divide and Conquer
参考[LeetCode] questions conlusion_InOrder, PreOrder, PostOrder traversal 可以对binary tree进行遍历. 此处说明Divi ...
- [LeetCode] 124. Binary Tree Maximum Path Sum_ Hard tag: DFS recursive, Divide and conquer
Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...
- 算法与数据结构基础 - 分治法(Divide and Conquer)
分治法基础 分治法(Divide and Conquer)顾名思义,思想核心是将问题拆分为子问题,对子问题求解.最终合并结果,分治法用伪代码表示如下: function f(input x size ...
- 算法上机题目mergesort,priority queue,Quicksort,divide and conquer
1.Implement exercise 2.3-7. 2. Implement priority queue. 3. Implement Quicksort and answer the follo ...
- 【LeetCode】分治法 divide and conquer (共17题)
链接:https://leetcode.com/tag/divide-and-conquer/ [4]Median of Two Sorted Arrays [23]Merge k Sorted Li ...
- The Divide and Conquer Approach - 归并排序
The divide and conquer approach - 归并排序 归并排序所应用的理论思想叫做分治法. 分治法的思想是: 将问题分解为若干个规模较小,并且类似于原问题的子问题, 然后递归( ...
- [算法]分治算法(Divide and Conquer)
转载请注明:http://www.cnblogs.com/StartoverX/p/4575744.html 分治算法 在计算机科学中,分治法是建基于多项分支递归的一种很重要的算法范式.字面上的解释是 ...
- Divide and Conquer.(Merge Sort) by sixleaves
algo-C1-Introductionhtml, body {overflow-x: initial !important;}html { font-size: 14px; }body { marg ...
随机推荐
- 4.model 字段
一.字段名 字段名 类型 参数 AutoField(Field) - int自增列, 必须填入参数 primary_key=True BigAutoField(AutoField) - bigint自 ...
- 基于flask的网页聊天室(一)
基于flask的网页聊天室(一) 基本目标 基于flask实现的web聊天室,具有基本的登录注册,多人发送消息,接受消息 扩展目标 除基本目标外添加当前在线人数,消息回复,markdown支持,历史消 ...
- Python之数字
Python之数字 int(数字)===>在Python3中,int没有范围,在Python2中,int超出范围就叫长整型(Long). 浮点运算:单精度 float 双精度 double a: ...
- 在项目中全局添加FastClick导致图片上传插件在ios端失效的解决方案
---恢复内容开始--- 项目是移动端的项目,为了解决300ms的click延迟,所以在全局中加入了FastClick,引入的方式很简单,网上一大堆教程,这里不做赘述 我们就谈,我遇到的问题: 某天产 ...
- express中间件的意思
中间件就是请求req和响应res之间的一个应用,请求浏览器向服务器发送一个请求后,服务器直接通过request定位属性的方式得到通过request携带过去的数据,就是用户输入的数据和浏览器本身的数据信 ...
- 笔记——python语言规范
Lint 对你的代码运行pylint 定义: pylint是一个在Python源代码中查找bug的工具. 对于C和C++这样的不那么动态的(译者注: 原文是less dynamic)语言, 这些bug ...
- 一个ajax实例
一个ajax实例 html <!DOCTYPE html> <html lang="zh-cn"> <head> <meta ch ...
- 6.0以上,SYSTEM_ALERT_WINDOW 权限的问题
6.0以上会因为SYSTEM_ALERT_WINDOW权限的问题,无法在最上层显示. 用户打开软件设置页手动打开,才能授权.路径是:Settings->Apps->App Setting- ...
- 将cocos2dx 2.x.x从eclipse转移到Android Studio遇到的问题
cocos2dx 2.x.x从eclipse转移到Android Studio遇到的问题 可能我用不太习惯Android Studio才会遇到这么多问题,让老手们见笑了. cocos2dx的最新版本, ...
- Linux学习笔记01
1.Linux不靠扩展名区分文件类型2.存储设备必须先挂载才能使用3.Windows下的程序不能直接在Linux中安装和运行 一.服务器的管理预配置Linux的目录的作用:/bin/存放系统命令的目录 ...