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:若原图不连通, ...
随机推荐
- 【转】flex中的labelFunction(combox和dataGrid)
Flex中,对于显示一个字段,只需要指定对应字段属性给labelField即可,当需要上述所需要的功能的时候就得做个转换了,在Flex的基于List的组件都有一个labelFunction方法能很简单 ...
- 一招制胜---详解分布式系统里session同步
一招制胜---详解分布式系统里session同步 几周前,有个盆友问老王,说现在有多台服务器,怎么样来解决这些服务器间的session同步问题?老王一下就来精神了,因为在n年以前,老王还在学校和几个同 ...
- 洛谷 P2690 接苹果
P2690 接苹果 题目背景 USACO 题目描述 很少有人知道奶牛爱吃苹果.农夫约翰的农场上有两棵苹果树(编号为1和2), 每一棵树上都长满了苹果.奶牛贝茜无法摘下树上的苹果,所以她只能等待苹果 从 ...
- OpenCascade Sweep Algorithm
OpenCascade Sweep Algorithm eryar@163.com Abstract. Sweeps are the objects you obtain by sweeping a ...
- 深入理解 GRE tunnel
深入理解 GRE tunnel 时间 2012-11-08 19:05:22 A Geek's Page 原文 http://wangcong.org/blog/archives/2149 主题 ...
- region实现大纲效果
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- spark ml阅读笔记
参考文档:http://www.cnblogs.com/huliangwen/p/7491797.html
- 一起talk C栗子吧(第三十四回:C语言实例--巧用溢出计算最值)
各位看官们.大家好,上一回中咱们说的是巧用移位的样例,这一回咱们说的样例是:巧用溢出计算最值. 闲话休提,言归正转.让我们一起talk C栗子吧! 大家都知道,程序中的变量都有一个取值范围,这个范围也 ...
- onblur 对象失去焦点事件
onblur 对象失去焦点事件 一.总结 1.几乎所有的控件都支持onblur事件 二.简介 onblur 事件 Event 对象 定义和用法 onblur 事件会在对象失去焦点时发生. 语法 onb ...
- linux下uboot kernel操作cpu寄存器
大多数的内核里面都有会对GPIO的操作,而且内核里面对GPIO进行配置也很方便,要什么功能就配置成什么就可以了. 还有一些寄存器是内核没有配置到的,但是我们要操作怎么办,内核里面也定义了相关的接口函数 ...