Tarjan算法来解这题。无向图可以转化为有向图来解决。

#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1000000001
#define ll __int64
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int MAXN = ;
struct node
{
int to;
int next;
}edge[MAXN*];
stack<int>s;
int n,m,pre[MAXN],ind,low[MAXN],dfn[MAXN],vis[MAXN],pa[MAXN],ins[MAXN];
void add(int x,int y)
{
edge[ind].to = y;
edge[ind].next = pre[x];
pre[x] = ind ++;
}
int find(int x)
{
if(x != pa[x])pa[x] = find(pa[x]);
return pa[x];
}
void dfs(int rt,int k,int fa)
{
ins[rt] = ;
vis[rt] = ;
low[rt] = dfn[rt] = k;
s.push(rt);
for(int i = pre[rt]; i!=-; i=edge[i].next){
int t = edge[i].to;
if(!dfn[t] && t != fa){
dfs(t,k+,rt);
low[rt] = min(low[t],low[rt]);
}
else if(ins[rt] && t != fa){
low[rt] = min(dfn[t],low[rt]);
}
}
if(low[rt] == dfn[rt]){
while(!s.empty()){
int temp = s.top();
s.pop();
int fx = find(temp);
int fy = find(rt);
if(fx != fy){
pa[fx] = fy;
}
if(temp == rt)break;
}
}
}
int main()
{
while(cin >>n >>m){
ind = ;
for(int i = ; i <= n; i++)pa[i] = i;
memset(ins,,sizeof(ins));
memset(low,,sizeof(low));
memset(dfn,,sizeof(dfn));
memset(pre,-,sizeof(pre));
memset(vis,,sizeof(vis));
for(int i = ; i <= m; i++){
int x,y;
cin >>x >>y;
add(x,y);
add(y,x);
} for(int i = ; i<= n; i++){
if(!vis[i]){
dfs(i,,-);
}
} int q;
cin >>q; while(q--){
int x,y;
cin >>x >>y;
if(find(x) == find(y)){
cout<<"Yes"<<endl;
}
else {
cout<<"No"<<endl;
}
}
}
return ;
}

51nod 1076强连通的更多相关文章

  1. 51nod 1076 2条不相交的路径(边双连通分量)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1076 题意: 思路: 边双连通分量,跑一遍存储一下即可. #includ ...

  2. AC日记——2条不相交的路径 51nod 1076

    1076 2条不相交的路径 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题  收藏  关注 给出一个无向图G的顶点V和边E.进行Q次查询,查询从G的某个顶点V[s] ...

  3. 51nod 1076 2条不相交的路径

    给出一个无向图G的顶点V和边E.进行Q次查询,查询从G的某个顶点V[s]到另一个顶点V[t],是否存在2条不相交的路径.(两条路径不经过相同的边)   (注,无向图中不存在重边,也就是说确定起点和终点 ...

  4. 51nod 1076

    * 无向图的割边将图分为不连通的两部分 * 对于是否有不想交的两条路径将s -> t 相连 * 只需判断是否处于同一部分 * Tarjan即可 #include <bits/stdc++. ...

  5. tarjan相关模板

    感性理解: o(* ̄︶ ̄*)o  ^_^ \(^o^)/~ 1. 当根节点有大于两个儿子时,割掉它,剩下的点必然不联通(有两个强连通分量),则他为割点. 那么对于非根节点,在无向图G中,刚且仅当点u存 ...

  6. 51nod 1456【强连通,缩点,并查集】

    话说这道题的机遇是看到了http://blog.csdn.net/u010885899/article/details/50611895很有意思:然后就去补了这题 题意: 建最少的边使得给出的点相连. ...

  7. 51nod图论题解(4级,5级算法题)

    51nod图论题解(4级,5级算法题) 1805 小树 基准时间限制:1.5 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 她发现她的树的点上都有一个标号(从1到n),这些树都在空 ...

  8. 【51Nod 1244】莫比乌斯函数之和

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1244 模板题... 杜教筛和基于质因子分解的筛法都写了一下模板. 杜教筛 ...

  9. 51Nod 1268 和为K的组合

    51Nod  1268  和为K的组合 1268 和为K的组合 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 给出N个正整数组成的数组A,求能否从中选出若干个,使 ...

随机推荐

  1. Unity C# 反编译

    前言 结合前篇:[反编译U3D]Decompile Unity Resources 修正 本篇说说如何查看unity项目(apk) 的源代码,前提是这个apk的代码未经过加密. 写这篇的目地就是看看别 ...

  2. copy-paste component

    http://www.cnblogs.com/wantnon/p/4579008.html

  3. 对RTMP视频流进行BitmapData.draw()出错的解决办法

    source on github 在对Flash Media Server中的视频流使用BitmapData.draw()进行绘制的时候,会抛出这样异常: cannot access rtmp://x ...

  4. jmeter 与 java http

    jmeter 如果对java代码进行测试 1.eclips中创建一个项目,且写一个待测试的简单java代码 2.将jmeter路径下 x:\xx\lxx\Dowxxxxxx\apache-jmeter ...

  5. 父元素与子元素之间的margin-top问题

    父元素的盒子包含一个子元素盒子,给子元素盒子一个垂直外边距margin-top,父元素盒子也会往下走margin-top的值,而子元素和父元素的边距则没有发生变化. html代码: <div c ...

  6. Linux常用命令学习

    1.ls命令 就是list的缩写,通过ls 命令不仅可以查看linux文件夹包含的文件,而且可以查看文件权限(包括目录.文件夹.文件权限)查看目录信息等等 常用参数搭配: ls -a 列出目录所有文 ...

  7. WARN util.NativeCodeLoader: Unable to load native-hadooplibrary for your platform… using builtin-java classes where applicable

    方法1glibc 官方要求的2.14版本以上 方法2:http://www.secdoctor.com/html/yyjs/31101.html 方法3: http://dl.bintray.com/ ...

  8. 在ESXi 5.x之间冷迁移虚机

    试过几种不同的方法都不成功, 直接用VMware vCenter Converter Standalone Client迁移, 会出现task中的source与job中的source不一致的情况, 使 ...

  9. Java 集合系列03之 ArrayList详细介绍(源码解析)和使用示例

    概要 上一章,我们学习了Collection的架构.这一章开始,我们对Collection的具体实现类进行讲解:首先,讲解List,而List中ArrayList又最为常用.因此,本章我们讲解Arra ...

  10. UIScrollView增加刷新

    1. if (!self.scrollView) { CGRect frame = CGRectMake(0.0, 0.0, CGRectGetWidth(self.view.frame), CGRe ...