锘??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>
#include<iostream>
#include<list>
using namespace std;
int n, r;
list<int> g[1001];
int num, lab[1001], low[1001];
list<pair<int, int> > edge;
int degree[1001];
int parent[1001], rank[1001];
void init_set()

{
for(int i=1; i<1001; i++)
{
parent[i] = i;
rank[i] = 1;
}
}
int find(int k)

{
if(parent[k] == k)
return k;
else
return parent[k] = find(parent[k]);
}
void union_set(int u, int v)

{
int pu = find(u), pv = find(v);
if(rank[pu] <= rank[pv])
{
parent[pu] = pv;
rank[pv] += pu;
}
else
{
parent[pv] = pu;
rank[pu] += pv;
}
}
void dfs(int u, int p)

{
lab[u] = low[u] = num++;
list<int>::iterator it;
for(it = g[u].begin(); it != g[u].end(); it++)
{
int v = *it;
if(lab[v] == 0)
{
dfs(v, u);
low[u] = min(low[u], low[v]);
if(low[v] > lab[u])
edge.push_back(make_pair(u, v));
else
union_set(u, v); //u涓巚鑳借繘琛岀緝鐐?/span>
}
else if(v != p)
low[u] = min(low[u], lab[v]);
}
}
int main()

{
scanf("%d%d", &n, &r);
for(int i=1; i<=n; i++)
g[i].clear();
for(int i=1; i<=r; i++)
{
int v1, v2;
scanf("%d%d", &v1, &v2);
g[v1].push_back(v2);
g[v2].push_back(v1);
}
memset(lab, 0, sizeof lab);
memset(low, 0x7f, sizeof low);
num = 1;
init_set();
dfs(1, 0);
memset(degree, 0, sizeof degree);
list<pair<int, int> >::iterator it;
int res = 0;
for(it = edge.begin(); it != edge.end(); it++)
{
int u = it->first, v = it->second;
degree[find(u)]++;
if(degree[find(u)] == 1)
res++;
else if(degree[find(u)] == 2)
res--;
degree[find(v)]++;
if(degree[find(v)] == 1)
res++;
else if(degree[find(v)] == 2)
res--;
}
printf("%d\n", (res+1) / 2);
}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>
#include<iostream>
#include<list>
using namespace std;
int t;
int v1, v2;
list<int> g[1001];
bool flag;
int root;
int counter;
bool spf[1001];
int low[1001], lab[1001];
bool visit[1001];
void dfs(int u, int fa)

{
low[u] = lab[u] = counter++;
list<int>::iterator it;
int counter = 0;
for(it = g[u].begin(); it != g[u].end(); it++)
{
int v = *it;
if(!lab[v])
{
counter++;
dfs(v, u);
low[u] = min(low[u], low[v]);
if((u==root && counter>=2) || (u!=root && low[v]>=lab[u]))
spf[u] = flag = true;
}
else if(v != fa)
low[u] = min(low[u], lab[v]);
}
}
void find(int u)

{
visit[u] = true;
list<int>::iterator it;
for(it = g[u].begin(); it != g[u].end(); it++)
if(!visit[*it])
find(*it);
}
int main()

{
t = 1;
while(1)
{
scanf("%d", &v1);
if(v1 == 0) break;
for(int i=1; i<=1000; i++)
g[i].clear();
memset(spf, 0, sizeof spf);
memset(low, 0, sizeof low);
memset(lab, 0, sizeof lab);
while(v1 != 0)
{
scanf("%d", &v2);
g[v1].push_back(v2);
g[v2].push_back(v1);
root = v1;
scanf("%d", &v1);
}
counter = 1;
flag = false;
dfs(root, -1);
printf("Network #%d\n", t++);
if(flag)
{
for(int i=1; i<=1000; i++)
{
if(!spf[i]) continue;
int cnt = 0;
memset(visit, 0, sizeof visit);
visit[i] = true;
list<int>::iterator it;
for(it = g[i].begin(); it != g[i].end(); it++)
if(!visit[*it])
{
cnt++;
find(*it);
}
printf(" SPF node %d leaves %d subnets\n", i, cnt);
}
}
else
printf(" No SPF nodes\n");
printf("\n");
}
}
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>
3
using namespace std;
4
struct node
5

{
6
int v, d;
7
void init(int vv, int dd)
{v = vv; d = dd;};
8
};
9
int t, n, m, v1, v2, len;
10
list<node> g[40001];
11
list<node> query[40001];
12
int dis[40001];
13
int res[201][3];
14
int parent[40001];
15
bool visit[40001];
16
int find(int k)
17

{
18
if(parent[k] == k)
19
return k;
20
return parent[k] = find(parent[k]);
21
}
22
void 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
}
46
int 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>
#include<iostream>
using namespace std;
#define MAXN 202
int s, t;
int n, np, nc, m;
char str[50];
int c[MAXN][MAXN];
int f[MAXN][MAXN];
int e[MAXN];
int h[MAXN];
void push(int u, int v)

{
int d = min(e[u], c[u][v] - f[u][v]);
f[u][v] += d;
f[v][u] = -f[u][v];
e[u] -= d;
e[v] += d;
}
bool relabel(int u)

{
int mh = INT_MAX;
for(int i=0; i<n+2; i++)
{
if(c[u][i] > f[u][i])
mh = min(mh, h[i]);
}
if(mh == INT_MAX)
return false; //孌嬬暀緗戠粶涓棤浠巙鍑哄彂鐨勮礬
h[u] = mh + 1;
for(int i=0; i<n+2; i++)
{
if(e[u] == 0) //宸叉棤浣欐祦錛屼笉闇鍐嶆push
break;
if(h[i] == mh && c[u][i] > f[u][i]) //push鐨勬潯浠?/span>
push(u, i);
}
return true;
}
void init_preflow()

{
memset(h, 0, sizeof(h));
memset(e, 0, sizeof(e));
h[s] = n+2;
for(int i=0; i<n+2; i++)
{
if(c[s][i] == 0)
continue;
f[s][i] = c[s][i];
f[i][s] = -f[s][i];
e[i] = c[s][i];
e[s] -= c[s][i];
}
}
void push_relabel()

{
init_preflow();
bool flag = true; //琛ㄧず鏄惁榪樻湁relabel鎿嶄綔
while(flag)
{
flag = false;
for(int i=0; i<n; i++)
if(e[i] > 0)
flag = flag || relabel(i);
}
}
int main()

{
while(scanf("%d%d%d%d", &n, &np, &nc, &m) != EOF)
{
s = n; t = n+1;
memset(c, 0, sizeof(c));
memset(f, 0, sizeof(f));
while(m--)
{
scanf("%s", &str);
int u=0, v=0, z=0;
sscanf(str, "(%d,%d)%d", &u, &v, &z);
c[u][v] = z;
}
for(int i=0; i<np+nc; i++)
{
scanf("%s", &str);
int u=0, z=0;
sscanf(str, "(%d)%d", &u, &z);
if(i < np)
c[s][u] = z;
else if(i >= np && i < np + nc)
c[u][t] = z;
}
push_relabel();
printf("%d\n", e[t]);
}
}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
#include<iostream>
#include<queue>
using namespace std;
#define MAXM 201
int m, n;
int si, ei, ci;
int c[MAXM][MAXM];
int f[MAXM][MAXM];
int cf[MAXM][MAXM];
bool visit[MAXM];
int p[MAXM];
struct node

{
int v, cf;
void set(int vv, int ccf)
{
v = vv; cf = ccf;
}
};
int main()

{
while(scanf("%d%d", &n, &m) != EOF)
{
memset(c, 0, sizeof(c));
memset(f, 0, sizeof(f));
memset(cf, 0, sizeof(cf));
while(n--)
{
scanf("%d%d%d", &si, &ei, &ci);
c[si][ei] += ci;
cf[si][ei] = c[si][ei];
}
bool flag = true; //鐢ㄤ簬琛ㄧず鏄惁鎵懼埌澧炲箍璺?/span>
while(flag)
{
flag = false;
memset(visit, 0, sizeof(visit));
queue<node> q;
node temp;
temp.set(1, INT_MAX);
p[1] = 0;
q.push(temp); visit[1] = true;
while(!q.empty()) //騫垮害浼樺厛鎼滅儲
{
node temp = q.front(); q.pop();
for(int i=1; i<=m; i++)
{
if(temp.v == i || visit[i] || cf[temp.v][i] == 0)
continue;
node newNode;
newNode.set(i, min(temp.cf, cf[temp.v][i]));
p[i] = temp.v;
q.push(newNode);
visit[i] = true;
if(i == m)
{
flag = true; //鎵懼埌澧炲箍璺?/span>
break;
}
}
if(flag)
break;
}
if(flag)
{
int mincf = q.back().cf;
int v1 = p[m], v2 = m;
while(v1 != 0)
{
f[v1][v2] += mincf; //淇敼嫻?/span>
f[v2][v1] = -f[v1][v2];
cf[v1][v2] = c[v1][v2] - f[v1][v2]; //淇敼孌嬬暀瀹歸噺
cf[v2][v1] = c[v2][v1] - f[v2][v1];
v2 = v1;
v1 = p[v1];
}
}
}
int res = 0;
for(int i=2; i<=m; i++) //璁$畻鏈澶ф祦
res += f[1][i];
printf("%d\n", res);
}
}