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:若原图不连通, ...
随机推荐
- wc---计算字数。
Linux wc命令用于计算字数. 利用wc指令我们可以计算文件的Byte数.字数.或是列数,若不指定文件名称.或是所给予的文件名为"-",则wc指令会从标准输入设备读取数据. 语 ...
- caioj 1066 动态规划入门(一维一边推4:护卫队)(分组型dp总结)
很容易想到f[i]为前i项的最优价值,但是我一直在纠结如果重量满了该怎么办. 正解有点枚举的味道. 就是枚举当前这辆车与这辆车以前的组合一组,在能组的里面取最优的. 然后要记得初始化,因为有min,所 ...
- Swift学习笔记(10)--枚举
1.定义语法: enum SomeEnumeration { // enumeration definition goes here } 2.使用 enum CompassPoint { case N ...
- Maven学习总结(12)——eclipse中构建多模块maven项目
摘要:本文要用Maven来构建一个多模块的web项目 项目结构如下: system-parent |----pom.xml |----system-domain ...
- ExtAspNet依据Grid导出Excel
protected void Button1_Click(object sender, EventArgs e) { Response.ClearContent(); Response.AddHead ...
- Excel的版本
https://en.wikipedia.org/wiki/Microsoft_Excel 取自维基百科,需要特别注意的是,从v12开始,有很大的改变.后缀名从xls变为xlsx Versions 5 ...
- Vue v-if v-for v-bind v-on
v-if <div v-if="type === 'A'"> A </div> <div v-else-if="type === 'B'&q ...
- ajax 使用 与 缓存问题
1:GET访问 浏览器 认为 是等幂的 就是 一个相同的URL 只有一个结果[相同是指 整个URL字符串完全匹配] 所以 第二次访问的时候 如果 URL字符串没变化 浏览器是 直接拿出了第一次访问的结 ...
- 关于字符串math函数的用法例子
var objStr=new String("Yue I love you till the end of my life!"); var reg3 = /[^\s+]/g; ob ...
- mvn本地执行java程序
mvn -f pom.xml compile exec:java -Dexec.classpathScope=compile -Dexec.mainClass=storm.starter.WordCo ...