# 开篇

[https://www.luogu.com.cn/blog/DarthVictor/#](洛谷博客)
[https://www.cnblogs.com/DarthVictor/](博客园)

# 题目

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

Input

Output

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 const int maxn=10000+5,maxe=2*20000+5;
4 int head[maxn],dfn[maxn],low[maxn],rs[maxn],f[maxn],belong[maxn];
5 int h[maxn],ans;
6 bool bridge[maxe],vis[maxn];
7 int n,m,tot,sum,dfs_clock;bool judge;
8 struct edge{
9     int to,next;
10 }e[maxe],ed[maxe];
11 void Add(int a,int b){
12     e[tot].to=b;
13     e[tot].next=head[a];
14     head[a]=tot;
15     tot++;
16 }
17 void tarjan(int u,int id){
18     dfn[u]=low[u]=++dfs_clock;
19     for(int i=head[u];i;i=e[i].next){
20         if(i==(id^1)) continue;
21         int v=e[i].to;
22         if(!dfn[v]){
23             tarjan(v,i);
24             low[u]=min(low[u],low[v]);
25             if(dfn[u]<low[v]){
26                 judge=1;
27                 bridge[i]=bridge[i^1]=1;
28             }
29         }
30         else low[u]=min(low[u],dfn[v]);
31     }
32 }
33 void dfs(int x,int id){
34     vis[x]=1;
35     f[id]+=rs[x];
36     belong[x]=id;
37     for(int i=head[x];i;i=e[i].next){
38         if(!bridge[i]&&!vis[e[i].to]) dfs(e[i].to,id);
39     }
40 }
41 int dfs2(int x){
42     vis[x]=1;
43     for(int i=h[x];i;i=ed[i].next){
44         int to=ed[i].to;
45         if(!vis[to]) f[x]+=dfs2(to);
46     }
47     ans=min(ans,abs(sum-2*f[x]));
48     return f[x];
49 }
50 int main(){
51     while(scanf("%d%d",&n,&m)!=EOF){
52         tot=2; sum=0; judge=0; dfs_clock=0;
53         memset(f,0,sizeof(f));
54         memset(e,0,sizeof(e));
55         memset(h,0,sizeof(h));
56         memset(ed,0,sizeof(ed));
57         memset(dfn,0,sizeof(dfn));
58         memset(low,0,sizeof(low));
59         memset(vis,0,sizeof(vis));
60         memset(head,0,sizeof(head));
61         memset(belong,0,sizeof(belong));
62         memset(bridge,0,sizeof(bridge));
63         for(int i=1;i<=n;i++){
64             scanf("%d",&rs[i]);
65             sum+=rs[i];
66         }
67         for(int i=1;i<=m;i++){
68             int a,b;
69             scanf("%d%d",&a,&b);
70             Add(a+1,b+1);
71             Add(b+1,a+1);
72         }
73         for(int i=1;i<=n;i++) if(!dfn[i]) tarjan(i,head[i]);
74         if(!judge){
75             printf("impossible\n");
76             continue;
77         }
78         int id=0;
79         for(int i=1;i<=n;i++) {
80             if(!belong[i]){
81                 id++;
82                 dfs(i,id);
83             }
84         }
85         tot=2;
86         for(int i=1;i<=n;i++){
87             for(int j=head[i];j;j=e[j].next){
88                 int from=belong[i],to=belong[e[j].to];
89                 if(from!=to){
90                     ed[tot].to=to;
91                     ed[tot].next=h[from];
92                     h[from]=tot;
93                     tot++;
94                     /*ed[tot].to=from;
95                     ed[tot].next=h[to];
96                     h[to]=tot;
97                     tot++;*/
98                 }
99             }
100         }
101         ans=10000000;
102         memset(vis,0,sizeof(vis));
103         dfs2(1);
104         printf("%d\n",ans);
105     }
106
107 }

posted @ 2020-03-05 10:36  DarthVictor  阅读(403)  评论(1编辑  收藏  举报