题目链接:

poj2762

题意:

给出一幅单向图。问这张图是否满足   随意两点ab 都能 从a到达b 或  从b到达a

题解思路:

推断一幅图是否满足弱连通

首先想到的是将图中的 强连通分量(能互相到达的顶点集)  进行缩点

然后再依据原有边 又一次建图

假设缩点后的图是一条单链(回路,通路都能够)   则一定满足弱连通

推断是否是一条单链 能够依据建图过程中得到 入度 出度 数组进行推断

某点的入度 或 出度假设大于1则一定不是单链

另外单链仅仅能有一条  不能有多个点入度=0

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#define maxn 1050
using namespace std;
struct node
{
int to,next;
} edge[maxn*6];
int head[maxn];
int s; int dfn[maxn],low[maxn],num; int sta[maxn],insta[maxn],top; int belong[maxn],block; void init()
{
memset(head,-1,sizeof(head));
memset(insta,0,sizeof(insta));
memset(dfn,0,sizeof(dfn));
block=s=num=top=0;
} void addedge(int a,int b)
{
edge[s]= {b,head[a]};
head[a]=s++;
} void Tarjan(int u,int pre)
{
dfn[u]=low[u]=++num;
insta[u]=1;
sta[top++]=u;
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].to;
if(!dfn[v])
{
Tarjan(v,u);
low[u]=min(low[u],low[v]);
}
else if(insta[v]) //回边
low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u]) //缩点
{
int d;
block++;
while(d!=u)
{
d=sta[--top];
insta[d]=0;
belong[d]=block;
}
}
} int rebuild(int n)
{
int indegree[maxn]= {0};
int outdegree[maxn]={0};
int u,v;
for(int i=1; i<=n; i++) //又一次建图
{
u=belong[i];
for(int j=head[i]; j!=-1; j=edge[j].next)
{
v=edge[j].to;
v=belong[v];
if(u!=v) //不在同一个强连通分量才干建边
{
outdegree[u]++;
indegree[v]++;
if(indegree[v]>1||outdegree[u]>1)
return 0;
}
}
}
int ss=0;
for(int i=1; i<=block; i++)
if(!indegree[i])
ss++;
if(ss>1) //入度=0的点有多个
return 0;
return 1;
} int main()
{
int n,m,a,b;
int T;
scanf("%d",&T);
while(T--)
{
init();
scanf("%d%d",&n,&m);
while(m--)
{
scanf("%d%d",&a,&b);
addedge(a,b);
}
for(int i=1;i<=n;i++) //
if(!dfn[i]) //有向图Tarjan算法
Tarjan(i,-1); // if(rebuild_topsort(n))
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
return 0;
}

POJ2762 Going from u to v or from v to u? 强连通+缩点的更多相关文章

  1. POJ2762 Going from u to v or from v to u(单连通 缩点)

    判断图是否单连通,先用强连通分图处理,再拓扑排序,需注意: 符合要求的不一定是链拓扑排序列结果唯一,即在队列中的元素始终只有一个 #include<cstdio> #include< ...

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

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

  3. [poj2762] Going from u to v or from v to u?(Kosaraju缩点+拓排)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud     Going from u to v or from v to u? Tim ...

  4. 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 ...

  5. 【缩点+拓扑判链】POJ2762 Going from u to v or from v to u?

    Description In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has ...

  6. Oracle基本数据字典:v$database、v$instance、v$version、dba_objects

    v$database: 视图结构: SQL> desc v$database; Name                                      Null?    Type - ...

  7. Going from u to v or from v to u?_POJ2762强连通+并查集缩点+拓扑排序

         Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K       Description I ...

  8. 临时文件相关的v$tempfile v$sort_usage与V$tempseg_usage

    SQL> select username,user,segtype,segfile#,segblk#,extents,segrfno# from v$sort_usage; SEGFILE#代表 ...

  9. [强连通分量] 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 ...

随机推荐

  1. JS 常用库汇总收集

    本文不定期更新, 用于汇总记录一些看着 ok 的 JS 库. 库名 简介 项目地址 macy.js 仅 4 kb的 原生 流布局插件 http://macyjs.com/ Driver.js 仅 4 ...

  2. 使用Java创建Excel,并添加内容

    使用Java创建Excel,并添加内容 一.依赖的Jar包 jxl.jar,使用jxl操作Excel Jxl是一个开源的Java Excel API项目,通过Jxl,Java可以很方便的操作微软的Ex ...

  3. blog搬家啦

    本blog大概不会更新了 新blog地址:https://zykykyk.github.io/

  4. Windows Server 2008 R2下将JBoss安装成windows系统服务

    JBoss版本是jboss-4.2.3.GA-jdk6.zip,操作系统是Windows Server 2008 R2. 1.系统已安装好java环境,JAVA_HOME已配置好: 2.下载所需文件. ...

  5. CodeForces 128D Numbers 构造

    D. Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...

  6. spring---transaction(2)---源代码分析(事务的定义TransactionDefinition)

    写在前面 事务属性通过TransactionDefinition接口实现定义,主要有事务隔离级别.事务传播行为.事务超时时间.事务是否只读. public interface TransactionD ...

  7. 反射生成SQL语句入门

    今天我们来学习学习通过反射技术来生成SQL语句. 反射提供了封装程序集.模块和类型的对象.您可以使用反射动态地创建类型的实例,将类型绑定到现有对象,或从现有对象中获取类型.然后,可以调用类型的方法或访 ...

  8. 理解JavaScript中BOM和DOM的关系

    JavaScript 有三部分构成,ECMAScript,DOM和BOM,根据宿主(浏览器)的不同,具体的表现形式也不尽相同,IE和其他的浏览器风格迥异.对象是JavaScript最重要的API,包含 ...

  9. Tomcat集群扩展session集中管理,Memcached-session-manager使用

    研究tomcat做负载均衡的时候如何实现ha,还有就是不采用session复制的方法做集群. 想到的是将session全部存储在后端的缓存服务器中. 正好网上有这么一个工具Memcached-sess ...

  10. Android平台上优秀的开源项目

    软件名:gaeproxy 软件作用:Android手机配置GoAgent. 项目地址:https://github.com/madeye/gaeproxy.git 软件名:ProxyDroid 软件作 ...