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

考研路茫茫——空调教室

Time Limit: 2000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 2242
64-bit integer IO format: %I64d      Java class name: Main
 
众所周知,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

解题:边双连通分量

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const int maxn = 10010;
 5 const int INF = 0x3f3f3f3f;
 6 struct arc {
 7     int to,next;
 8     arc(int x = 0,int y = -1) {
 9         to = x;
10         next = y;
11     }
12 } e[200000];
13 int hd[maxn],hd2[maxn],low[maxn],dfn[maxn],belong[maxn],tot,clk,bcc;
14 void add(int *head,int u,int v) {
15     e[tot] = arc(v,head[u]);
16     head[u] = tot++;
17 }
18 stack<int>stk;
19 int sz[maxn],p[maxn],n,m;
20 void tarjan(int u,int fa) {
21     dfn[u] = low[u] = ++clk;
22     stk.push(u);
23     bool flag = false;
24     for(int i = hd[u]; ~i; i = e[i].next) {
25         if(!flag && e[i].to == fa) {
26             flag = true;
27             continue;
28         }
29         if(!dfn[e[i].to]) {
30             tarjan(e[i].to,u);
31             low[u] = min(low[u],low[e[i].to]);
32         } else low[u] = min(low[u],dfn[e[i].to]);
33     }
34     if(low[u] == dfn[u]) {
35         int v;
36         bcc++;
37         do {
38             v = stk.top();
39             stk.pop();
40             belong[v] = bcc;
41             sz[bcc] += p[v];
42         } while(v != u);
43     }
44 }
45 void init() {
46     for(int i = tot = bcc = clk = 0; i < maxn; ++i) {
47         hd[i] = hd2[i] = -1;
48         sz[i] = dfn[i] = belong[i] = 0;
49     }
50     while(!stk.empty()) stk.pop();
51 }
52 void dfs(int u,int fa) {
53     for(int i = hd2[u]; ~i; i = e[i].next) {
54         if(e[i].to == fa) continue;
55         dfs(e[i].to,u);
56         sz[u] += sz[e[i].to];
57     }
58 }
59 int main() {
60     int sum,u,v;
61     while(~scanf("%d%d",&n,&m)) {
62         for(int i = sum = 0; i < n; ++i) {
63             scanf("%d",p + i);
64             sum += p[i];
65         }
66         init();
67         for(int i = 0; i < m; ++i) {
68             scanf("%d%d",&u,&v);
69             add(hd,u,v);
70             add(hd,v,u);
71         }
72         for(int i = 0; i < n; ++i)
73             if(!dfn[i]) tarjan(i,-1);
74         if(bcc == 1) {
75             puts("impossible");
76             continue;
77         }
78         for(int i = 0; i < n; ++i)
79             for(int j = hd[i]; ~j; j = e[j].next) {
80                 if(belong[i] == belong[e[j].to]) continue;
81                 add(hd2,belong[i],belong[e[j].to]);
82             }
83         dfs(1,-1);
84         int ret = INF;
85         for(int i = 1; i <= bcc; ++i)
86             for(int j = hd2[i]; ~j; j = e[j].next)
87                 ret = min(ret,abs(sum - sz[i] - sz[i]));
88         printf("%d
",ret);
89     }
90     return 0;
91 }
View Code
原文地址:https://www.cnblogs.com/crackpotisback/p/4752932.html