HDU 4738 Caocao ' s Bridges (double Unicom component + and check set)

Source: Internet
Author: User

Effect: there are N islands and M-Bridge, each bridge has a W soldiers guarding, now to send not less than the number of soldiers to defend the bridge to the bridge, can only fry a bridge, so that the N island is not connected, ask at least how many people to send.


Idea: We just need to get the minimum number of the bridge directly after shrinking. (ps:1, note the figure if the direct output 0 is not unicom.) 2, if the figure of the bridge is 0, one so let a person go. 3, the problem of heavy edge. can be ignored here)


#include <map> #include <queue> #include <cmath> #include <cstdio> #include <stack># include<iostream> #include <cstring> #include <algorithm> #define LL int#define inf 0x3f3f3f3f#define EPS 1e-8#include<vector> #define LS l,mid,rt<<1#define rs mid+1,r,rt<<1|1using namespace Std;const int Ma = 1100;struct node{int to,w,next;} Q[ma*ma];int Head[ma*ma],dfn[ma],num[ma],du[ma],stk[ma*5],vis[ma],low[ma];int Cnt,top,tim,scc,out[Ma],f[Ma],n,mi    void Add (int a,int b,int c) {q[cnt].to = b;    Q[CNT].W = C;    Q[cnt].next = Head[a]; Head[a] = cnt++;}    void Init () {SCC = CNT = top = 0;    Tim = 1;    MI = inf;    memset (head,-1,sizeof (head));        for (int i = 1;i <= n;++ i) {f[i] = i;    Low[i] = vis[i] = Out[i] = Num[i] = Dfn[i] = 0;    }}void Tarjan (int u,int to) {Low[u] = dfn[u] = tim++;    Vis[u] = 1;    stk[top++] = u;        for (int i = head[u]; ~i; i = q[i].next) {int v = q[i].to; if (i = = (to^1)) ContinuE            if (!vis[v]) {Tarjan (v,i);            Low[u] = min (low[u],low[v]);        if (Low[v] > Dfn[u]) if (Q[I].W < mi) mi = q[i].w;    } else Low[u] = min (low[u],dfn[v]);        } if (low[u] = = Dfn[u]) {scc++;            while (Top > 0&&stk[top]! = u) {top--;            Vis[stk[top]] = 1;        Num[stk[top]] = SCC; }}}int Fi (int x) {return f[x] = = x x:f[x]=fi (f[x]);}    void mer (int a,int b) {int x = fi (a);    int y = fi (b); x > y? F[X] = y:f[y] = x;}    int main () {int m,i,j,k,a,b,c,cla;        while (~SCANF ("%d%d", &n,&m)) {if (!n&&!m) break;        Init ();                for (i = 0;i < m;++ i) {scanf ("%d%d%d", &a,&b,&c);                ADD (A,B,C);                ADD (B,A,C);        Mer (A, b);        } int tmp = 0;                for (i = 1;i <= n;++ i) if (f[i] = = i) {tmp++; if (tMP > 1) break;        } if (tmp > 1) {puts ("0"); continue;        } Tarjan (1,-1);                for (i = 1;i <= n;++ i) {for (j = head[i]; ~j; j=q[j].next) {int v = q[j].to;                if (Num[i]!=num[v]) {out[num[i]]++;        }}} int ans = 0;        for (i = 1;i <= scc;++ i) {if (out[i]==0) ans++;        } if (ans==1) {puts ("-1");        } else if (!mi) {puts ("1");    } else printf ("%d\n", MI); } return 0;}




HDU 4738 Caocao ' s Bridges (double Unicom component + and check set)

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.