The boundless road--air-conditioned classroomTime limit:4000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)Total Submission (s): 2447 Accepted Submission (s): 721
Problem description Well-known, HDU of the examination of the classroom 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.
Input This topic contains multiple sets of data, please process to the end of the file.
The first row of each group of tests contains two integers N and M (0<n<=10000,0<m<20000). where n represents the number of classrooms (classroom numbering is from 0 to N-1) and M represents the number of ventilation ducts.
The second line has n integer Vi (0<=vi<=1000), representing the number of each classroom.
Next there are m lines, each line of two integers ai,bi (0<=ai,bi<n), which indicates that a ventilation pipeline has been built between classroom Ai and classroom Bi.
Output for each set of data, print out the difference in one line.
The output of "impossible" if the classroom cannot be divided into two classrooms regardless of which pipe is closed.
Sample Input
4 31 1 1 10 11 22 34 31 2 3 50 11 22 3
Sample Output
01
no words, no to the edge of the double-link contraction point has not been written for a long time. This time to write a pinch point because not to filter out the heavy edge results in the test data are not enough, looking for a long time to find the error.
IdeasA: To find the total number, and then use Tarjan to find the edge-double connected EBC, and then to build a new map and find out the number of each EBC, the number of each EBC as the corresponding node after the weight value. Second: The new image as a tree, you can use the idea of the tree-shaped DP. Traverse the whole tree and record the sum of the points of all nodes of the tree with the root of U num[, each updated ans = min (ans, ABS (TOTAL-2 * num[u)), and the last ans is the answer.
AC Code:
#include <cstdio> #include <cstring> #include <stack> #include <queue> #include <vector># Include <cmath> #include <cstdlib> #include <algorithm> #define MAXN 10000+10#define MAXM 40000+100# Define INF 10000000using namespace std;struct edge{int from, to, next;}; Edge Edge[maxm];int HEAD[MAXN], Edgenum;int man[maxn];int LOW[MAXN], Dfn[maxn];int dfs_clock;int Ebcno[MAXN], ebc_cnt; stack<int> s;bool instack[maxn];vector<int> ebc[maxn];int N, m;//n classrooms M pipes int total;//Records total number of classrooms void init ( ) {edgenum = 0; Memset (Head,-1, sizeof (head));} void Addedge (int u, int v) {Edge E = {u, V, Head[u]}; Edge[edgenum] = E; Head[u] = edgenum++;} void Getmap () {int A, B; while (m--) {scanf ("%d%d", &a, &b); Addedge (A, b); Addedge (b, a); }}void Tarjan (int u, int fa) {int V, flag = 0; Low[u] = dfn[u] = ++dfs_clock; S.push (U); Instack[u] = true; for (int i = head[u]; I! =-1; i = edge[i].next) {v = edge[i].to; if (v = = FA &&!flag) {flag = 1; Continue } if (!dfn[v]) {Tarjan (V, u); Low[u] = min (Low[u], low[v]); } else if (Instack[v]) low[u] = min (Low[u], dfn[v]); } if (low[u] = = Dfn[u]) {ebc_cnt++; Ebc[ebc_cnt].clear (); for (;;) {v = s.top (); S.pop (); INSTACK[V] = false; EBCNO[V] = ebc_cnt; Ebc[ebc_cnt].push_back (v); if (v = = u) break; }}}void find_cut (int l, int r) {dfs_clock = ebc_cnt = 0; memset (Low, 0, sizeof (low)); memset (DFN, 0, sizeof (DFN)); memset (ebcno, 0, sizeof (EBCNO)); Memset (Instack, False, sizeof (Instack)); for (int i = l; I <= R; i++) if (!dfn[i]) Tarjan (i,-1);} int ebc_man[maxn];//records the number of each EBC vector<int> g[maxn];void Suodian () {for (int i = 1; I <= ebc_cnt; i++) { G[i].clear (); int sum = 0; for (int j = 0; J < Ebc[i].size (); j + +) sum + = man[ebc[i][j]]; Ebc_man[i] = sum; } for (int i = 0; i < edgenum; i+=2)//build diagram first without adding i+=2 ... {int u = Ebcno[edge[i].from]; int v = ebcno[edge[i].to]; if (U = v) g[u].push_back (v), g[v].push_back (U); }}int ans;//Minimum difference int num[maxn];//statistic child node weights sum void DFS (int u, int fa)//Solve minimum difference {num[u] = Ebc_man[u]; for (int i = 0; i < g[u].size (); i++) {int v = g[u][i]; if (v = = FA) continue; DFS (V, u); Num[u] + = Num[v]; } ans = min (ans, ABS (TOTAL-2 * num[u]));//update}void solve () {find_cut (0, N-1);//Find EBC if (ebc_cnt = 1)//Only one EBC {printf ("impossible\n"); return; } Suodian ();//shrink-point ans = INF; DFS (1,-1);//Traverse Whole tree printf ("%d\n", ans);} int main () {while (scanf ("%d%d", &n, &m)! = EOF) {total = 0; for (int i = 0; i < N; i++) scanf ("%d", &man[i]), total + = man[i];//Total count Init (); Getmap (); Solve (); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDOJ 2242 Entrance Examination Road boundless--air-conditioning classroom "non-map to the edge of double-link reduction + tree-type DP"