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
Output
Sample Input
7 7 1 2 2 3 2 4 2 6 3 4 4 5 6 7
Sample Output
HINT
题意
给你一个图,让你连一条边之后,使得这个图的桥最少
题解:
缩点变成一颗树之后,跑树的直径,然后把树的直径的两端连起来就行了
代码:
#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 树的直径 缩点的更多相关文章
- codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径
题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...
- Codeforces Gym 100114 J. Computer Network
Description 给出一个图,求添加一条边使得添加后的图的桥(割边)最少. Sol Tarjan. 一遍Tarjan求割边. 我们发现连接的两个点一定是这两个点之间的路径上的桥最多,然后就可以贪 ...
- [Codeforces 555E]Case of Computer Network(Tarjan求边-双连通分量+树上差分)
[Codeforces 555E]Case of Computer Network(Tarjan求边-双连通分量+树上差分) 题面 给出一个无向图,以及q条有向路径.问是否存在一种给边定向的方案,使得 ...
- [J]computer network tarjan边双联通分量+树的直径
https://odzkskevi.qnssl.com/b660f16d70db1969261cd8b11235ec99?v=1537580031 [2012-2013 ACM Central Reg ...
- 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 ...
- Codeforces GYM 100876 J - Buying roads 题解
Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...
- Codeforces Gym 100114 H. Milestones 离线树状数组
H. Milestones Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descripti ...
- Codeforces GYM 100114 D. Selection 线段树维护DP
D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descriptio ...
- Codeforces GYM 100114 B. Island 水题
B. Island Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Description O ...
随机推荐
- mybatis+spring+struts2框架整合
近期公司要开发新的项目,要用struts2+mybatis+spring框架,所以学习了下,来自己的博客发表下,希望能给大家带来帮助!下边我把我的myschool开发的源代码以及数据库贴出来! 开 ...
- #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)宏的运行机理:1. ( (TYPE *)0 ) 将零转型为TY ...
- 【转】如何用WINDBG分析64位机上32位程序的DUMP文件
将dump拖入到windbg中后,在command输入栏输入 .load wow64exts 回车 !sw 回车,就将windbg的dump,从64位模式切换到了32位模式,否则看到的call sta ...
- LightOJ 1038-Race to 1 Again(概率dp)
题意: 给你一个数n每一步这个数可以变为他的因子,直到这个数变为1,求n变到1的期望步数. 分析: dp[i],表示i变为1的期望步数,dp[1]=0,dp[n]是答案. dp[i]=sum(dp[j ...
- Apache OFBiz 学习笔记 之 实体引擎
1.概述 entity engine和常见的ORM有一点很大的不同,他的mapping object只有一个 GenericEntity,称它的entity engine 为adaptive ...
- MorningSale 使用帮助
待添加 http://121.37.42.173:8080/morningsale
- 【和我一起学python吧】python的一些推荐
看到未名的几篇帖子 使我想起了和python的一些经历,于是写了一篇咚咚. 1 书籍: python的syntax足够简单,semantics也不复杂,不怎么会使人混乱,一般来说看自带的文档足够可以学 ...
- QT5.3+VS2013+QCustomPlot+QwtPlot+QwtPlot3D使用环境配置
VS安装QT后运行环境所需配置 安装好QT和QT在VS下的插件之后: 1.打开VS,找到QT5→QT Option,如下: 2.配置电脑环境变量,在系统变量→Path下增加QT的动态库所在文件夹,也就 ...
- Tkinter教程之Event篇(3)
本文转载自:http://blog.csdn.net/jcodeer/article/details/1823550 '''Tkinter教程之Event篇(3)''''''11.两个事件同时绑定到一 ...
- vbox磁盘空间扩容
前提:将虚拟机真正关机,不能在仅状态保存的场合做磁盘扩容. 步骤1.获取需要增加容量的映像的uuid 在vbox的安装目录下使用命令行:VBoxManage list hdds 得到结果如下: UUI ...