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. Linux系统性能监控

    系统的性能指标主要包括CPU.内存.磁盘I/O.网络几个方面. 1. CPU性能 (1)利用vmstat命令监控系统CPU 该命令可以显示关于系统各种资源之间相关性能的简要信息,这里我们主要用它来看C ...

  2. HDU 5119 Happy Matt Friends

    Happy Matt Friends Time Limit: 6000/6000 MS (Java/Others) Memory Limit: 510000/510000 K (Java/Others ...

  3. 打印Ibatis最终的SQL语句

    在项目开发时都大家都希望将SQL在后台打印出来,以帮助开发以及后续的bug修改.如果用JDBC那么可以方便的打印,可使用ibatis就不知道怎么办了,最近在网上找了一段log4j的配置可以很保姆的处理 ...

  4. VS2010下 LibVLC开发环境搭建

    LibVLC环境的搭建  最近又 LIBVLC 做一个视频播放器,封装成ActiveX控件,之前做过一个基于OpenCV的播放器(只解码视频,音频不用,OpenCV也没有解码音频的功能). 到目前位置 ...

  5. springmvc里面的中文乱码问题

    如果是以get方法提交的表单,则可以在comcat服务器的server.xml文件里面设置 <Connector connectionTimeout="20000" port ...

  6. LeetCode题解——Palindrome Number

    题目: 判断一个数字是不是回文数字,即最高位与最低位相同,次高位与次低位相同,... 解法: 求出数字的位数,然后依次求商和求余判断是否相等. 代码: class Solution { public: ...

  7. 基于Python的Grib数据可视化

    http://www.cnblogs.com/kallan/p/5160017.html

  8. DOM笔记(三):Element接口和HTMLElement接口

    一.Element接口 Element接口表示一个元素,该接口扩展自Node接口,自然继承了Node接口的属性和方法,也有一套针对元素的属性和方法. Element接口常见的属性比较少,常用的就是一个 ...

  9. Kindle Paperwhite 2使用体验

    博客开通后一懒就扔下了几十天,着实自惭.鉴于是第一篇,先说点题外话. 一转眼读研的生活已经过去一年有余.曾经的同学已经在职场拼搏,同龄人的生活状态也自然地带给自己一份紧迫感:不敢再贪恋校园生活的安逸, ...

  10. CentOS安装XRDP实现远程桌面访问

    1.配置环境: yum install gcc pam-devel openssl-devel -y 2.进入指定目录下载并解压xrdp: 先安装 wget; sudo yum -y install ...