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. 微信小程序的加载机制和运行机制

    一.运行机制 冷启动指的是重新启动,热启动指的是5分钟内从后台切换到前台,只有冷启动才能加载最新的包. 小程序什么时候会关闭? 5min后台运行,连续收到两次(2s)系统告警. 二.加载机制 三.小程 ...

  2. Spring事务管理-传播行为-隔离级别

    事务是逻辑上的一组操作,这组操作要么全部成功,要么全部失败. 事务的特性:ACID 原子性:事务是一个不可分割的工作单位,事务中的操作要么都发生,要么都不发生 一致性:事务前后数据的完整性约束保持一致 ...

  3. hdu 2774 后缀数组

    题目大意: 求两个字符串的最长公共子串 基本思路: 首先自然而然的想到应当把两个串拼接起来,为了便于处理,所以在串的中间加个大量(就是一个比其他值大的值),最后别忘了拼接上下标为n的时候是0,调用da ...

  4. Linux常用命令入门

    在Linux早期的版本中,由于不支持图形化操作,用户基本上都是使用命令行方式来对系统进行操作.掌握常用 的一些Linux命令是非常有必要的,下面将分类进行介绍.由于篇幅有限,在这里我们介绍命令时有些不 ...

  5. sql update语句

    如果要更新数据库表中的记录,我们就必须使用UPDATE语句. UPDATE语句的基本语法是: UPDATE <表名> SET 字段1=值1, 字段2=值2, ... WHERE ...; ...

  6. Shiro学习(3)授权

    授权,也叫访问控制,即在应用中控制谁能访问哪些资源(如访问页面/编辑数据/页面操作等).在授权中需了解的几个关键对象:主体(Subject).资源(Resource).权限(Permission).角 ...

  7. HTML之web项目的目录结构

    文件夹树注解 htmls    html一个文件放除去index.html外的其他页面文件. imgs    存放所有的图片文件:.png..jpg..jpeg.壁纸等. 示例:icon.png.ho ...

  8. Delphi 获取系统当前进程、窗口句柄、文件属性以及程序运行状态

    uses TLHelp32,PsAPI;(1)显示进程列表: procedure TForm1.Button2Click(Sender: TObject); var lppe: TProcessEnt ...

  9. Mac版本navicat premium彻底卸载的终端命令

    Mac版本navicat premium彻底卸载的终端命令: sudo rm -Rf /Applications/Navicat\ Premium.app sudo rm -Rf /private/v ...

  10. P1435 回文字串

    P1435 回文字串 题目背景 IOI2000第一题 题目描述 回文词是一种对称的字符串.任意给定一个字符串,通过插入若干字符,都可以变成回文词.此题的任务是,求出将给定字符串变成回文词所需要插入的最 ...