锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
鎻忚堪錛?/strong>鍦ㄥ浜庝換閫変竴涓浘涓粨鐐逛負鏍圭殑DFS鎼滅儲鏍戜腑寤虹珛涓涓狶AB鏁扮粍涓嶭OW鏁扮粍錛孡AB鏁扮粍瀛樺偍涓粨鐐圭殑緙栧彿錛孡OW鏁扮粍瀛樺偍鍚勭偣鍙婂叾瀛愭爲鐨勫悇緇撶偣鑳藉埌杈劇殑鏈灝忕紪鍙風粨鐐圭殑緙栧彿銆?br>
2 DFS(u)
3 LAB[u] = LOW[u] = lab++
4 for each (u, v) in E(G)
5 if LAB[v] is 0
6 DFS(v)
7 LOW[u] = min{LOW[u], LOW[v]}
8 else if v isnot parent of u
9 LOW[u] = min{LOW[u], LAB[v]}
絎?琛屼腑錛屽鏋?u, v)鏄爲杈癸紝鍒欏v鍋氭繁搴︿紭鍏堟悳绱紝騫朵笖LOW[u] = min{LOW[u], LOW[v]}錛屽鏋?u, v)鏄弽鍚戣竟錛屽垯LOW[u] = min{LOW[u], LAB[v]}銆?/span>
涓夈佸壊鐐?br> 鎻忚堪錛?/strong>褰撲竴涓粨鐐箄鏄壊鐐規椂蹇呮弧瓚充互涓嬩袱涓潯浠朵箣涓錛?br> 1錛塽涓烘牴涓旇嚦灝戞湁涓ゆ5瀛愭爲錛?br> 2錛塽涓嶄負鏍逛笖瀛樺湪涓涓猽鍦ㄦ繁鎼滄爲涓殑瀛愬コv浣垮緱LOW[v] ≥ LAB[u]銆?br> 紺轟緥錛?/strong>POJ 1523 瑙i鎶ュ憡銆?br>鍥涖佹ˉ
鎻忚堪錛?/strong>涓鏉¤竟e=(u, v)鏄ˉ錛屽綋涓斾粎褰揺涓烘爲鏋濊竟涓擫OW[v] > LAB[u]銆?br> 紺轟緥錛?/strong>POJ 3352 瑙i鎶ュ憡銆?/span>
]]>
Description
It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of the roads on the tropical island paradise of Remote Island would like to repair and upgrade the various roads that lead between the various tourist attractions on the island.
The roads themselves are also rather interesting. Due to the strange customs of the island, the roads are arranged so that they never meet at intersections, but rather pass over or under each other using bridges and tunnels. In this way, each road runs between two specific tourist attractions, so that the tourists do not become irreparably lost.
Unfortunately, given the nature of the repairs and upgrades needed on each road, when the construction company works on a particular road, it is unusable in either direction. This could cause a problem if it becomes impossible to travel between two tourist attractions, even if the construction company works on only one road at any particular time.
So, the Road Department of Remote Island has decided to call upon your consulting services to help remedy this problem. It has been decided that new roads will have to be built between the various attractions in such a way that in the final configuration, if any one road is undergoing construction, it would still be possible to travel between any two tourist attractions using the remaining roads. Your task is to find the minimum number of new roads necessary.
Input
The first line of input will consist of positive integers n and r, separated by a space, where 3 ≤ n ≤ 1000 is the number of tourist attractions on the island, and 2 ≤ r ≤ 1000 is the number of roads. The tourist attractions are conveniently labelled from 1 to n. Each of the following r lines will consist of two integers, v and w, separated by a space, indicating that a road exists between the attractions labelled v and w. Note that you may travel in either direction down each road, and any pair of tourist attractions will have at most one road directly between them. Also, you are assured that in the current configuration, it is possible to travel between any two tourist attractions.
Output
One line, consisting of an integer, which gives the minimum number of roads that we need to add.
Sample Input
Sample Input 1 10 12 1 2 1 3 1 4 2 5 2 6 5 6 3 7 3 8 7 8 4 9 4 10 9 10 Sample Input 2 3 3 1 2 2 3 1 3
Sample Output
Output for Sample Input 1 2 Output for Sample Input 2 0
浜屻佸垎鏋?br> 鐢―FS瑙e喅闂錛岃緇嗙畻娉曪細鍓茬偣涓庢ˉ銆?/span>
涓夈佷唬鐮?br>
Description
Input
Output
Sample Input
1 2 5 4 3 1 3 2 3 4 3 5 0 1 2 2 3 3 4 4 5 5 1 0 1 2 2 3 3 4 4 6 6 3 2 5 5 1 0 0
Sample Output
Network #1 SPF node 3 leaves 2 subnets Network #2 No SPF nodes Network #3 SPF node 2 leaves 2 subnets SPF node 3 leaves 2 subnets
浜屻佸垎鏋?br> 鐢―FS瑙e喅闂錛岃緇嗙畻娉曪細鍓茬偣涓庢ˉ銆?/span>
涓夈佷唬鐮?br>
鍒嗘瀽錛?/strong>鍒濆鍖栬繃紼嬪疄闄呬笂鏄竴涓姩鎬佽鍒掔殑鎬濇兂銆傛槗鐭ワ紝鍒濆鍖栬繃紼嬫晥鐜囨槸O(nlogn)錛岃屾煡璇㈣繃紼嬫晥鐜囨槸O(1)銆係T鏄竴涓湪綰跨畻娉曘?br> 紺轟緥錛?/strong>POJ 3368 瑙i鎶ュ憡
2錛夋眰瑙CA闂
鎻忚堪錛?/strong>
錛?錛塂FS錛氫粠鏍慣鐨勬牴寮濮嬶紝榪涜娣卞害浼樺厛閬嶅巻錛屽茍璁板綍涓嬫瘡嬈″埌杈劇殑欏剁偣銆傜涓涓殑緇撶偣鏄痳oot(T)錛屾瘡緇忚繃涓鏉¤竟閮借褰曞畠鐨勭鐐廣傜敱浜庢瘡鏉¤竟鎭板ソ緇忚繃2嬈★紝鍥犳涓鍏辮褰曚簡2n-1涓粨鐐癸紝鐢‥[1, ... , 2n-1]鏉ヨ〃紺恒?br> 錛?錛夎綆桼錛氱敤R[i]琛ㄧずE鏁扮粍涓涓涓間負i鐨勫厓绱犱笅鏍囷紝鍗沖鏋淩[u] < R[v]鏃訛紝DFS璁塊棶鐨勯『搴忔槸E[R[u], R[u]+1, ..., R[v]]銆傝櫧鐒跺叾涓寘鍚玼鐨勫悗浠o紝浣嗘繁搴︽渶灝忕殑榪樻槸u涓巚鐨勫叕鍏辯鍏堛?br> 錛?錛塕MQ錛氬綋R[u] ≥ R[v]鏃訛紝LCA[T, u, v] = RMQ(L, R[v], R[u])錛涘惁鍒橪CA[T, u, v] = RMQ(L, R[u], R[v])錛岃綆桼MQ銆?br> 鐢變簬RMQ涓嬌鐢ㄧ殑ST綆楁硶鏄湪綰跨畻娉曪紝鎵浠ヨ繖涓畻娉曚篃鏄湪綰跨畻娉曘?br> 紺轟緥錛?/strong>ZOJ 3195 瑙i鎶ュ憡銆?br>
2 3 2 1 2 10 3 1 15 1 2 2 3 2 2 1 2 100 1 2 2 1
10 25 100 100
浜屻佸垎鏋?br> 鐢═arjan瑙e喅鐨凩CA闂錛岃緇嗙畻娉曪細LCA闂銆?/span>
涓夈佷唬鐮?/p>
#include<iostream>
2#include<list>
3using namespace std;
4struct node
5{
6 int v, d;
7 void init(int vv, int dd)
{v = vv; d = dd;};
8};
9int t, n, m, v1, v2, len;
10list<node> g[40001];
11list<node> query[40001];
12int dis[40001];
13int res[201][3];
14int parent[40001];
15bool visit[40001];
16int find(int k)
17{
18 if(parent[k] == k)
19 return k;
20 return parent[k] = find(parent[k]);
21}
22void tarjan(int u)
23{
24 if(visit[u]) return;
25 visit[u] = true;
26 parent[u] = u;
27 list<node>::iterator it = query[u].begin();
28 while(it != query[u].end())
29
{
30 if(visit[it->v])
31 res[it->d][2] = find(it->v);
32 it++;
33 }
34 it = g[u].begin();
35 while(it != g[u].end())
36
{
37 if(!visit[it->v])
38
{
39 dis[it->v] = min(dis[it->v], dis[u] + it->d);
40 tarjan(it->v);
41 parent[it->v] = u;
42 }
43 it++;
44 }
45}
46int main()
47{
48 scanf("%d", &t);
49 while(t--)
50
{
51 scanf("%d%d", &n, &m);
52 for(int i=1; i<=n; i++)
53 g[i].clear();
54 for(int i=1; i<=m; i++)
55 query[i].clear();
56 for(int i=1; i<n; i++)
57
{
58 scanf("%d%d%d", &v1, &v2, &len);
59 node n1; n1.init(v2, len);
60 g[v1].push_back(n1);
61 node n2; n2.init(v1, len);
62 g[v2].push_back(n2);
63 }
64 for(int i=1; i<=m; i++)
65
{
66 scanf("%d%d", &v1, &v2);
67 res[i][0] = v1;
68 res[i][1] = v2;
69 node n1; n1.init(v2, i);
70 query[v1].push_back(n1);
71 node n2; n2.init(v1, i);
72 query[v2].push_back(n2);
73 }
74 memset(visit, 0, sizeof(visit));
75 memset(dis, 0x7f, sizeof(dis));
76 dis[1] = 0;
77 tarjan(1);
78 for(int i=1; i<=m; i++)
79 printf("%d\n", dis[res[i][0]] + dis[res[i][1]] - 2*dis[res[i][2]]);
80 }
81}
]]>
璇存槑錛?/strong>絎?琛岀殑DFS(u)榪囩▼錛屽綋瀛樺湪浠巙寮濮嬬殑M鍙騫胯礬錛屽垯榪斿洖true錛屽茍瀹屾垚M鐨勬墿灞曪紝姝ゆ椂|M|鍔犱竴銆傚鏋滆繑鍥瀎alse錛屽垯琛ㄧず涓嶅瓨鍦∕鍙騫胯礬銆?nbsp;
紺轟緥錛?/strong>POJ 1274 瑙i鎶ュ憡銆?/span>
涓夈佹渶浼樺尮閰嶏紙KM綆楁硶錛?br> 鎻忚堪錛?/strong>
錛?錛変粠浠繪剰鍙欏舵爣l寮濮嬶紝紜畾l絳夊瓙鍥綠l錛屽茍涓斿湪Gl涓夊彇鍖歸厤M銆傝嫢M楗卞拰V1錛屽垯M鏄畬緹庡尮閰嶏紝涔熷嵆M鏄渶浼樺尮閰嶏紝綆楁硶緇堟錛?br> 錛?錛夊惁鍒欙紝榪愮敤鍖堢墮鍒╃畻娉曪紝緇堟浜嶴灞炰簬V1錛孴灞炰簬V2涓斾嬌瀵逛簬Gl錛孨(S)=T銆備護al=min{l(x)+l(y)-w(x, y) | x∈S, y∈V2-T}錛屼護l'(u)=l(u)-al濡傛灉u∈S錛沴'(u)=l(u)+al濡傛灉u∈T錛沴'(u)=l(u)錛屽叾瀹冦傜敤l'浠f浛l錛岀敤Gl'浠f浛Gl杞叆錛?錛夈?br> 瀹炵幇錛?/strong>