这题搞了好久,先是拓扑排序这里没想到,一开始自己傻乎乎的跑去找每层出度为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. Java ee 与安卓环境搭建个人心得

    最近加了个IT俱乐部,第一次作业就是搞定eclipse,完成Java ee 与安卓环境搭建.为此我上网看了好多教程,之前我安装了Java,可以说省了不少事,而且还了解一点安装方法.流程网上都有,但是不 ...

  2. Java学生成绩

    import java.util.*; public class guanlixiton { public static void main(String[] args) { Scanner in = ...

  3. oAuth 认证

    这段时间公司开发项目用到oAuth2协议,现在做一下梳理. CORS即Cross Origin Resouce Share,跨域资源共享:是W3C为防止脚本攻击,而制定的安全标准之一,它云溪浏览器向跨 ...

  4. 自己动手封装一个url参数解释器( ghostWuUrlParser.js )

    ghostWuUrlParser.js的作用是分析一段url中的查询参数,即: '?'号后面的 键值对参数. ghostWuUrlParser.js 使用说明: ghostWuUrlParser( ' ...

  5. vue指令v-model示例解析

    限制 <input> <select> <textarea> components 修饰符 .lazy - 取代 input 监听 change 事件 .numbe ...

  6. 最短路和次短路问题,dijkstra算法

    /*  *题目大意:  *在一个有向图中,求从s到t两个点之间的最短路和比最短路长1的次短路的条数之和;  *  *算法思想:  *用A*求第K短路,目测会超时,直接在dijkstra算法上求次短路; ...

  7. 一个普通的 Zepto 源码分析(三) - event 模块

    一个普通的 Zepto 源码分析(三) - event 模块 普通的路人,普通地瞧.分析时使用的是目前最新 1.2.0 版本. Zepto 可以由许多模块组成,默认包含的模块有 zepto 核心模块, ...

  8. QTextEdit控件使用

    QTextEdit控件使用 QTextEdit *mpContentTextEdit = new QTextEdit(this); //设置占位符文本 mpContentTextEdit->se ...

  9. VC++动态链接库(DLL)编程深入浅出

    1.概论 先来阐述一下DLL(Dynamic Linkable Library)的概念,你可以简单的把DLL看成一种仓库,它提供给你一些可以直接拿来用的变量.函数或类.在仓库的发展史上经历了“无库-静 ...

  10. 谈谈出入React框架踩过的坑

    1 在JSX的元素中写入内联样式,例如<div style={"color:blue"}></div> 报错:warning:Style prop valu ...