HDU 2242 连通分量缩点+树形dp
题目大意是:
所有点在一个连通图上,希望去掉一条边得到两个连通图,且两个图上所有点的权值的差最小,如果没有割边,则输出impossible
这道题需要先利用tarjan算法将在同一连通分量中的点缩成一个点后,重新构建一幅图,然后利用新建的图进行树形dp解决问题
这道题目需要注意的是可能存在重边,那么子节点回到父节点有两条边及以上的话,就需要对子节点经过父节点的边进行low值更新
tarjan算法学习的网站个人感觉还不错https://www.byvoid.com/blog/scc-tarjan/
/*
找割边是否存在
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <stack>
using namespace std; typedef long long ll;
const int N = ;
const int INF = ; int first[N] , k , k_scc , val[N] , val_scc[N] , sum[N] , all ;
int scc[N] , num_of_scc , dfs_clock , dfn[N] , low[N];
stack<int> s; struct Edge{
int x , y , next ;
bool flag;
}e[N<<] , e_scc[N<<]; int my_abs(int x)
{
return x>=?x:-x;
} void add_edge(int x , int y)
{
e[k].x = x , e[k].y = y , e[k].next = first[x];
first[x] = k++;
} void add_edge_scc(int x , int y)
{
e_scc[k_scc].x = x , e_scc[k_scc].y = y , e_scc[k_scc].next = first[x] , e_scc[k_scc].flag = false;
first[x] = k_scc++;
} void tarjan(int u , int fa)
{
dfn[u] = low[u] = ++dfs_clock;
s.push(u);
int flag = ; //用来解决重边情况
for(int i=first[u] ; i!=- ; i=e[i].next){
int v = e[i].y;
/*
因为第一次遇到父节点的边,表示是一开始下来的边,这个是不允许访问的,
但是如果遇到2次及以上,说明u v之间不止一条边,访问到第二次之后的边是允许
low[u]通过这个重边得到dfn[v]比较下的较小值进行更新
*/
if(v == fa && flag){
flag = ;
continue;
}
if(!dfn[v]){
tarjan(v,u);
low[u] = min(low[u] , low[v]);
}
else if(!scc[v])
low[u] = min(low[u] , dfn[v]);
}
if(low[u] == dfn[u]){
num_of_scc++;
while(true){
int x = s.top();
s.pop();
scc[x] = num_of_scc;
val_scc[num_of_scc] += val[x];//得到新构建图上的点的权值
if(x == u) break;
}
}
} void dfs(int u , int fa)
{
sum[u] = val_scc[u];
for(int i=first[u] ; i!=- ; i=e_scc[i].next){
int v = e_scc[i].y;
if(v == fa) continue;
e_scc[i].flag = true;
dfs(v , u);
sum[u]+=sum[v];
}
} int main()
{
// freopen("a.in" , "r" , stdin);
int n , m , x , y;
while(scanf("%d%d" , &n , &m) == ){
memset(first , - , sizeof(first));
k= , all = ;
for(int i= ; i<n ; i++)
{
scanf("%d" , val+i);
all += val[i];
} for(int i= ; i<m ; i++){
scanf("%d%d" , &x , &y);
add_edge(x , y);
add_edge(y , x);
} //强连通分量缩点
dfs_clock = , num_of_scc = ;
memset(dfn , ,sizeof(dfn));
memset(scc , , sizeof(scc));
memset(val_scc , , sizeof(val_scc));
tarjan( , -); if(num_of_scc == ){
puts("impossible");
continue;
} //重新构建一个以连通分量缩点后的树形图
memset(first , - , sizeof(first));
k_scc = ;
for(int i= ; i<k ; i++){
if(scc[e[i].x] == scc[e[i].y]) continue;
add_edge_scc(scc[e[i].x] , scc[e[i].y]);
}
dfs( , -); int minn = INF;
for(int i= ; i<k_scc ; i++){
if(!e_scc[i].flag) continue;
minn = min(minn , my_abs(all - sum[e_scc[i].y] - sum[e_scc[i].y]) );
}
printf("%d\n" , minn);
}
return ;
}
HDU 2242 连通分量缩点+树形dp的更多相关文章
- hdu 2242双联通分量+树形dp
/*先求出双联通缩点,然后进行树形dp*/ #include<stdio.h> #include<string.h> #include<math.h> #defin ...
- hdu 4514 并查集+树形dp
湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tot ...
- 洛谷 P2515 [HAOI2010]软件安装(缩点+树形dp)
题面 luogu 题解 缩点+树形dp 依赖关系可以看作有向边 因为有环,先缩点 缩点后,有可能图不联通. 我们可以新建一个结点连接每个联通块. 然后就是树形dp了 Code #include< ...
- 训练指南 UVA - 11324(双连通分量 + 缩点+ 基础DP)
layout: post title: 训练指南 UVA - 11324(双连通分量 + 缩点+ 基础DP) author: "luowentaoaa" catalog: true ...
- [HDU 5293]Tree chain problem(树形dp+树链剖分)
[HDU 5293]Tree chain problem(树形dp+树链剖分) 题面 在一棵树中,给出若干条链和链的权值,求选取不相交的链使得权值和最大. 分析 考虑树形dp,dp[x]表示以x为子树 ...
- HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...
- hdu 4003 Find Metal Mineral 树形DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4003 Humans have discovered a kind of new metal miner ...
- HDU 5758 Explorer Bo(树形DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5758 [题目大意] 给出一棵树,每条路长度为1,允许从一个节点传送到任意一个节点,现在要求在传送次 ...
- 2017 Multi-University Training Contest - Team 1 1003&&HDU 6035 Colorful Tree【树形dp】
Colorful Tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
随机推荐
- E20171102-E
segment n. 环节; 部分,段落; [计算机] (字符等的) 分段; [动物学] 节片; distinct adj. 明显的,清楚的; 卓越的,不寻常的; 有区别的; 确切的;
- P4556 [Vani有约会]雨天的尾巴(线段树合并)
传送门 一道线段树合并 首先不难看出树上差分 我们把每一次修改拆成四个,在\(u,v\)分别放上一个,在\(lca\)和\(fa[lca]\)各减去一个,那么只要统计一下子树里的总数即可 然而问题就在 ...
- Windows 使用之那些你还不知道操作
作者:你未读 整理:君未读 关于使用 win 系统的基本普及. 建议更换 win10 系统 可能很多朋友看到这个建议,心里很不爽,还很不服气,别急,且看官方给出的信息. 也就是说在 2019 年你完全 ...
- WKWebView 和 UIWebView 允许背景音乐自动播放(记录)
WKWebView WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init]; config.allowsInlin ...
- C#---数据库访问通用类、Access数据库操作类、mysql类 .
//C# 数据库访问通用类 (ADO.NET)using System;using System.Collections.Generic;using System.Text;using System. ...
- Storm概念学习系列之storm的可靠性
这个概念,对于理解storm很有必要. 1.worker进程死掉 worker是真实存在的.可以jps查看. 正是因为有了storm的可靠性,所以storm会重新启动一个新的worker进程. 2.s ...
- Snipaste强大离线/在线截屏软件的下载、安装和使用
步骤一: https://zh.snipaste.com/ ,去此官网下载. 步骤二:由于此是个绿色软件,直接解压即可. 步骤三:使用,见官网.ttps://zh.snipaste.com 按F1 ...
- 专题七:UDP编程补充——UDP广播程序的实现
一.程序实现 UDP广播程序的实现代码: using System; using System.Net; using System.Net.Sockets; using System.Text; us ...
- FCC 基础JavaScript 练习1
1.JavaScript中的注释方式有以下两种 // This is an in-line comment. /* This is a multi-line comment */ 2.avaScrip ...
- React Native组件的结构和生命周期
React Native组件的结构和生命周期 一.组件的结构 1.导入引用 可以理解为C++编程中的头文件. 导入引用包括导入react native定义的组件.API,以及自定义的组件. 1.1 导 ...