codeforces GYM 100114 J. Computer Network tarjan 树的直径 缩点

J. Computer Network

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/gym/100114

Description

The computer network of “Plunder & Flee Inc.” consists of n servers and m two-way communication links. Two servers can communicate either through a direct link, or through a chain of links, by relaying information from server to server. Current network setup enables communication for any pair of servers. The network administrator strives to maximize network reliability. Some communication links in the network were identified as critical. A failure on any critical link will split the network into disconnected segments. Company management responded to the administrator’s concerns and agreed to fund another communication link, provided that when the new link goes online the number of critical links will be minimized. Write a program that, given a network configuration, will pick a pair of servers to be connected by the new communication link. If several such pairs allow minimizing the number of critical links then any of them will be considered as a correct answer. Example. The following figure presents a network consisting of 7 servers and 7 communication links. Essential links are shown as bold lines. A new link connecting servers #1 and #7 (dotted line) can reduce the number of the critical links to only one

Input

The first line contains two space-delimited integer numbers n (the number of servers) and m (the number of communication links). The following m lines describe the communication links. Each line contains two space-delimited integers xi and yi, which define the IDs of servers connected by link number i. Servers are identified with natural numbers ranging from 1 to n.

Output

The output file should contain a single line with space-delimited integers x and y, the IDs of servers to be connected by the new link..

Sample Input

7 7 1 2 2 3 2 4 2 6 3 4 4 5 6 7

Sample Output

1 7

HINT

1 ≤ n ≤ 10 000; 1≤ m ≤ 100 000; 1 ≤ xi, yi ≤ n; xi ≠ yi.

题意

给你一个图,让你连一条边之后,使得这个图的桥最少

题解:

缩点变成一颗树之后,跑树的直径,然后把树的直径的两端连起来就行了

代码:

 #pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<stdio.h>
#include<string.h>
const int VM = ;
const int EM = ; struct Edeg
{
int to,nxt,vis;
}edge[EM<<],tree[EM<<]; int head[VM],vis[VM],thead[VM];
int dfn[VM],low[VM],stack[VM],belong[VM];
int ep,bridge,son,maxn,src,n,cnt,scc,top;
int te[VM];
int max (int a,int b)
{
return a > b ? a : b;
}
int min(int a ,int b)
{
return a > b ? b : a;
}
void addedge (int cu,int cv)
{
edge[ep].to = cv;
edge[ep].vis = ;
edge[ep].nxt = head[cu];
head[cu] = ep ++;
edge[ep].to = cu;
edge[ep].vis = ;
edge[ep].nxt = head[cv];
head[cv] = ep ++;
}
void Buildtree(int cu,int cv)
{
tree[son].to = cv;
tree[son].nxt = thead[cu];
thead[cu] = son ++;
}
void Tarjan (int u)
{
int v;
vis[u] = ;
dfn[u] = low[u] = ++cnt;
stack[top++] = u;
for (int i = head[u];i != -;i = edge[i].nxt)
{
v = edge[i].to;
if (edge[i].vis) continue; //
edge[i].vis = edge[i^].vis = ; //正向边访问过了,反向边得标志,否则两点会成一块。
if (vis[v] == )
low[u] = min(low[u],dfn[v]);
if (!vis[v])
{
Tarjan (v);
low[u] = min(low[u],low[v]);
if (low[v] > dfn[u])
bridge ++;
}
}
if (dfn[u] == low[u])
{
++scc;
do{
v = stack[--top];
vis[v] = ;
belong[v] = scc;
te[scc]=v;
}while (u != v);
}
}
void BFS(int u)
{
int que[VM+];
int front ,rear,i,v;
front = rear = ;
memset (vis,,sizeof(vis));
que[rear++] = u;
vis[u] = ;
while (front != rear)
{
u = que[front ++];
front = front % (n+);
for (i = thead[u];i != -;i = tree[i].nxt)
{
v = tree[i].to;
if (vis[v]) continue;
vis[v] = ;
que[rear++] = v;
rear = rear%(n+);
}
}
src = que[--rear];//求出其中一个端点
}
int ans2=;
void DFS (int u,int dep)
{
if(maxn<dep)
{
maxn = max(maxn,dep);
ans2 = u;
}
vis[u] = ;
for (int i = thead[u]; i != -; i = tree[i].nxt)
{
int v = tree[i].to;
if (!vis[v])
DFS (v,dep+);
}
}
void solve()
{
int u,v;
memset (vis,,sizeof(vis));
cnt = bridge = scc = top = ;
Tarjan ();
memset (thead,-,sizeof(thead));
son = ;
for (u = ;u <= n;u ++) //重构图
for (int i = head[u];i != -;i = edge[i].nxt)
{
v = edge[i].to;
if (belong[u]!=belong[v])
{
Buildtree (belong[u],belong[v]);
Buildtree (belong[v],belong[u]);
}
}
maxn = ; //最长直径
BFS(); //求树直径的一个端点
memset (vis,,sizeof(vis));
DFS(src,); //求树的最长直径
printf("%d %d\n",te[src],te[ans2]);
//printf ("%d\n",bridge-maxn);
} int main ()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
int m,u,v;
while (~scanf ("%d%d",&n,&m))
{
if (n == &&m == )
break;
memset (head,-,sizeof(head));
ep = ;
while (m --)
{
scanf ("%d%d",&u,&v);
addedge (u,v);
}
solve();
}
return ;
}
上一篇:SQL中declare申明变量


下一篇:HDU4514 湫湫系列故事——设计风景线 ——树的直径/树形dp+判环