HDOJ 5409 CRB and Graph 无向图缩块
无向图缩块后,以n所在的块为根节点,dp找每块中的最大值.
对于每一个桥的答案为两块中的较小的最大值和较小的最大值加1
CRB and Graph
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 113 Accepted Submission(s): 41
and M edges
is given to CRB.
A pair of vertices (u, v)
(u < v)
is called critical for edge e if
and only if u and v become
disconnected by removing e.
CRB’s task is to find a critical pair for each of M edges.
Help him!
indicating the number of test cases. For each test case:
The first line contains two integers N, M denoting
the number of vertices and the number of edges.
Each of the next M lines
contains a pair of integers a and b,
denoting an undirected edge between a and b.
1 ≤ T ≤
12
1 ≤ N, M ≤ 105
1 ≤ a, b ≤ N
All given graphs are connected.
There are neither multiple edges nor self loops, i.e. the graph is simple.
of them should contain two integers u and v,
denoting a critical pair (u, v)
for the i-th
edge in the input.
If no critical pair exists, output "0 0" for that edge.
If multiple critical pairs exist, output the pair with largest u.
If still ambiguous, output the pair with smallest v.
2
3 2
3 1
2 3
3 3
1 2
2 3
3 1
1 2
2 3
0 0
0 0
0 0
/* ***********************************************
Author :CKboss
Created Time :2015年08月22日 星期六 10时24分13秒
File Name :HDOJ5409.cpp
************************************************ */ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map> using namespace std; const int maxn=100100; typedef long long int LL;
typedef pair<int,int> pII; struct Edge
{
int from,to,next,id;
}edge[maxn*2]; int Adj[maxn],Size,n,m; void init()
{
Size=0; memset(Adj,-1,sizeof(Adj));
} void Add_Edge(int u,int v,int id)
{
edge[Size].from=u;
edge[Size].id=id;
edge[Size].to=v;
edge[Size].next=Adj[u];
Adj[u]=Size++;
} int Low[maxn],DFN[maxn],Stack[maxn],Belong[maxn];
int Index,top,scc;
bool Instack[maxn],vis[maxn],ve[maxn*2]; void Tarjan(int u,int fa)
{
int v;
Low[u]=DFN[u]=++Index;
Stack[top++]=u;
Instack[u]=true; for(int i=Adj[u];~i;i=edge[i].next)
{
v=edge[i].to;
if(v==fa&&ve[edge[i].id]) continue;
ve[edge[i].id]=true;
if(!DFN[v])
{
Tarjan(v,u);
Low[u]=min(Low[u],Low[v]);
}
else
{
Low[u]=min(Low[u],DFN[v]);
}
}
if(Low[u]==DFN[u])
{
scc++;
do
{
v=Stack[--top];
Belong[v]=scc;
Instack[v]=false;
}while(v!=u);
}
} void scc_solve()
{
memset(DFN,0,sizeof(DFN));
memset(Instack,0,sizeof(Instack)); Index=scc=top=0;
memset(ve,0,sizeof(ve)); for(int i=1;i<=n;i++)
{
if(!DFN[i]) Tarjan(i,i);
}
} int value[maxn];
vector<pII> G[maxn];
int ans[maxn][2];
int bian[maxn][2];
int MX[maxn]; void dfs(int u,int fa)
{
MX[u]=value[u];
for(int i=0,sz=G[u].size();i<sz;i++)
{
int v=G[u][i].first;
if(v==fa) continue;
dfs(v,u);
MX[u]=max(MX[u],MX[v]);
}
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); int T_T;
scanf("%d",&T_T);
while(T_T--)
{
scanf("%d%d",&n,&m);
init();
for(int i=0;i<m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
bian[i][0]=a; bian[i][1]=b;
Add_Edge(a,b,i); Add_Edge(b,a,i);
}
scc_solve(); /***************REBUILD**********************/ memset(value,0,sizeof(value));
memset(ans,0,sizeof(ans));
int root=0; for(int i=1;i<=n;i++)
{
G[i].clear();
int b=Belong[i];
value[b]=max(value[b],i);
if(value[b]==n) root=b;
} //for(int i=1;i<=scc;i++) cout<<i<<" value: "<<value[i]<<endl; for(int i=0;i<m;i++)
{
int u=Belong[bian[i][0]];
int v=Belong[bian[i][1]];
if(u==v) continue;
G[u].push_back(make_pair(v,i)); G[v].push_back(make_pair(u,i));
} dfs(root,root); //for(int i=1;i<=scc;i++) { cout<<i<<" mx: "<<MX[i]<<endl; } for(int i=0;i<m;i++)
{
int u=Belong[bian[i][0]];
int v=Belong[bian[i][1]];
if(u==v)
{
puts("0 0"); continue;
}
else
{
int mx=min(MX[u],MX[v]);
printf("%d %d\n",mx,mx+1);
}
}
} return 0;
}
HDOJ 5409 CRB and Graph 无向图缩块的更多相关文章
- HDU 5409 CRB and Graph 【点双连通+DFS】
<题目链接> 题目大意: 给你一个连通的无向图,问你删除每一条边后,是否能够出现一对(u,v),使得u,v不连通,且u<v,如果有多对u,v,则输出尽量大的u,和尽量小的v. 解题分 ...
- hdu 5409 CRB and Graph(边双联通分量)
题意: 给一个图一些边,保证图连通 问对于每条边,如果去除该边后使得图中一些点不连通.设这些点(u,v),要求使u尽量小,v尽量大,输出这样的(u,v).否则输出0 0. #include <b ...
- hdu-4612(无向图缩点+树的直径)
题意:给你n个点和m条边的无向图,问你如果多加一条边的话,那么这个图最少的桥是什么 解题思路:无向图缩点和树的直径,用并查集缩点: #include<iostream> #include& ...
- POJ 3177 (Redundant Paths) —— (有重边,边双联通,无向图缩点)
做到这里以后,总算是觉得tarjan算法已经有点入门了. 这题的题意是,给出若干个点和若干条边连接他们,在这个无向图中,问至少增加多少条边可以使得这个图变成边双联通图(即任意两点间都有至少两条没有重复 ...
- Codeforces Round #143 (Div. 2) E. Cactus 无向图缩环+LCA
E. Cactus A connected undirected graph is called a vertex cactus, if each vertex of this graph bel ...
- [LeetCode] Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- [LeetCode] Clone Graph 无向图的复制
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...
- poj-3177(无向图缩点)
题意:给你n个点,m条边的无向联通图,问你最少增加几条边,使得这个图每对点至少有两条路径 解题思路:考虑每个环内的点必定有>=2条路径,所以先把这个无向图中的环去掉,用并查集缩环,然后剩下的图一 ...
- hdu 4738 无向图缩点断桥 // 细节坑题
Caocao's Bridges 题意:给个无向图,求出边权最小的桥. 一看,直接缩点,若无桥,输出-1,有桥,遍历下边,更新最小..分分钟搞定,以为IA的..一交wa... 坑点:1:若原图不连通, ...
随机推荐
- 把书《CUDA By Example an Introduction to General Purpose GPU Programming》读薄
鉴于自己的毕设需要使用GPU CUDA这项技术,想找一本入门的教材,选择了Jason Sanders等所著的书<CUDA By Example an Introduction to Genera ...
- jQuery获取区间随机数
1.自定义函数 function getRandom(min,max){ //x上限,y下限 var x = max; var y = min; if(x<y){ ...
- WinSocket 编程
套接字 套接字指通信双方在通信时所使用的通信点(Endpoint),通信的双方通过通信点来交换信息和数据.不同类型的通信会使用不同的类型通信点,比如对于电话通信而言,通信点就是电话号码和分机号码的组合 ...
- JBoss配置连接池
什么是数据库连接池? 配置连接池为的是解决效率问题.由于每创建一个连接都是非常耗时的,有了连接池,就能够提前放一些连接进去.以后我们再用连接就去连接池里面取而不是每次都创建.可是我们知道连接池是有上限 ...
- Android性能优化之提高ListView性能的技巧
ListView优化一直是一个老生常谈的问题.无论是面试还是寻常的开发中,ListView永远不会被忽略掉,那么这篇文章我们来看看怎样最大化的优化ListView的性能. 1.在adapter中的ge ...
- [Python] Understand Mutable vs. Immutable objects in Python
In this lesson, you will learn what mutable and immutable objects are, and the difference between th ...
- jquery14 on() trigger() : 事件操作的相关方法
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- js --- for in 和 for of
前言:for of是ES6新增的循环方法.前面已经说到了 [JavaScript]for.forEach .for in.each循环详解.那for of又是怎么使用的? 一.使用例子 使用例子(一) ...
- 扩展: 简介pyinstaller: py文件压缩成exe文件
写在开始: 后缀 .py 的文件, 必须在装有python解释器, 和相对应的模块下才能运行. 对于装13来说,比较不方便. 如果人家电脑上也装上了python解释器, 说不好一眼就看穿了 自己写的 ...
- css3 transform:scale(x)实现字体的缩放:
css3 transform:scale(x)字体的缩放: transform:scale(x),针对于整体的缩放,缩放的整体包括宽,高,背景.这自然对于内联元素就无法使用此属性,最好使用无属性的sp ...