首页 > 其他分享 >HDU 2242 考研路茫茫——空调教室

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

时间:2022-11-09 19:07:17浏览次数:45  
标签:HDU int 教室 空调 maxn low include 2242 考研


Problem Description


众所周知,HDU的考研教室是没有空调的,于是就苦了不少不去图书馆的考研仔们。Lele也是其中一个。而某教室旁边又摆着两个未装上的空调,更是引起人们无限YY。

一个炎热的下午,Lele照例在教室睡觉的时候,竟然做起了空调教室的美梦。

Lele梦到学校某天终于大发慈悲给某个教室安上了一个空调。而且建造了了M条通气管道,让整个教学楼的全部教室都直接或间接和空调教室连通上,构成了教室群,于是,全部教室都能吹到空调了。

不仅仅这样,学校发现教室人数越来越多,单单一个空调已经不能满足大家的需求。于是,学校决定封闭掉一条通气管道,把全部教室分成两个连通的教室群,再在那个没有空调的教室群里添置一个空调。

当然,为了让效果更好,学校想让这两个教室群里的学生人数尽量平衡。于是学校找到了你,问你封闭哪条通气管道,使得两个教室群的人数尽量平衡,并且输出人数差值的绝对值。


Input


本题目包含多组数据,请处理到文件结束。
每组测试第一行包含两个整数N和M(0<N<=10000,0<M<20000)。其中N表示教室的数目(教室编号从0到N-1),M表示通气管道的数目。
第二行有N个整数Vi(0<=Vi<=1000),分别代表每个教室的人数。
接下来有M行,每行两个整数Ai,Bi(0<=Ai,Bi<N),表示教室Ai和教室Bi之间建了一个通气管道。


Output


对于每组数据,请在一行里面输出所求的差值。
如果不管封闭哪条管道都不能把教室分成两个教室群,就输出"impossible"。


Sample Input


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


Sample Output


0 1



先用双联通缩点(tarjan算法现学现卖),然后简单的dfs一遍即可。



二合一版:



#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#include<cstdlib>
#include<iostream>
#include<vector>
using namespace std;
const int maxn=10005;
vector<int> tree[maxn];
stack<int> p;
int SUM,n,m,x,y,tot,num,ans,vis[maxn],dfn[maxn],low[maxn],a[maxn];

void begin()
{
for (int i=SUM=0;i<n;i++)
{
scanf("%d",&a[i]);
SUM+=a[i];
tree[i].clear();
}
for (int i=0;i<m;i++)
{
scanf("%d%d",&x,&y);
tree[x].push_back(y);
tree[y].push_back(x);
}
tot=num=0; ans=0X7FFFFFFF;
memset(vis,0,sizeof(vis));
while (!p.empty())p.pop();
}

void tarjan(int x,int fa)
{
dfn[x]=low[x]=tot++;
vis[x]=1; p.push(x);
int flag=1;
for (int i=0;i<tree[x].size();i++)
{
int y=tree[x][i];
if (y==fa&&flag) {flag=0; continue;}
if (!vis[y])
{
tarjan(y,x);
low[x]=min(low[x],low[y]);
if (dfn[x]<low[y])
{
int u,t=0;
do
{
u=p.top();
t+=a[u];
p.pop();
} while (u!=y);
ans=min(ans,abs(SUM-t-t));
a[x]+=t;
num++;
}
}
else low[x]=min(low[x],dfn[y]);
}
}

int main()
{
while (~scanf("%d%d",&n,&m))
{
begin(); tarjan(0,0);
if (num) printf("%d\n",ans);
else printf("impossible\n");
}
}



标签:HDU,int,教室,空调,maxn,low,include,2242,考研
From: https://blog.51cto.com/u_15870896/5838280

相关文章

  • HDU 4041 Eliminate Witches!
    ProblemDescriptionKanameMadokaisaMagicalGirl(MahouShoujo/PuellaMagi).ThedutyofaMagicalGirlistoeliminateWitches(Majo).Thoughsoundshorrif......
  • HDU 1237 简单计算器
    ProblemDescription读入一个只包含+,-,*,/的非负整数计算表达式,计算该表达式的值。Input测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字......
  • HDU 4739 Zhuge Liang's Mines
    ProblemDescriptionIntheancientthreekingdomperiod,ZhugeLiangwasthemostfamousandsmartestmilitaryleader.HisenemywasShimaYi,whoalways......
  • HDU 3608 最长回文
    ProblemDescription给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度.回文就是正反读都是一样的字符串,如aba,abba等 Inp......
  • HDU 5591 ZYB's Game
    ProblemDescription withhisclassmatesinhiking:ahostkeepsanumberin  loses,orthehostwilltellalltheplayersthatthenumbernowisbigger......
  • HDU 4433 locker
    ProblemDescriptionApasswordlockerwithNdigits,eachdigitcanberotatedto0-9circularly.Youcanrotate1-3consecutivedigitsupordownino......
  • HDU 5586 Sum
    ProblemDescriptionThereisanumbersequence ,youcanselectainterval[l,r]ornot,allthenumbers  willbecome ..Afterthat,thesumofnnumbers......
  • HDU 4436 str2int
    ProblemDescriptionInthisproblem,youaregivenseveralstringsthatcontainonlydigitsfrom'0'to'9',inclusive.Anexampleisshownbelow.1......
  • HDU 5264 pog loves szh I
    ProblemDescriptionPoghaslotsofstrings.Andhealwaysmixestwoequal-lengthstrings.Forexample,therearetwostrings:"abcd"and"efgh".Afterm......
  • HDU 5639 Deletion
    ProblemDescriptionG with n verticesand m edges.Everytime,youcanselectseveraledgesanddeletethem.Theedgesselectedmustmeetthe......