The main idea: it is well known that the hdu of the Postgraduate school is not air-conditioning, so the bitter many do not go to the library of postgraduate students. Lele is also one of them. And a classroom next to two not installed on the air-conditioning, but also cause people infinite yy.
A hot afternoon, lele as usual in the classroom when sleeping, unexpectedly did the dream of air-conditioned classrooms.
Lele Dream to school one day finally mercy to a classroom to install an air-conditioning. And the construction of the M-duct, so that the entire classroom building all the classrooms are directly or indirectly connected with the air-conditioned classrooms, forming a classroom group, so that all classrooms can be blown to the air-conditioning.
Not only that, the school found the number of classrooms more and more, a single air conditioning can not meet the needs of everyone. As a result, the school decided to close down a ventilation pipeline, dividing all the classrooms into two connected classrooms and adding an air conditioner to the non-air-conditioned classrooms.
Of course, to make it better, schools want to keep the number of students in these two classrooms as balanced as possible. So the school found you and asked you to close which ventilation pipe, so that the number of two classroom groups as far as possible balance, and output the absolute value of the difference.
Problem-solving ideas: Find all sides-two connected components, these side-double connected components can not operate, because deleting the edge does not increase the connectivity component
Then, all the edge-to-double connected components are shrunk and connected by a bridge, thus forming a tree without roots.
Special, only one side-two connected components.
Then you can do the DP on this tree.
Note that it is possible to re-edge, so pay attention to
#include <cstdio>#include <cstring>#define MIN (A, B) ((a) < (b)? (a): (b))#define ABS (x) ((x) > 0? (x): (-(x)))#define N 10010#define M 40010structEdge {intTo, next;} E[M];structnode{intx, y; }NODE[M];intCost[n], Head[n], bccno[n], Pre[n],Stack[N], low[n], num[n];intSum, N, M, tot, Dfs_clock, bcc_cnt, top;BOOLVis[n];voidAddedge (intUintV) {e[tot].to = v; E[tot].next = Head[u]; Head[u] = tot++; u = u ^ v; v = u ^ v; u = u ^ v; E[tot].to = v; E[tot].next = Head[u]; Head[u] = tot++;}voidInit () {Sum =0; for(inti =0; I < n; i++) {scanf("%d", &cost[i]); Sum + = Cost[i]; }memset(Head,-1,sizeof(head)); tot =0;intU, v; for(inti =0; I < m; i++) {scanf("%d%d", &u, &v); node[i].x = u; Node[i].y = v; Addedge (U, v); }}voidDfsintUintFA) {Low[u] = pre[u] = ++dfs_clock; Vis[u] =1;Stack[++top] = u;BOOLFlag =false; for(inti = Head[u]; I! =-1; i = e[i].next) {intv = e[i].to;if(v = = FA &&!flag) {flag =1;Continue;}//Two side, it is useless to seal one . if(!vis[v]) Dfs (v, u); Low[u] = min (Low[u], low[v]); }intXif(Pre[u] = = Low[u]) {bcc_cnt++; NUM[BCC_CNT] =0; while(1) {x =Stack[top--]; BCCNO[X] = bcc_cnt; NUM[BCC_CNT] + = cost[x];if(x = = u) Break; } }}intMin;intDP (intUintFA) {intall = Num[u]; for(inti = Head[u]; I! =-1; i = e[i].next) {intv = e[i].to;if(v! = FA) All + = DP (v, u); } min = min (min,ABS(Sum-2* all));returnAll;}voidSolve () {memset(Bccno,0,sizeof(BCCNO));memset(Pre,0,sizeof(pre));memset(Vis,0,sizeof(VIS)); Dfs_clock = bcc_cnt = top =0; Dfs0, -1);if(bcc_cnt = =1) {printf("impossible\n");return; }memset(Head,-1,sizeof(head)); tot =0;intx, y; for(inti =0; I < m; i++) {x = node[i].x; y = node[i].y;if(bccno[x]! = Bccno[y]) Addedge (Bccno[x], bccno[y]); } Min =0x3f3f3f3f; DP (1, -1);printf("%d\n", Min);}intMain () { while(scanf("%d%d", &n, &m)! = EOF) {init (); Solve (); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU-2242 Entrance Examination Road boundless-air-conditioned classroom (tree-shaped dp+ strong connected components)