poj 2762 强连通缩点+拓扑排序
这题搞了好久,先是拓扑排序这里没想到,一开始自己傻乎乎的跑去找每层出度为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 强连通缩点+拓扑排序的更多相关文章
- 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 ...
- poj 2762 tarjan缩点+拓扑序
2013-09-08 10:00 var m, n :longint; t :longint; f, last :..] of longint; pre, other :..] of longint; ...
- 【BZOJ2330】糖果(差分约束系统,强连通分量,拓扑排序)
题意: 幼儿园里有N个小朋友,lxhgww老师现在想要给这些小朋友们分配糖果,要求每个小朋友都要分到糖果.但是小朋友们也有嫉妒心,总是会提出一些要求,比如小明不希望小红分到的糖果比他的多,于是在分配糖 ...
- POJ 3249 Test for Job (拓扑排序+DP)
POJ 3249 Test for Job (拓扑排序+DP) <题目链接> 题目大意: 给定一个有向图(图不一定连通),每个点都有点权(可能为负),让你求出从源点走向汇点的路径上的最大点 ...
- 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. ...
- 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 ...
- poj 2762(强连通分量+拓扑排序)
题目链接:http://poj.org/problem?id=2762 题意:给出一个有向图,判断任意的两个顶点(u,v)能否从u到达v,或v到达u,即单连通,输出Yes或No. 分析:对于同一个强连 ...
- 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 ...
- 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 ...
随机推荐
- 教学小例子:简易的webSevrer
HttpListener 流利简单的API static void Main() { using (var server = new SimpleWebServer("http://loca ...
- Redis主从复制(Master/Slave)
Redis主从复制(Master/Slave) 修改配置文件 拷贝多个redis.conf文件分别配置如下参数: 开启daemonize yes pidfile port logfile dbfile ...
- CentOS 常用命令及快捷键整理
常用命令: 文件和目录: # cd /home 进入 '/home' 目录 # cd .. ...
- python实战第一天-socket模块练习
操作系统 Ubuntu 15.10 IDE & editor JetBrains PyCharm 5.0.2 ipython3 Python版本 python-3.4.3 导入socket模块 ...
- python--DenyHttp项目(2)--ACM监考客户端测试版(1阶段客户端总结)
客户端: 1.既然脚本是让别人用的,怎么说也得有个界面,(虽然很low) ''' DenyManager.py 调用客户端与客户端界面 ''' from DenyClient import * fro ...
- JS数组去重的十种方法
一.前言: 我们在实际工作中,或者在面试找工作时,都会用到或者被问到一个问题,那就是"数组如何去重".是的,这个问题有很多种解决方案,看看下面的十种方式吧! 二.数组去重方式大汇总 ...
- 数据结构二叉树的所有基本功能实现。(C++版)
本人刚学数据结构,对树的基本功能网上找不到C++代码 便自己写了一份,贴出方便大家进行测试和学习. 大部分功能未测试,如有错误或者BUG,请高手们指教一下,谢谢. 结点声明: BinTreeNode. ...
- SQL存储过程和函数
SQL存储过程: 由来:在具体应用中,一个完整的操作会包含多条SQL语句,在执行过程中需要根据前面SQL语句的执行结果有选择的执行后面的SQL语句.因此,mysql提供了数据库对象存储过程和函数. 定 ...
- iOS开发中如何创建多个target
在开发iOS应用程序的过程中,经常需要根据不同的需求,切换到不同的项目配置,或者打不同的包(测试环境.开发环境.生产环境等等),如果每次都是手动配置,一则比较麻烦,二则容易配置错,那么有没有更好的方案 ...
- Publish Web Site To IIS From VS
Referenced: http://jingyan.baidu.com/article/7908e85ca6db2daf491ad27e.html 1. Install IIS on IIS ser ...