题意: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缩点)的更多相关文章

  1. HDU 2242 考研路茫茫——空调教室 无向图缩环+树形DP

    考研路茫茫——空调教室 Problem Description 众所周知,HDU的考研教室是没有空调的,于是就苦了不少不去图书馆的考研仔们.Lele也是其中一个.而某教室旁边又摆着两个未装上的空调,更 ...

  2. HDU2242 考研路茫茫——空调教室 (双联通分+树形DP)

    考研路茫茫——空调教室 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  3. HDU 2242 考研路茫茫——空调教室(边双连通)

    HDU 2242 考研路茫茫--空调教室 题目链接 思路:求边双连通分量.然后进行缩点,点权为双连通分支的点权之和,缩点完变成一棵树,然后在树上dfs一遍就能得出答案 代码: #include < ...

  4. HDU 2242 考研路茫茫----空调教室

    传送门 考研路茫茫——空调教室 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  5. HDU 2242 考研路茫茫——空调教室

    考研路茫茫——空调教室 http://acm.hdu.edu.cn/showproblem.php?pid=2242 分析: 树形dp,删边. 代码: #include<cstdio> # ...

  6. 【HDOJ】2242 考研路茫茫——空调教室

    tarjan缩点,然后树形dp一下可解.重点是重边的处理. /* 2242 */ #include <iostream> #include <sstream> #include ...

  7. hdu2242 考研路茫茫——空调教室

    弱联通 #include<iostream> #include<cstdio> #include<cstring> #include<map> #inc ...

  8. HDU 2242 考研路茫茫—空调教室 (边双连通+树形DP)

    <题目链接> 题目大意: 给定一个连通图,每个点有点权,现在需要删除一条边,使得整张图分成两个连通块,问你删除这条边后,两联通块点权值和差值最小是多少. 解题分析: 删除一条边,使原连通图 ...

  9. HDU 2242 考研路茫茫——空调教室(边双连通分量+树形dp+重边标号)

    http://acm.hdu.edu.cn/showproblem.php?pid=2242 题意: 思路:首先求一下双连通分量,如果只有一个双连通分量,那么无论断哪根管子,图还是连通的. 最后只需要 ...

随机推荐

  1. c++ 使用类生成随机数

    // generate algorithm example #include <iostream> // cout #include <algorithm> // genera ...

  2. spring事务配置异常

    spring事务配置不回滚spring事务管理配置,一般来说都是可以回滚的,最近在开发的过程中遇到了一个异常不回滚的问题,最终找到了原因,贴出来一下 1.首先这里定义一个接口 在接口中定义几个方法 2 ...

  3. BufferedWriter中write与close函数使用

    BufferedWriter 是一个缓冲字符输出流,可以将要输出的内容先缓冲到一个字符数组中,等字符数组满了才一次性写到输出流内,默认的字符数组长度为8192.使用BufferedWriter 时需要 ...

  4. Async and Await (Stephen Cleary)

    https://blog.stephencleary.com/2012/02/async-and-await.html Most people have already heard about the ...

  5. CentOS7下安装php-redis扩展

    yum -y install php70w-pecl-redis

  6. redhat 7.6下如何更新YUM源(仓库)?

    1. 安装epel-release     yum install epel-release 2. 安装webstatic-release     rpm -Uvh https://mirror.we ...

  7. Maven 引入外部依赖

    pom.xml 的 dependencies 列表列出了我们的项目需要构建的所有外部依赖项. 要添加依赖项,我们一般是先在 src 文件夹下添加 lib 文件夹,然后将你工程需要的 jar 文件复制到 ...

  8. 通过OpenCL内核代码猜测设备寄存器个数

    在OpenCL标准中,没有给出查看计算设备一共有多少寄存器,至少能分配给每个work-item多少寄存器使用的特征查询.而由于一个段内核代码是否因寄存器紧缺而导致性能严重下降也是一个比较重要的因素,因 ...

  9. c++ throw异常(学习)

    #include <iostream>#include <stdio.h> using namespace std; void my_copy(const char* src_ ...

  10. 【leetcode_easy】551. Student Attendance Record I

    problem 551. Student Attendance Record I 题意: solution: class Solution { public: bool checkRecord(str ...