HDU 2242 Entrance Examination Road boundless--air-conditioning classroom (double connected component + Tree DP)

Source: Internet
Author: User

Title Address: HDU 2242

The two-connected component is used to shrink the point, then a tree is formed, and then a tree DP is made on the tree to find out the subtrees of each point. Then find the minimum value. Note the problem of heavy edges, which can be ignored the first time you return to the parent node, because this is a reverse edge, and then it is not reversed when you return. Can't ignore it.

The code is as follows:

#include <iostream> #include <string.h> #include <math.h> #include <queue> #include < algorithm> #include <stdlib.h> #include <map> #include <set> #include <stdio.h>using namespace std; #define LL Long long#define pi ACOs ( -1.0) const int Mod=1e9+7;const int Inf=0x3f3f3f3f;const double eqs=1e-9; const int Maxn=10000+10;int HEAD[MAXN], ecnt, top, indx, Scc;int LOW[MAXN], DFN[MAXN], BELONG[MAXN], STK[MAXN], DP[MAXN], a[maxn];struct node{int U, V, next;}        edge[maxn<<2];void Add (int u, int v) {edge[ecnt].u=u;        Edge[ecnt].v=v;        Edge[ecnt].next=head[u]; head[u]=ecnt++;}        void Tarjan (int u, int fa) {low[u]=dfn[u]=++indx;        Stk[++top]=u;        int flag=1;                for (int i=head[u];i!=-1;i=edge[i].next) {int v=edge[i].v;                        if (V==fa&&flag) {flag=0;                Continue;                        } if (!dfn[v]) {Tarjan (V,u);                Low[u]=min (Low[u],low[v]);        } else Low[u]=min (Low[u],dfn[v]);                } if (Low[u]==dfn[u]) {scc++;                        while (1) {int v=stk[top--];                        BELONG[V]=SCC;                        DP[SCC]+=A[V];                if (u==v) break;        }}}void init () {memset (head,-1,sizeof (head));        memset (dfn,0,sizeof (DFN));        Memset (Dp,0,sizeof (DP)); ecnt=top=indx=scc=0;} int HEAD1[MAXN], Ecnt1, vis[maxn];struct node1{int u, V, next;}        edge1[maxn<<2];void add1 (int u, int v) {edge1[ecnt1].v=v;        Edge1[ecnt1].next=head1[u]; head1[u]=ecnt1++;}        void Dfs (int u) {vis[u]=1;                for (int i=head1[u];i!=-1;i=edge1[i].next) {int v=edge1[i].v;                if (Vis[v]) continue;                DFS (v);        DP[U]+=DP[V]; }}void init1 () {memset (head1,-1,sizeof (heaD1));        memset (vis,0,sizeof (VIS)); ecnt1=0;}        int main () {int n, m, u, V, I, J, Sum, min1;                while (scanf ("%d%d", &n,&m)!=eof) {sum=0;                        for (i=0;i<n;i++) {scanf ("%d", &a[i]);                Sum+=a[i];                } init ();                        while (m--) {scanf ("%d%d", &u,&v);                        Add (U,V);                Add (V,u);                } for (i=0;i<n;i++) {if (!dfn[i]) Tarjan (i,-1);                        } if (scc==1) {puts ("impossible");                Continue;                } init1 (); for (i=0;i<n;i++) {for (J=head[i];j!=-1;j=edge[j].next) {v=edge[j].                                V if (Belong[i]!=belong[v]) {ADD1 (Belong[i],belong[v]);                                ADD1 (Belong[v],belong[i]);                }}} dfs (1);                Min1=inf;                for (i=1;i<=scc;i++) {min1=min (Min1,abs (Sum-2*dp[i]));        } printf ("%d\n", min1); } return 0;}


HDU 2242 Entrance Examination Road boundless--air-conditioning classroom (double connected component + Tree DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.