锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
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>
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}
]]>
Description
Input
Output
Sample Input
2 1 1 2 (0,1)20 (1,0)10 (0)15 (1)20 7 2 3 13 (0,0)1 (0,1)2 (0,2)5 (1,0)1 (1,2)8 (2,3)1 (2,4)7 (3,5)2 (3,6)5 (4,2)7 (4,3)5 (4,5)1 (6,0)5 (0)5 (1)2 (3)2 (4)1 (5)4
Sample Output
15 6
浜屻佸垎鏋?br> 澧炲姞鐐筺涓簊錛岀偣n+1涓簍錛屾眰鏈澶ф祦錛屼嬌鐢≒ush-Relabel綆楁硶錛屽叿浣撶畻娉曪細鏈澶ф祦闂
涓夈佷唬鐮?/strong>
Description
Input
Output
Sample Input
5 4 1 2 40 1 4 20 2 4 20 2 3 30 3 4 10
Sample Output
50