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 ;}