Hdu 4738 (dual Connection Bridge)

Source: Internet
Author: User

2013 ACM/ICPC Asia Regional Hangzhou Online question: there are n Islands, Cao has built some bridges between some islands, and some soldiers guard each bridge, zhou Yu only has one bomb that can only blow up one bridge. The bomb needs to be carried by soldiers. The number of soldiers cannot be smaller than the number of guards on the target bridge and the minimum number of soldiers to be dispatched is determined. At the beginning of the competition, yyf told me that the first question was a Unicom question. After talking about the question to me, I knew it was a pair of Unicom requests, but I saw that the first question of the competition was red, I think there must be a pitfall. I wrote it myself and the result is wrong. Later, I learned that there are two pitfalls in this question. If the graph is not connected, I don't need to blow it up, And I will output 0. The number of guards on the given data bridge is 0. If the answer is 0, a soldier will take a bomb.

# Include <stdio. h> # include <string. h> # define N 1010 int head [N], num, dfs [N], low [N], n, m, idx, bum, ans, opp; struct edge {int st, ed, w, next;} E [N * 2]; void addedge (int x, int y, int w) {E [num]. st = x; E [num]. ed = y; E [num]. next = head [x]; E [num]. w = w; head [x] = num ++; E [num]. st = y; E [num]. ed = x; E [num]. next = head [y]; E [num]. w = w; head [y] = num ++;} void Tarjan (int u, int id) {int I, v; low [u] = dfs [u] = idx ++; for (I = head [u]; I! =-1; I = E [I]. next) {v = E [I]. ed; if (I = (id ^ 1) continue; // if (dfs [v] =-1) {Tarjan (v, I); low [u] = low [u]> low [v]? Low [v]: low [u]; if (low [v]> dfs [u]) // bridge {bum ++; // number of bridges if (ans> E [I]. w) ans = E [I]. w ;}} else low [u] = low [u]> dfs [v]? Dfs [v]: low [u];} opp ++;} int main () {int I, x, y, sum, w; while (scanf ("% d", & n, & m), n | m) {memset (head,-1, sizeof (head )); num = 0; sum = 1; bum = 0; idx = 0; ans = 99999999; opp = 0; for (I = 0; I <m; I ++) {scanf ("% d", & x, & y, & w); addedge (x, y, w);} memset (dfs,-1, sizeof (dfs); Tarjan (1,-1); if (opp <n) printf ("0 \ n"); // the graph is not connected, else if (bum = 0) {printf ("-1 \ n");} // if the source image does not have a bridge, figure is a dual-link diagram. else if (ans = 0) printf ("1 \ n"); // obtain that the minimum guard of the bridge is 0, A soldier needs to take the bomb to else printf ("% d \ n", ans);} return 0 ;}

 

 

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.