【HDU - 5452】Minimum Cut(树形dp 或 最近公共祖先lca+树上差分,转化tricks,思维)

题干:

Given a simple unweighted graph GG (an undirected graph containing no loops nor multiple edges) with nn nodes and mm edges. Let TT be a spanning tree of GG. 
We say that a cut in GG respects TT if it cuts just one edges of TT. 

Since love needs good faith and hypocrisy return for only grief, you should find the minimum cut of graph GG respecting the given spanning tree TT.

Input

The input contains several test cases. 
The first line of the input is a single integer t (1≤t≤5)t (1≤t≤5) which is the number of test cases. 
Then tt test cases follow. 

Each test case contains several lines. 
The first line contains two integers n (2≤n≤20000)n (2≤n≤20000) and m (n−1≤m≤200000)m (n−1≤m≤200000). 
The following n−1n−1 lines describe the spanning tree TT and each of them contains two integers uu and vv corresponding to an edge. 
Next m−n+1m−n+1 lines describe the undirected graph GG and each of them contains two integers uu and vv corresponding to an edge which is not in the spanning tree TT.

Output

For each test case, you should output the minimum cut of graph GG respecting the given spanning tree TT.

Sample Input

1
4 5
1 2
2 3
3 4
1 3
1 4

Sample Output

Case #1: 2

题目大意:

给你一个图G,n个点m条边,图中给定一颗生成树(前n-1条输入的边)。问一个最小割的边数量(将图变成一个不连通图),要求需要包含给定生成树内的一条边。

解题报告:

因为有一条生成树内的边必选,所以这是个切入点,我们枚举生成树上的每一条边<U,V>,假设我们要删的边是<U,V>, 那么自然图就分成了两块,这也正是我们要的结果。我们所要做的就是让V的子树上的任何节点,不再和其V子树外的其他节点相连。使得他们完全分离开。

有了这个思路之后,就需要换一个角色重新考虑问题了,站在新加入的边的角度上看:对于每一条新加入的一条树T外的边<a,b>,  那么我们需要判断枚举到哪一条T内的边的时候,需要将这条边删除掉,不难观察出来,是ab两点相连的这条链上的边,那么树上维护这条链上的边计数+1,就转化成有根树然后树上差分即可。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e4 + 5;
int n,m,val[MAX];
struct Edge {int u,v;int ne;
} e[MAX*20];
int tot;
int head[MAX],f[MAX];
int fa[MAX][33],dep[MAX];
void add(int u,int v) {e[++tot].u = u;e[tot].v = v;e[tot].ne = head[u];head[u] = tot;
}
void dfs(int cur,int rt) {fa[cur][0] = rt;dep[cur] = dep[rt] + 1;for(int i = head[cur]; ~i; i = e[i].ne) {int v = e[i].v;if(v == rt) continue;dfs(v,cur);}for(int i = 1; i<=31; i++) {fa[cur][i] = fa[fa[cur][i-1]][i-1];}	
}
int lca(int u,int v) {if(dep[u] < dep[v]) swap(u,v);int dc = dep[u] - dep[v];for(int i = 0; i<=31; i++) {if((1<<i) & dc) u = fa[u][i];}if(u == v) return u;for(int i = 31; i>=0; i--) {if(fa[u][i] != fa[v][i]) u = fa[u][i],v = fa[v][i];}return fa[u][0];
}
int ans = 0x3f3f3f3f;
void DFS(int cur,int rt) {for(int i = head[cur]; ~i; i = e[i].ne) {if(e[i].v == rt) continue;DFS(e[i].v,cur);val[cur] += val[e[i].v];}if(cur != 1) ans = min(ans,val[cur]);
}
int main()
{int t,iCase=0;cin>>t;while(t--) {scanf("%d%d",&n,&m);tot=0,ans=0x3f3f3f3f;for(int i = 1; i<=n; i++) head[i] = -1,val[i]=0;for(int u,v,i = 1; i<=n-1; i++) {scanf("%d%d",&u,&v);add(u,v);add(v,u);}dfs(1,0);for(int u,v,i = n; i<=m; i++) {scanf("%d%d",&u,&v);val[u]++,val[v]++;val[lca(u,v)]-=2;}DFS(1,0);printf("Case #%d: %d\n",++iCase,ans+1);}return 0 ;
}

另一个解题报告:https://blog.csdn.net/cqbztsy/article/details/50706555

 

树形dp做法:https://www.cnblogs.com/qscqesze/p/4822468.html

对于这个点,如果要消除他和他父亲之间的联系的话,代价就是他的子树所有连向外界的边就好了

跑一遍dfs维护一下就行了。

(但是好像证明是不对的

2
4 4 1 2 2 3 3 4 1 3
4 4 1 3 2 3 2 4 1 2 

这组数据,应该是1 1,但是下面这个代码输出的是1 2)

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 20050
#define mod 10007
#define eps 1e-9
int Num;
char CH[20];
//const int inf=0x7fffffff;   //нчоч╢С
const int inf=0x3f3f3f3f;
//*********************************************************************************vector<int> Q[maxn];
vector<int> E[maxn];
int vis[maxn];
int dp[maxn];
int ans;
void dfs(int x)
{vis[x]=1;for(int i=0;i<Q[x].size();i++){int y=Q[x][i];dfs(Q[x][i]);dp[x]+=dp[y]-1;}ans = min(ans,dp[x]);for(int i=0;i<E[x].size();i++)if(vis[E[x][i]])dp[E[x][i]]--;vis[x]=0;
}
int main()
{int t;scanf("%d",&t);for(int cas = 1;cas<=t;cas++){ans = 99999999;int n,m;scanf("%d%d",&n,&m);for(int i=0;i<=n;i++)Q[i].clear(),E[i].clear();memset(dp,0,sizeof(dp));memset(vis,0,sizeof(vis));for(int i=0;i<n-1;i++){int x,y;scanf("%d%d",&x,&y);dp[x]++,dp[y]++;if(x>y)swap(x,y);Q[x].push_back(y);}for(int i=n-1;i<m;i++){int x,y;scanf("%d%d",&x,&y);dp[x]++,dp[y]++;if(x>y)swap(x,y);E[y].push_back(x);}dfs(1);printf("Case #%d: %d\n",cas,ans);}
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440193.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Idea自带的工具打jar包和Maven打Jar包(SpringBoot工程)

1.Idea自带的工具打jar包 &#xff08;1&#xff09;点击菜单栏的File后选中Project Structure&#xff0c;接着按如下图所示操作&#xff1a; &#xff08;2&#xff09;点击“OK”按钮后会出现下图的界面&#xff0c;然后继续点击“OK”按钮 &#xff08;3&#xff09;现在开…

图解算法学习笔记(目录)

今天遇到一本好书&#xff0c;如下&#xff0c;书很薄&#xff0c;不到200页&#xff0c;有将近400张图片&#xff0c;算法介绍的很有趣。这也是我读的第三本袁国忠先生翻译的书&#xff0c;向两位致敬。 目录大致如下; 第1章&#xff1a;二分查找和大O表示法&#xff1b; 第…

2019ACM浪潮杯山东省赛参赛总结

emmm是要记录一下生活了呢&#xff0c;不然以后退役了连自己经历过什么都记不住了。 5.11周六&#xff0c;早上5:30分&#xff0c;qdu集训队一行40人(左右)集合登上大巴&#xff0c;前往济南大学参加ACM省赛。上车清点了一下人数&#xff0c;然后发车以后就睡着了&#xff0c;…

Linux系统查看开放的端口、开启指定端口、关闭指定端口和查看及删除定时任务

Linux系统管理端口的操作命令 以下操作在需要开启防火墙&#xff0c;防火墙的开启(重启)、关闭和查看防火墙的状态见末尾 1.查看所有已经对外开放的端口&#xff1a;firewall-cmd --list-ports 2.开启指定的端口&#xff1a;firewall-cmd --zonepublic --add-port8080/tcp -…

图解算法学习笔记(一): 算法简介

本章内容&#xff1a; 编写第一种查找算法——二分查找。 学习如何谈论算法的运行时间——大O表示法。 1) 算法是一组完成任务的指令&#xff0c;任何代码片段都可视为算法。 2)二分查找&#xff1a;一种查找算法&#xff0c;其输入是一个有序的元素列表。 Python实现二分查…

用友通ERP客户端报无法登陆错

用友通ERP客户端报“无法登陆”错 排除系统版本错&#xff0c;要求windows xp professional sp2&#xff1b;client.dll文件错误后 请确认 c:/windows/system32/drivers/etc/目录下存在 hosts 文件&#xff0c;并且含有 127.0.0.1 localhost 行

【POJ - 3249】Test for Job(DAG线性求带负权的最长路,dp)

题干&#xff1a; Mr.Dog was fired by his company. In order to support his family, he must find a new job as soon as possible. Nowadays, Its hard to have a job, since there are swelling numbers of the unemployed. So some companies often use hard tests for …

图解算法学习笔记(二): 选择排序

目录 1)数组和链表&#xff1a; 2)选择排序算法&#xff1a; 3)小结 本章内容&#xff1a; 两种基本数据结构&#xff1a;数组和链表&#xff1b; 选择排序算法&#xff1b; 1)数组和链表&#xff1a; 数组是连续的内存单元&#xff0c;链表可以不连续&#xff1b; 链表…

javascript递归遍历文件夹下面的所有文件并返回所有文件全路径名称数组以及解析JavaScript方法体字符串的结束位置

一、前端脚本经常需要用到遍历指定文件夹下面的所有文件&#xff08;包含子文件夹&#xff09;的内容并做特定的逻辑处理&#xff0c;下面给出同步遍历的方式&#xff0c;开箱即用。 const fs require(fs);main()function main() {let allFiles getAllFiles(srcDir);console…

WinXP下替代IIS的新思路

WinXP下&#xff0c;.Net服务器有两个选择&#xff0c;IIS和Webdev.webservice。然而&#xff0c;IIS有最多十连接的限制&#xff0c;网上的解决方式&#xff08;包括注册表修改、微软工具、NTSwitch&#xff09;均未能突破&#xff1b;webdev.webservice虽没有连接数量限制&am…

【HDU - 5456】Matches Puzzle Game(数位dp,思维)

题干&#xff1a; As an exciting puzzle game for kids and girlfriends, the Matches Puzzle Game asks the player to find the number of possible equations A−BCA−BC with exactly n (5≤n≤500)n (5≤n≤500) matches (or sticks). In these equations, A,BA,B and …

图解算法学习笔记(三):递归

本章内容&#xff1a; 学习递归&#xff1b;如何将问题分解成基线条件和递归条件。 1) 每个递归函数都有两部分&#xff1a;基线条件(base case)和递归条件(recursive base)。例如&#xff1a;打印3...2...1 def countdown(i):print(i)if i < 0:returnelse:countdown(i…

深入理解Angular模块化概念

深入理解Angular模块NgModule装饰器 Angular应用程序全部是由 模块化组成的 &#xff0c;即模块化开发&#xff08;组件/指令/服务/管道/路由&#xff09;&#xff0c;与js模块化是不同的概念&#xff0c;但具有异曲同工之妙&#xff1b; Angular 模块之间是隔离 的&#xff0…

在IIS中启用父路径,不被黑客利用

在IIS中&#xff0c;有时要启用父路径&#xff0c;但黑客常常利用父路径访问硬盘文件。因此&#xff0c;我使用了一种方法&#xff1a; 先将IIS暂停&#xff0c;启用父路径。之所以暂停是为了防止操作时遭攻击然后&#xff0c;在根目录下创建虚拟路径“..”&#xff0c;任意选择…

【HRBUST - 1996】数学等式 (HASH 或 二分)

题干&#xff1a; 又到了数学题的时刻了&#xff0c;给出三个数组A,B,C,然后再给出一个数X&#xff0c;现在我想知道是否能找到三个数满足等式A[i]B[j]C[k]X&#xff0c;你能帮助我么&#xff1f;&#xff1f; Input 本题有多组数据&#xff0c;每组数据第一行输入三个数n, …

Apollo自动驾驶入门课程第⑨讲 — 控制(上)

目录 1. 简介 2. 控制流程 3. PID控制 4. PID优劣对比 本文转自微信公众号&#xff1a;Apollo开发者社区 原创&#xff1a; 阿波君 Apollo开发者社区 9月26日 上周我们发布了无人驾驶技术的 规划篇&#xff0c;车辆基于高精地图&#xff0c;感知和预测模块的数据来进行这一…

Angular高版本中为自定义的独立class类添加显式的Angular装饰器

Angular9或10及以后的版本&#xff0c;如果自定义的类上面没有写装饰器的话&#xff0c;编译后在Browser平台不会报错&#xff0c;但是在执行打包命令npm run build --prod时就会报错如下所示&#xff1a; error NG2007: Class is using Angular features but is not decorate…

Python操作Kafka爬坑

组内做大数据&#xff0c;需要kafka写入数据&#xff0c;最近在看python正好&#xff0c;练练手&#xff0c;网上找了一圈&#xff0c;都是用的pykafka&#xff0c;经过一整圈的安装&#xff0c;最终搞定&#xff0c;代码如下#coding:u8import sysimport timeimport randomimpo…

Apollo自动驾驶入门课程第⑩讲 — 控制(下)

目录 1. 线性二次调节器 2. 模型控制预测 3. 总结 本文转自微信公众号&#xff1a;Apollo开发者社区 原创&#xff1a; 阿波君 Apollo开发者社区 昨天 Apollo自动驾驶课程马上进入尾声&#xff0c;在无人驾驶技术控制篇&#xff08;上&#xff09;中&#xff0c;具体讲解了最…

*【ZOJ - 3703】Happy Programming Contest(带优先级的01背包)

题干&#xff1a; In Zhejiang University Programming Contest, a team is called "couple team" if it consists of only two students loving each other. In the contest, the team will get a lovely balloon with unique color for each problem they solved.…