题目链接:

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.
 

题意:

给你一个无相图,加一条边,使得桥的个数最小化。

题解:

对原图缩点得到一颗树,求树的直径,把直径两端点连起来。

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<stack>
using namespace std; const int maxn = + ; struct Edge {
int u,v,type;
Edge(int u,int v,int type=) : u(u),v(v),type(type) {}
}; vector<int> G[maxn];
vector<Edge> egs;
int n, m; void addEdge(int u, int v) {
egs.push_back(Edge(u,v));
G[u].push_back(egs.size() - );
} int pre[maxn], lowlink[maxn], sccno[maxn], dfs_clock, scc_cnt;
stack<int> S; //无相图的边双联通分量 tarjan
void dfs(int u) {
pre[u] = lowlink[u] = ++dfs_clock;
S.push(u);
for (int i = ; i < G[u].size(); i++){
Edge& e = egs[G[u][i]];
if (e.type == ) continue;
egs[G[u][i] ^ ].type ^= ;//针对无相图,要标记反向边
if (!pre[e.v]) {
dfs(e.v);
lowlink[u] = min(lowlink[u], lowlink[e.v]);
}
else if (!sccno[e.v]) {
lowlink[u] = min(lowlink[u],pre[e.v]);
}
}
if (lowlink[u] == pre[u]) {
scc_cnt++;
for (;;) {
int x = S.top(); S.pop();
sccno[x] = scc_cnt;
if (x == u) break;
}
}
} void find_scc() {
dfs_clock = scc_cnt = ;
memset(sccno, , sizeof(sccno));
memset(pre, , sizeof(pre));
for (int i = ; i < n; i++) if (!pre[i]) dfs(i);
} vector<int> T[maxn];
//缩点得到的桥构成的树
void build_tree() {
for (int i = ; i < egs.size(); i++) {
Edge& e = egs[i];
if (e.type) continue;
if (sccno[e.u] != sccno[e.v]) {
T[sccno[e.u]].push_back(sccno[e.v]);
T[sccno[e.v]].push_back(sccno[e.u]);
}
}
//for (int i = 1; i <= scc_cnt; i++) {
// printf("%d:", i);
// for (int j = 0; j < T[i].size(); j++) {
// int v = T[i][j];
// printf("%d ", v);
// }
// printf("\n");
//}
} //求树的直径
void dfs2(int u,int fa,int d,int &dep,int &res) {
if (dep < d) dep = d, res = u;
for (int i = ; i < T[u].size(); i++) {
int v = T[u][i];
if (v == fa) continue;
dfs2(v, u, d + , dep, res);
}
} void init() {
for (int i = ; i < n; i++) G[i].clear(), T[i].clear();
T[n].clear();
} int main() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d%d", &n, &m);
init();
for (int i = ; i < m; i++) {
int u, v;
scanf("%d%d", &u, &v),u--,v--;
addEdge(u, v);
addEdge(v, u);
}
find_scc();
build_tree();
int u, v, dep;
dep=-,dfs2(, -, , dep, u);
dep=-,dfs2(u, -, , dep, v);
int au, av;
for (int i = ; i < n; i++) {
//printf("sccno[%d]:%d\n", i,sccno[i]);
if (sccno[i] == u) au = i + ;
if (sccno[i] == v) av = i + ;
}
printf("%d %d\n", au, av);
return ;
}

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

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

    J. Computer Network Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Des ...

  2. Codeforces Gym 100114 J. Computer Network

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

  3. 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 ...

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

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

  5. Codeforces GYM 100876 J - Buying roads 题解

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

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

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

  7. Codeforces GYM 100114 B. Island 水题

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

  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 C. Sequence 打表

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

随机推荐

  1. Windows 10 (or 8)Chrome 观看视频发生flash不能加载,即"could't load plugins"原因之一

    最近一直如题,不能看视频,后来发现从一个已经使用管理员权限打开的应用转到Chrome就可以加载flash,而从桌面打开Chrome就加载不了. 今天再次查找信息,从Ubuntu下Chrome不能加载f ...

  2. 20141128—JavaScript对象

    JavaScript 中的所有事物都是对象:字符串.数值.数组.函数... String 对象的 length 属性来获得字符串的长度: var message="Hello World!& ...

  3. 数据挖掘:Weka代码学习

    在Eclipse中配置Weka,在Eclipse中新建一个Java Project,然后在Eclipse的Resource目录中,在新new的Project上右键选择Build Path中选择add ...

  4. 【学习】Windows PE文件学习(一:导出表)

    今天做了一个读取PE文件导出表的小程序,用来学习. 参考了<Windows PE权威指南>一书. 首先, PE文件的全称是Portable Executable,可移植的可执行的文件,常见 ...

  5. 四位数码管SH5461AS的问题,arduino学习实测.

    arduino入门教程到第16课遇到些问题.效果一直是混乱的状态. 琢磨了半天发现一些问题,和大家分享下 1)接线图,原图没有问题,只是比较含糊,线比较多不好看. 我用红色数字标示数码管的12个脚,并 ...

  6. 《NFS文件共享服务的搭建》RHEL

    首先要清楚一点:NFS服务的共享是建立在linux和linux之间的. 配置任何服务之前我们要做的2件事: iptables  -F setenforce 0    NFS服务内核防火墙影响不大,主要 ...

  7. SQL server数据库内置账户SA登录设置

    SQL server数据库内置账户SA登录不了   设置SQL Server数据库给sa设置密码的时候  提示18456 解决步骤:   第二步:右击sa,选择属性: 第三步:点击状态选项卡:勾选授予 ...

  8. Android:WebView中对图片注册上下文菜单

    前言 今天一朋友问我一个问题,就是如何在WebView控件中的图片增加上下文菜单,以便增加保存图片等功能.今天就给他简单做了一个演示Demo,现写下来,给有相同问题的朋友提供些许思路吧. 概要实现 其 ...

  9. 删除select中所有option选项jquery代码

    select中所有option选项如何删除,本文使用jquery简单实现下,有此需求的朋友可以参考下,希望对大家有所帮助. 这样写 复制代码代码如下: <select id="sear ...

  10. Swift初步介绍

    Swift是本届WWDC大会苹果推出的一门新开发语言,开发者网站上已经放出了这门新语言的介绍.教程和手册,如果手里有一台iOS设备的话,通过苹果的iBooks应用,从它的官方书店里搜索Swift,可以 ...