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

codeforces GYM 100114 J. Computer Network tarjan 树的直径 缩点的更多相关文章

  1. codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径

    题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...

  2. Codeforces Gym 100114 J. Computer Network

    Description 给出一个图,求添加一条边使得添加后的图的桥(割边)最少. Sol Tarjan. 一遍Tarjan求割边. 我们发现连接的两个点一定是这两个点之间的路径上的桥最多,然后就可以贪 ...

  3. [Codeforces 555E]Case of Computer Network(Tarjan求边-双连通分量+树上差分)

    [Codeforces 555E]Case of Computer Network(Tarjan求边-双连通分量+树上差分) 题面 给出一个无向图,以及q条有向路径.问是否存在一种给边定向的方案,使得 ...

  4. [J]computer network tarjan边双联通分量+树的直径

    https://odzkskevi.qnssl.com/b660f16d70db1969261cd8b11235ec99?v=1537580031 [2012-2013 ACM Central Reg ...

  5. Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】

     2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...

  6. Codeforces GYM 100876 J - Buying roads 题解

    Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...

  7. Codeforces Gym 100114 H. Milestones 离线树状数组

    H. Milestones Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descripti ...

  8. Codeforces GYM 100114 D. Selection 线段树维护DP

    D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descriptio ...

  9. Codeforces GYM 100114 B. Island 水题

    B. Island Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Description O ...

随机推荐

  1. js如何判断是否在iframe中及防止网页被别站用 iframe嵌套 (Load denied by X-Frame-Options)

    1. js如何判断是否在iframe中 //方式一 if (self.frameElement && self.frameElement.tagName == "IFRAME ...

  2. util-C# 复杂条件查询(sql 复杂条件查询)查询解决方案

    ylbtech-funcation-util:  C# 复杂条件查询(sql 复杂条件查询)查询解决方案 C# 复杂条件查询(sql 复杂条件查询)查询解决方案 1.A,Ylbtech.Model返回 ...

  3. Mybatis拦截器介绍

    拦截器的一个作用就是我们可以拦截某些方法的调用,我们可以选择在这些被拦截的方法执行前后加上某些逻辑,也可以在执行这些被拦截的方法时执行自己的逻辑而不再执行被拦截的方法.Mybatis拦截器设计的一个初 ...

  4. (原创)LAMP教程4-用VirtualBox安装64位的centos6.4

    (原创)LAMP教程4-用VirtualBox安装64位的centos6.4 好的,今天就要开始正式的讲一些有营养的东西了,是的,没有错就是讲如何用VirtualBox安装64位的centos6.4 ...

  5. 【C++对象模型】函数返回C++对象的问题

    在深入C++对象模型中,对于形如 CObj obj1 = Get(obj2); 的形式,编译器会在将其改变为如下 Get(obj, CObj&  obj1); 将赋值操作符左边的变量作为函数的 ...

  6. 根据关键词获取进程ID然后杀掉进程

    例如需要杀掉监听进程,如下: [oracle@kel ~]$ ps -ef|grep lsnr oracle 4973 1 1 19:40 ? 00:00:00 /home/oracle/produc ...

  7. Python十分钟入门

    [简介] Python是一种动态解释型的编程语言.Python可以在Windows.UNIX.MAC等多种操作系统上使用,也可以在Java..NET开发平台上使用. [特点] 1. Python使用C ...

  8. 博客测试:博客系统i94web beta1.0 请求测试

    最近博客没怎么更新了,因为一直在撸代码,自己写了一个小小的博客系统:i94web,匆忙发布beta1.0,请求各位测试各种漏洞. 先看几张截图. 首页: 边栏: 文章页: 后台发布: 测试地址:htt ...

  9. C字符串和C++中string的区别 &amp;&amp;&amp;&amp;C++中int型与string型互相转换

    在C++中则把字符串封装成了一种数据类型string,可以直接声明变量并进行赋值等字符串操作.以下是C字符串和C++中string的区别:   C字符串 string对象(C++) 所需的头文件名称 ...

  10. php pdo(二)

    定义:PDO(PHP Data Object)是PHP5才支持的扩展,它为PHP访问各种数据库定义了一个轻量级的.一致性的接口. PDO是PHP5中的一个重大功能,PHP6中将只默认使用PDO来处理数 ...