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. JavaScript基础篇最全

    本章内容: 简介 定义 注释 引入文件 变量 运算符 算术运算符 比较运算符 逻辑运算符 数据类型 数字 字符串 布尔类型 数组 Math 语句 条件语句(if.switch) 循环语句(for.fo ...

  2. hdu 2818 Building Block(加权并查集)2009 Multi-University Training Contest 1

    题意: 一共有30000个箱子,刚开始时都是分开放置的.接下来会有两种操作: 1. M x y,表示把x箱子所在的一摞放到y箱子那一摞上. 2. C y,表示询问y下方有多少个箱子. 输入: 首行输入 ...

  3. [转] C# 中的static静态变量

    logitechyan原文关于C#中static静态变量 C#静态变量使用static 修饰符进行声明,在类被实例化时创建,通过类进行访问不带有 static 修饰符声明的变量称做非静态变量,在对象被 ...

  4. java制作证书的工具keytool用法

    一.keytool的概念 keytool 是个密钥和证书管理工具.它使用户能够管理自己的公钥/私钥对及相关证书,用于(通过数字签名)自我认证(用户向别的用户/服务认证自己)或数据完整性以及认证服务.在 ...

  5. IOS 给图片添加水印 打印文字

    1.加文字   -(UIImage *)addText:(UIImage *)img text:(NSString *)text1  {           //get image width and ...

  6. 网页抓取:PHP实现网页爬虫方式小结

    来源:http://www.ido321.com/1158.html 抓取某一个网页中的内容,需要对DOM树进行解析,找到指定节点后,再抓取我们需要的内容,过程有点繁琐.LZ总结了几种常用的.易于实现 ...

  7. 高精度+搜索+质数 BZOJ1225 [HNOI2001] 求正整数

    // 高精度+搜索+质数 BZOJ1225 [HNOI2001] 求正整数 // 思路: // http://blog.csdn.net/huzecong/article/details/847868 ...

  8. hdfs 数据块重分布 sbin/start-balancer.sh -threshold

    数据块重分布sbin/start-balancer.sh -threshold <percentage of disk capacity>percentage of disk capa ...

  9. PyBayes的安装和使用

    PyBayes 主页 文档 PyBayes is an object-oriented Python library for recursive Bayesian estimation (Bayesi ...

  10. Java8新特性 1——利用流和Lambda表达式操作集合

    Java8中可以用简洁的代码来操作集合,比如List,Map,他们的实现ArrayList.以此来实现Java8的充分利用CPU的目标. 流和Lambda表达式都是Java8中的新特性.流可以实现对集 ...