这题搞了好久,先是拓扑排序这里没想到,一开始自己傻乎乎的跑去找每层出度为1的点,然后才想到能用拓扑排序来弄。

拓扑排序的时候也弄了挺久的,拓扑排序用的也不多。

题意:给一个图求是否从对于任意两个点能从v 到w 或者从w到v连通。

思路:单连通,先强连通缩点,若scnt为1,或者出度为零的点为0,直接输出YES,若出度为零的点大于1,则代表有分支输出NO。若出度为零的点为1,判断组成的树是否为单链,即没有分支,用拓扑排序即可。

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define MAXN 1100
#define MAXM 6600
struct Edge
{
int to,next; }edge[MAXM]; int first[MAXN], stack[MAXN], DFN[MAXN], Low[MAXN], Belong[MAXN];
int indegree[MAXN],instack[MAXN];
int n,m,tot,scnt,top,cnt;
bool new_map[MAXN][MAXN];int vis[MAXN]; void Tarjan(int v)
{
int min,t;
DFN[v]=Low[v]=++tot;
instack[v]=1;
stack[top++]=v;
for(int e=first[v];e!=-1;e=edge[e].next)
{
int j=edge[e].to;
if(!DFN[j])
{
Tarjan(j);
if(Low[v]>Low[j])Low[v]=Low[j];
}
else if(instack[j]&&DFN[j]<Low[v])
{
Low[v]=DFN[j];
}
}
if(DFN[v]==Low[v])
{
scnt++;
do
{
t=stack[--top];
instack[t]=0;
Belong[t]=scnt;
}while(t!=v);
}
}
void read_graph(int v,int w)
{
edge[tot].to=w;
edge[tot].next=first[v];
first[v]=tot++;
}
void solve()
{
for(int i=1;i<=n;i++)
if(!DFN[i])
Tarjan(i);
}
void process(int j,int n)
{
for(int i=1;i!=n+1;i++)
{
if(new_map[j][i])
indegree[i]--;
}
}
int check(int n)
{
int count(0);
int t(0);
for(int i=1;i!=n+1;i++)
{
if(vis[i]==false&&indegree[i]==0)
{
t=i;
vis[i] = true;
count++;
}
}
if(t!=0)
process(t,n);
return count;
}
bool topo_sort(int n)
{
memset(vis,false,sizeof(vis));
for(int i=1;i!=n+1;i++)
{
if(check(n)>1)
return false;
}
return true;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(indegree,0,sizeof(indegree));
memset(DFN,0,sizeof(DFN));
memset(first,-1,sizeof(first));
cnt=scnt=tot=top=0; memset(new_map,false,sizeof(new_map)); scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
int v,w;
scanf("%d%d",&v,&w);
read_graph(v,w);
}
solve();
//cout<<scnt<<endl;
if(scnt==1)
{
printf("Yes\n");
continue;
}
for(int i=1;i<=n;i++)
{
for(int j=first[i];j!=-1;j=edge[j].next)
{
int v=edge[j].to;
if(Belong[i]!=Belong[v])
{
new_map[Belong[i]][Belong[v]] = true;
indegree[Belong[v]]++;
}
}
}
int count1=0;
for(int i=1;i<=scnt;i++)
{
if(indegree[i]==0)
count1++;
}
if(count1==0)
{
printf("Yes\n");
continue;
}
else if(count1>1)
{
printf("No\n");
continue;
}
if(topo_sort(scnt))
printf("Yes\n");
else
printf("No\n");
}
return 0;
}

poj 2762 强连通缩点+拓扑排序的更多相关文章

  1. Java实现判断单联通(强连通缩点+拓扑排序)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 ...

  2. poj 2762 tarjan缩点+拓扑序

    2013-09-08 10:00 var m, n :longint; t :longint; f, last :..] of longint; pre, other :..] of longint; ...

  3. 【BZOJ2330】糖果(差分约束系统,强连通分量,拓扑排序)

    题意: 幼儿园里有N个小朋友,lxhgww老师现在想要给这些小朋友们分配糖果,要求每个小朋友都要分到糖果.但是小朋友们也有嫉妒心,总是会提出一些要求,比如小明不希望小红分到的糖果比他的多,于是在分配糖 ...

  4. POJ 3249 Test for Job (拓扑排序+DP)

    POJ 3249 Test for Job (拓扑排序+DP) <题目链接> 题目大意: 给定一个有向图(图不一定连通),每个点都有点权(可能为负),让你求出从源点走向汇点的路径上的最大点 ...

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

  6. 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: 15812 ...

  7. poj 2762(强连通分量+拓扑排序)

    题目链接:http://poj.org/problem?id=2762 题意:给出一个有向图,判断任意的两个顶点(u,v)能否从u到达v,或v到达u,即单连通,输出Yes或No. 分析:对于同一个强连 ...

  8. POJ2762 Going from u to v or from v to u? 强连通分量缩点+拓扑排序

    题目链接:https://vjudge.net/contest/295959#problem/I 或者 http://poj.org/problem?id=2762 题意:输入多组样例,输入n个点和m ...

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

随机推荐

  1. Redis主从复制(Master/Slave)

    Redis主从复制(Master/Slave) 修改配置文件 拷贝多个redis.conf文件分别配置如下参数: 开启daemonize yes pidfile port logfile dbfile ...

  2. 26. leetcode 350. Intersection of Two Arrays II

    350. Intersection of Two Arrays II Given two arrays, write a function to compute their intersection. ...

  3. swift3.0 屏幕截图并且保存到本地相册

    所要截取的对象 var bg_view: UIView! 截取并且保存的代码如下 UIGraphicsBeginImageContextWithOptions(bg_view.frame.size, ...

  4. web端常见安全漏洞测试结果分析-- appscan

    基于appscan测试结果分析: 一.XSS跨站脚本 指的是攻击者往Web页面里插入恶意html代码,通常是JavaScript编写的恶意代码,当用户浏览该页之时,嵌入其中Web里面的html代码会被 ...

  5. WCF项目的架构设计

    本文将介绍以WCF开发项目为主的架构设计,主要从类库的分类和代码的结构. 下面将以一个WCF实例做具体的介绍.此项目底层是一个Windows Service,WCF服务Hosted在此Windows ...

  6. 再起航,我的学习笔记之JavaScript设计模式06(工厂方法模式)

    上一次已经给大家介绍了简单工厂模式,相信大家对创建型设计模式有了初步的了解,本次我将给大家介绍的是工厂方法模式. 工厂方法模式 工厂方法模式(Factory Method):通过对产品类的抽象使其创建 ...

  7. LGTB与序列 状压dp

    考试一看我就想到了状压dp.当时没有想到素数,以为每一位只有0~9这些数,就开始压了.后来发现是小于30,然后改到了15,发现数据一点不给面子,一个小点得数都没有,完美爆零.. 考虑到bi最多变成58 ...

  8. Jedis与Redisson选型对比

    1 概述 1.1.       主要内容 本文的主要内容为对比Redis的两个框架:Jedis与Redisson,分析各自的优势与缺点,为无线云管理项目中Redis编程模型的选择提供参考. 2.    ...

  9. SQL 结合CASE WHEN 实现二维统计

    在开发中往往要用到类似下面的二维统计:   a b type1 54 65 type2 54 54 在SQL中使用CASE WHEN 语句可以很轻松的实现: SELECT SUM(CASE WHEN ...

  10. ReactiveCocoa应用篇(二)

    上一篇介绍了ReactiveCocoa的常用类,已经基本满足项目中的简单应用要求,但是针对复杂的功能还需要其它的类来协同处理.ReactiveCocoa提供了强大的流程处理功能来解决复杂的问题,包括事 ...