http://poj.org/problem?id=2762

强连通求子图和子图关系 + 子图关系是链式结构

 #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
#define ll long long const double eps=1e-;
const ll inf=1e9;
const ll mod=1e9+;
const int maxn=1e3+; struct node
{
int d;
node *to;
}*e[maxn]; bool vis[maxn]; int num,cnt_st,st[maxn],dfn[maxn],low[maxn],cnt_group,group[maxn],cnt_single,gx[maxn],gy[maxn];
bool vis_st[maxn],ifok; void tarjan(int d)
{
dfn[d]=low[d]=++num;
vis[d]=;
st[++cnt_st]=d;
node *p=e[d];
while (p)
{
if (!vis[p->d])
{
tarjan(p->d);
low[d]=min(low[d],low[p->d]);
}
else if (!vis_st[p->d])
low[d]=min(low[d],dfn[p->d]);
p=p->to;
}
if (dfn[d]==low[d])
{
cnt_group++;
while (d!=st[cnt_st])
{
group[st[cnt_st]]=cnt_group;
vis_st[st[cnt_st]]=;
cnt_st--;
}
group[st[cnt_st]]=cnt_group;
vis_st[st[cnt_st]]=;
cnt_st--;
}
} int main()
{
node *p;
int T,n,m,x,y,i;
scanf("%d",&T);
while (T--)
{
scanf("%d%d",&n,&m);
for (i=;i<=n;i++)
e[i]=NULL; while (m--)
{
scanf("%d%d",&x,&y);
p=new node();
p->d=y;
p->to=e[x];
e[x]=p;
} memset(vis,,sizeof(vis));
memset(vis_st,,sizeof(vis_st));
num=,cnt_st=,cnt_group=;
for (i=;i<=n;i++)
if (!vis[i])
tarjan(i); /*
if all are ok:需要的是链式结构
1.对于一个子图,最多只有一个子图指向它,最多只有一个子图被它指向(非头结点)
2.有且只有一个子图,没有子图指向它(头结点)
*/ ifok=,cnt_single=;
memset(gx,,sizeof(gx));
memset(gy,,sizeof(gy));
for (i=;i<=n;i++)
{
p=e[i];
while (p)
{
///i -> p->d
if (group[p->d]!=group[i])
{
if (gx[group[p->d]]==)
gx[group[p->d]]=group[i];
else if (gx[group[p->d]]!=group[i])
ifok=; if (gy[group[i]]==)
gy[group[i]]=group[p->d];
else if (gy[group[i]]!=group[p->d])
ifok=;
}
p=p->to;
}
}
for (i=;i<=cnt_group;i++)
if (!gx[i])
cnt_single++; printf("%s\n",(ifok&(cnt_single==))?"Yes":"No");
}
return ;
}
/*
10
3 2
1 2
3 2 4 3
1 2
2 3
3 1 3 3
1 2
2 3
3 1 5 4
1 2
2 3
3 4
4 5 3 2
1 2
1 3 3 2
1 2
3 2 4 3
1 2
2 3
3 1 3 3
1 2
2 3
3 1 5 4
1 2
2 3
3 4
4 5 3 2
1 2
1 3
*/

Advance:如何判断两个结点是否属于同一个单向连通子图(就是是否可以从x到y或者从y到x)[多组数据]

强连通图,同一个集合内的点,可以互相到达

拓扑结构,point a in group x, point b in group y,如x能到达y,则a能到b,但b不能到a。

其它情况下,两者均不能到达对方。

在拓扑结构中,一个点也许有多个孩子,多个祖先。

没有想到什么好的方法。。。不过感觉应该有。

TLE代码

 #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
#define ll long long
//#include <map> const double eps=1e-;
const ll inf=1e9;
const ll mod=1e9+;
const int maxn=1e3+; struct node
{
int d;
node *to;
}*e[maxn]; bool vis[maxn],f[maxn][maxn];
int c,q[maxn]; //void dfs(int d)
//{
// vis[d]=1;
// f[c][d]=1;
// node *p=e[d];
// while (p)
// {
// if (!vis[p->d])
// dfs(p->d);
// p=p->to;
// }
//} //map<int,int> ma; int main()
{
node *p;
int T,n,m,x,y,i,j;
int head,tail,d;
int num;
bool v;
scanf("%d",&T);
while (T--)
{
scanf("%d%d",&n,&m);
for (i=;i<=n;i++)
e[i]=;
// ma.clear();
num=;
while (m--)
{
scanf("%d%d",&x,&y);
// if (ma.find(x)!=ma.end())
// x=ma[x];
// else
// ma[x]=++num,x=num;
// if (ma.find(y)!=ma.end())
// y=ma[y];
// else
// ma[y]=++num,y=num; p=new node();
p->d=y;
p->to=e[x];
e[x]=p;
}
for (c=;c<=n;c++)
{
// memset(vis,0,sizeof(vis));
// dfs(c); q[]=c;
vis[c]=;
head=,tail=;
while (head<tail)
{
head++;
d=q[head];
p=e[d];
while (p)
{
if (!vis[p->d])
{
vis[p->d]=;
q[++tail]=p->d;
f[c][p->d]=;
}
p=p->to;
}
}
for (j=;j<=tail;j++)
vis[q[j]]=;
} v=;
for (i=;i<=n;i++)
for (j=i+;j<=n;j++)
if (!f[i][j] && !f[j][i])
v=;
printf("%s\n",v?"Yes":"No");
}
return ;
}

单向连通图 Going from u to v or from v to u? poj2762的更多相关文章

  1. poj2767,单向连通图判定,缩点+重新建图+新图DFS

    /*该题被博客里标记为中等题,30分钟内1A,掌握了算法就简单了,单向连通图判定,单向连通图缩点 后必然唯一存在出度为0的点和入度为0的点,并且从入度为0的点出发,可以遍历所有点后到达出度为0点 (一 ...

  2. POJ2762 Going from u to v or from v to u?(判定单连通图:强连通分量+缩点+拓扑排序)

    这道题要判断一张有向图是否是单连通图,即图中是否任意两点u和v都存在u到v或v到u的路径. 方法是,找出图中所有强连通分量,强连通分量上的点肯定也是满足单连通性的,然后对强连通分量进行缩点,缩点后就变 ...

  3. 判断单向连通图(拓扑排序+tarjan缩点)

    题意: 给你一个有向图,如果对于图中的任意一对点u和v都有一条从u到v的路或从v到u的路,那么就输出’Yes’,否则输出’No’. 理解:当出现两个及以上入度为0的点(有一个就可能是别人到它,有两个的 ...

  4. POJ2762 单向连通图(缩点+拓扑排序

    Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19552 ...

  5. [强连通分量] POJ 2762 Going from u to v or from v to u?

    Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17089 ...

  6. poj 2762 Going from u to v or from v to u?

    题目描述:为了让他们的儿子变得更勇敢些,Jiajia和Wind将他们带到一个大洞穴中.洞穴中有n个房间,有一些单向的通道连接某些房间.每次,Wind选择两个房间x和y,要求他们的一个儿子从一个房间走到 ...

  7. POJ 2762 Going from u to v or from v to u? (强连通分量缩点+拓扑排序)

    题目链接:http://poj.org/problem?id=2762 题意是 有t组样例,n个点m条有向边,取任意两个点u和v,问u能不能到v 或者v能不能到u,要是可以就输出Yes,否则输出No. ...

  8. POJ 2762 Going from u to v or from v to u?(强联通 + TopSort)

    题目大意: 为了锻炼自己的儿子 Jiajia 和Wind 把自己的儿子带入到一个洞穴内,洞穴有n个房间,洞穴的道路是单向的. 每一次Wind 选择两个房间  x 和 y,   让他的儿子从一个房间走到 ...

  9. poj2762 Going from u to v or from v to u?

    Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13040 ...

  10. poj 2762 Going from u to v or from v to u? (推断它是否是一个薄弱环节图)

    意甲冠军:给定一个有向图有m单向边缘.免费推断是否两点起来(a可以b要么b可以a或最多彼此),该请求 弱联通重量. 算法: 缩点求强连通分量.然后又一次建图.推断新图是否是一条单链,即不能分叉,假设分 ...

随机推荐

  1. 【LeetCode】排序

    [349] Intersection of Two Arrays [Easy] 两个无序可重复数组找交集, 交集要求元素唯一. Given nums1 = [1, 2, 2, 1], nums2 =  ...

  2. java中String类的面试题大全含答案

    1.下面程序的运行结果是()(选择一项)String str1="hello";String str2=new String("hello");System.o ...

  3. android studio 设计任务内容和识别内容界面 (android stuido design task layout)

    本人android studio版本是 3.4.1,设计了一个任务内容和识别内容的界面,欢迎大家品尝. 界面显示如下图所示: 实现代码如下: <?xml version="1.0&qu ...

  4. 新建门脸Facade类

    1.App\Contract目录下新建 CommonContract 类 <?php namespace App\Contract; use Carbon\Carbon; use \Dimsav ...

  5. javascript onclick事件可以调用两个方法吗?

    答案是:可以的,onclick事件可以调用多个方法,每个方法之间用分号(:)隔开即可. onclick后面其实是可以写任何代码的,但是一般不建议这么写!! 例:onclick="fun1() ...

  6. 阿里云资深技术专家黄省江:让天下没有难做的SaaS

    导语:本文中,阿里云资深技术专家黄省江(花名禅笑)将聚焦“SaaS加速器——让天下没有难做的SaaS”,对伙伴来说,SaaS加速器帮助他们做好SaaS,卖好SaaS:对企业来说,SaaS加速器帮助他们 ...

  7. Shiro学习(6)Realm整合

    6.1 Realm [2.5 Realm]及[3.5 Authorizer]部分都已经详细介绍过Realm了,接下来再来看一下一般真实环境下的Realm如何实现. 1.定义实体及关系 即用户-角色之间 ...

  8. NX二次开发-用户自定义资源栏选项卡RegisterActivationCallback

    最近在研究UGOPEN文件夹里的例子,发现从NX10版开始可以用户自定义资源栏选项卡了,NX10以下也可以做,但是需要反编译DLL调内部函数,这个只有高手才会,我是不会弄. 以前看过有人把标准件库做到 ...

  9. ArcMAP中Excel数据转换为shp数据

    参考百度知道:http://jingyan.baidu.com/article/f7ff0bfc1cf22c2e26bb138d.html 将数据库中带有X.Y坐标的二维表转换为空间点数据:从数据中将 ...

  10. Bootstrap 避免模态框在用户点击背景空白处时,会自动关闭。

    问题: Bootstrap 模态框在用户点击背景空白处时,会自动关闭. 解决方法: 在HTML页面中编写模态框时,在div初始化时添加属性 aria-hidden=”true” data-backdr ...