考研路茫茫——空调教室HDU2242(Tarjan缩点)
题意:http://acm.hdu.edu.cn/showproblem.php?pid=2242
给你一个图,问你缩完点树上割边的做小绝对值差。
思路:
这题核算起来整整做了我一天(即24个小时)!!!一开始是MLE了近20发,然后TLE5、6发,再WA了一个晚上加一个下午。
有一种自闭是你突然对任何事物都失去追求:期中考?0分算了,这题再错我不学了还不行吗。
再加上HDU的评测机本来就很迷,该RE的判MLE,我N开1还MLE。服了嗷 !
算法里要注意的就是:Tarjan里因为你补了一条反向边,所以edge^1和edge都标记掉不要再dfs了。
然后Head链式前向星数组必须和tot变量初始化一致 -1 or 0,i!=-1 or 0,不然就炸了。(幸亏我同学帮我找到错误,不然我一个礼拜都难受)
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#include <cstdio>//sprintf islower isupper
#include <cstdlib>//malloc exit strcat itoa system("cls")
#include <iostream>//pair
#include <fstream>//freopen("C:\\Users\\13606\\Desktop\\Input.txt","r",stdin);
#include <bitset>
#include <map>
#include<unordered_map>
#include <vector>
#include <Stack>
#include <set>
#include <string.h>//strstr substr
#include <string>
#include <time.h>// srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
#include <cmath>
#include <deque>
#include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
#include <vector>//emplace_back
//#include <math.h>
#include <cassert>
//#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
#include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
#include <iomanip> using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
//******************
clock_t __STRAT,__END;
double __TOTALTIME;
void _MS(){__STRAT=clock();}
void _ME(){__END=clock();__TOTALTIME=(double)(__END-__STRAT)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
//***********************
#define rint register int
#define fo(a,b,c) for(rint a=b;a<=c;++a)
#define fr(a,b,c) for(rint a=b;a>=c;--a)
#define mem(a,b) memset(a,b,sizeof(a))
#define pr printf
#define sc scanf
#define ls rt<<1
#define rs rt<<1|1
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef long long ll;
const double E=2.718281828;
const double PI=acos(-1.0);
const ll INF=(1LL<<);
const int inf=(<<);
const double ESP=1e-;
const int mod=(int)1e9+;
const int N=(int); int val[N];
int peo[N];
int dfn[N],low[N],Stack[N],color[N];
bool vis[N];
int deep,top,sccn;//sccn是团;
int tot,head[N];//edge;
void Init()
{
tot=-;
deep=sccn=;
top=;
mem(Stack,);
mem(low,);
mem(dfn,);
mem(vis,);
mem(head,-);
mem(val,);
}
struct node
{
int next,to;
bool vis;
}edge[N];
void add(int from,int to)
{
++tot;
edge[tot].to=to;
edge[tot].next=head[from];
head[from]=tot;
}
void Tarjan(int u,int fa)
{
dfn[u]=++deep;
low[u]=deep;
vis[u]=true;
Stack[++top]=u;
for(int i=head[u];i!=-;i=edge[i].next)
{
int to=edge[i].to;
if(edge[i].vis) continue;
edge[i].vis=edge[i^].vis=;
if(!dfn[to])
{
Tarjan(to,u);
low[u]=min(low[u],low[to]);
}
else if(vis[to])
low[u]=min(low[u],low[to]);
}
if(dfn[u]==low[u])
{
++sccn;
do{
vis[Stack[top]]=false;
color[Stack[top]]=sccn;
val[sccn]+=peo[Stack[top]];
}while(Stack[top--]!=u);
}
}
struct
{
int u,v;
}e[N];
int ans;
int sum;
int dfs(int u,int fa)
{
int temp=val[u];
vis[u]=;
for(int i=head[u];i;i=edge[i].next)
{
int to=edge[i].to;
if(to!=fa&&!vis[to])
{
temp+=dfs(to,u);
}
}
ans=min(ans,abs(sum-temp*));
return temp;
} int main()
{
int n,m;
while(~sc("%d%d",&n,&m))
{
Init();
sum=;
for(int i=;i<=n;++i)
sc("%d",&peo[i]),sum+=peo[i];
mem(edge,);
for(int i=;i<=m;++i)
{
int u,v;
sc("%d%d",&u,&v);
u++,v++;
e[i]={u,v};
add(u,v);
add(v,u);
}
for(int i=;i<=n;++i)
if(!dfn[i])
Tarjan(i,-);
if(sccn==)
{
pr("impossible\n");
continue;
}
ans=2e9;
mem(head,);
tot=;
mem(edge,);
for(int i=;i<=m;++i)
{
if(color[e[i].u]==color[e[i].v])continue;
add(color[e[i].u],color[e[i].v]);
add(color[e[i].v],color[e[i].u]);
}
mem(vis,);
dfs(,);
pr("%d\n",ans);
}
return ;
} /**************************************************************************************/
考研路茫茫——空调教室HDU2242(Tarjan缩点)的更多相关文章
- HDU 2242 考研路茫茫——空调教室 无向图缩环+树形DP
考研路茫茫——空调教室 Problem Description 众所周知,HDU的考研教室是没有空调的,于是就苦了不少不去图书馆的考研仔们.Lele也是其中一个.而某教室旁边又摆着两个未装上的空调,更 ...
- HDU2242 考研路茫茫——空调教室 (双联通分+树形DP)
考研路茫茫——空调教室 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 2242 考研路茫茫——空调教室(边双连通)
HDU 2242 考研路茫茫--空调教室 题目链接 思路:求边双连通分量.然后进行缩点,点权为双连通分支的点权之和,缩点完变成一棵树,然后在树上dfs一遍就能得出答案 代码: #include < ...
- HDU 2242 考研路茫茫----空调教室
传送门 考研路茫茫——空调教室 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- HDU 2242 考研路茫茫——空调教室
考研路茫茫——空调教室 http://acm.hdu.edu.cn/showproblem.php?pid=2242 分析: 树形dp,删边. 代码: #include<cstdio> # ...
- 【HDOJ】2242 考研路茫茫——空调教室
tarjan缩点,然后树形dp一下可解.重点是重边的处理. /* 2242 */ #include <iostream> #include <sstream> #include ...
- hdu2242 考研路茫茫——空调教室
弱联通 #include<iostream> #include<cstdio> #include<cstring> #include<map> #inc ...
- HDU 2242 考研路茫茫—空调教室 (边双连通+树形DP)
<题目链接> 题目大意: 给定一个连通图,每个点有点权,现在需要删除一条边,使得整张图分成两个连通块,问你删除这条边后,两联通块点权值和差值最小是多少. 解题分析: 删除一条边,使原连通图 ...
- HDU 2242 考研路茫茫——空调教室(边双连通分量+树形dp+重边标号)
http://acm.hdu.edu.cn/showproblem.php?pid=2242 题意: 思路:首先求一下双连通分量,如果只有一个双连通分量,那么无论断哪根管子,图还是连通的. 最后只需要 ...
随机推荐
- reGeorg(不需要外网ip的代理)
reGeorg _____ ______ __|___ |__ ______ _____ _____ ______ | | | ___|| ___| || ___|/ \| | | ___| | \ ...
- pwn学习日记Day22 《程序员的自我修养》读书笔记
知识杂项 软连接 命令: ln -s 原文件 目标文件 特征: 1.相当于windows的快捷方式 2.只是一个符号连接,所以软连接文件大小都很小 3.当运行软连接的时候,会根据连接指向找到真正的文件 ...
- 冲刺阶段——Day3
[今日进展] 完善黄金点游戏的算法与代码架构. 将文字界面改编为图形界面 码云链接:https://gitee.com/jxxydwt1999/20175215-java/blob/master/Go ...
- suduku
github地址 PSP: PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟) Planning 计划 30 30 Estimate 估 ...
- python Image 模块处理图片
Python-Image 基本的图像处理操作,有需要的朋友可以参考下. Python 里面最常用的图像操作库是 pip install Pillow #安装模块 from PIL import Ima ...
- iOS获取应用当前Caches目录路径以及当前日期
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSSt ...
- thinkphp5 更改入口文件在主目录
默认thinkPHP入口文件在public/index.php,而在虚拟主机部署时,不能设置访问路径,因此需要将入口文件放置在主目录上. 一.主目录下新建index.php 复制以下内容 // 定义应 ...
- ubuntu docker 环境安装
转载:https://www.cnblogs.com/blog-rui/p/9946382.html 1. 在Ubuntu中安装Docker 更新ubuntu的apt源索引 sudo apt-get ...
- BTE的一些知识
1.今天遇到一个问题,看项目上做了BTE增强00001120 事件的BTE 但是自定义函数 更改了参数 t_bseg t_bkpf MODIFY t_bkpf FROM ls_bkpf TRAN ...
- golang继承与接口
继承 结构体 Go语言的结构体(struct)和其他语言的类(class)有同等的地位,但Go语言放弃了包括继 承在内的大量面向对象特性,只保留了组合(composition)这个最基础的特性. 组合 ...