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. 将Eclipse项目转换成AndroidStudio项目过程中遇到的问题以及解决方法

    将Eclipse项目转换成AndroidStudio项目也不是第一次了,昨天转的时候遇到几个问题: 首先将项目导入androidstudio,导完后报错: 问题一: Error:java.util.c ...

  2. 2018-2-13-win10-uwp-魔力鬼畜

    title author date CreateTime categories win10 uwp 魔力鬼畜 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17: ...

  3. Stm32CubeMX5 配置 STM32的串口DMA接受方式 --- 基于 stm32f051k8u6

     实现的功能: 使用MDA方式把串口接受的数据在发送给串口(当然也可以做其他解析控制使用) 1. 先初始化 时钟使用外部的晶振配置系统时钟为48Mhz 2. 串口参数配置 3. 使能中断 4. 配置串 ...

  4. 使用ReadStream方法延时读取文件

    const fs = require('fs'); let file = fs.createReadStream("filenpath.js"); file.pause(); fi ...

  5. 视频专家之路【四】:ffmpeg简单实战之获取属性

    本文是听了雷宵骅大神的课之后的总结,部分内容借用了其PPT的内容,如有侵权请告知删除. 雷宵骅大神的博客为:https://blog.csdn.net/leixiaohua1020 本节的目的正式开始 ...

  6. idea激活教程,最新!!!

    1.下载破解补丁(关键). 破解补丁:JetbrainsIdesCrack-4.2-release.jar百度云地址:https://pan.baidu.com/s/18ovphd7sm7oYXQb4 ...

  7. cf期望概率专题

    cf1009E:求到第i段期望和的比较困难,但是单独求每段的期望是比较容易的,所以单独对每段求和,然后累计总和 E[i]=1/2*a1+1/4*a2+...+1/2^(i-1)*ai-1+1/2^(i ...

  8. python:列表、元组和字典

    1.1.特点:   任意对象的有序集合   通过偏移量读取   可变长度,异构以及任意嵌套   属于可变序列的分类   对象引用数组:当把一个对象赋给一个数据结构元素或变量名时,python总会存储对 ...

  9. Web移动端常见问题-摘抄

      一.按钮点击时出现黑色背景 解决方法: 1 2 .class { -webkit-tap-highlight-color:rgba(0,0,0,0);} .class { -webkit-appe ...

  10. CSS:CSS padding(填充)

    ylbtech-CSS:CSS padding(填充) 1.返回顶部 1. CSS padding(填充) CSS padding(填充)是一个简写属性,定义元素边框与元素内容之间的空间,即上下左右的 ...