这题搞了好久,先是拓扑排序这里没想到,一开始自己傻乎乎的跑去找每层出度为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. cn_office2016

    office 2016是可用激活器激活的,但是激活office 365就有点困难了. 附上office 2016下载地址:http://pan.baidu.com/s/1pLTqPyr 破解机:htt ...

  2. 玛雅游戏[NOIP2011]

    题目描述 Mayan puzzle 是最近流行起来的一个游戏.游戏界面是一个7 行5 列的棋盘,上面堆放着一些方块,方块不能悬空堆放,即方块必须放在最下面一行,或者放在其他方块之上.游戏通关是指在规定 ...

  3. [js高手之路] es6系列教程 - 不定参数与展开运算符(...)

    三个点(...)在es6中,有两个含义: 用在形参中, 表示传递给他的参数集合, 类似于arguments, 叫不定参数. 语法格式:  在形参面前加三个点( ... ) 用在数组前面,可以把数组的值 ...

  4. vc类型转换函数大全

    windows c++中存在各种类型,在实际应用过程中也需要将类型互相转换,故整理了常用类型之间的转换并将之封装成函数,仅供参考,有什么不对的地方,还请指正!   ****************** ...

  5. css中的几个小tip(二)

    margin的塌陷现象 (一)在标准文档流中, 垂直方向存在margin的塌陷现象 先上段代码: <style type="text/css"> .box{ width ...

  6. Cordova + idea 环境搭建

    1.安装前期工作 1).安装Node.js http://nodejs.cn/download/ 里面内置了npm,可以用来安装 Cordova,把该路径添加到环境变量,这样就可以在 cmd 里面任何 ...

  7. Python使用PDFMiner解析PDF

    近期在做爬虫时有时会遇到网站只提供pdf的情况,这样就不能使用scrapy直接抓取页面内容了,只能通过解析PDF的方式处理,目前的解决方案大致只有pyPDF和PDFMiner.因为据说PDFMiner ...

  8. MATLAB批量读入图片

    %% import pictures, and save into images{img_num} function [images, img_num, vertical_border] = impo ...

  9. vue如何封装自己需要的方法

    因为现在vue的流行,vue的各种插件都出来了,我们公司也是使用vue做项目,我自己在做项目的时候自己去琢磨了其他的插件以及结合自己对vue和es2015的理解,自己找的了一种在vue中使用封装方法的 ...

  10. Dynamics CRM:Word Template Feature 的使用和实际遇到问题解决方案

    Word Template 是Dynamics CRM 2016 中的其中一个新的功能.Word Template 就是文档模板,用于在Dynamics CRM中定义文档模板,从而对实体记录按照文档模 ...