POJ 2762 Going from u to v or from v to u?(强连通分量+拓扑排序)
职务地址: id=2762">POJ 2762
先缩小点。进而推断网络拓扑结构是否每个号码1(排序我是想不出来这点的。
。。
)。由于假如有一层为2的话,那么从此之后这两个岔路的点就不可能从一点到还有一点的。
代码例如以下:
#include <iostream>
#include <string.h>
#include <math.h>
#include <queue>
#include <algorithm>
#include <stdlib.h>
#include <map>
#include <set>
#include <stdio.h>
using namespace std;
#define LL __int64
#define pi acos(-1.0)
const int mod=1e9+7;
const int INF=0x3f3f3f3f;
const double eqs=1e-9;
const int MAXN=1000+10;
int head[MAXN], Ecnt, scc, top, indx;
int low[MAXN], dfn[MAXN], belong[MAXN], stk[MAXN], instack[MAXN];
struct node
{
int u, v, next;
}edge[7000];
void add(int u, int v)
{
edge[Ecnt].v=v;
edge[Ecnt].next=head[u];
head[u]=Ecnt++;
}
void tarjan(int u)
{
low[u]=dfn[u]=++indx;
instack[u]=1;
stk[++top]=u;
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].v;
if(!dfn[v]){
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(instack[v]){
low[u]=min(low[u],dfn[v]);
}
}
if(low[u]==dfn[u]){
scc++;
while(1){
int v=stk[top--];
belong[v]=scc;
instack[v]=0;
if(u==v) break;
}
}
}
void init()
{
memset(head,-1,sizeof(head));
memset(dfn,0,sizeof(dfn));
memset(instack,0,sizeof(instack));
Ecnt=top=indx=scc=0;
}
int head1[MAXN], Ecnt1, deg[MAXN];
struct node1
{
int u, v, next;
}edge1[7000];
void add1(int u, int v)
{
edge1[Ecnt1].v=v;
edge1[Ecnt1].next=head1[u];
head1[u]=Ecnt1++;
}
bool topo(int scc)
{
int i, u, tot, m=scc;
while(m--){
tot=0;
for(i=1;i<=scc;i++){
if(!deg[i]){
tot++;
u=i;
deg[i]--;
}
}
if(tot>1) return false;
for(i=head1[u];i!=-1;i=edge1[i].next){
deg[edge1[i].v]--;
}
}
return true;
}
void init1()
{
memset(deg,0,sizeof(deg));
memset(head1,-1,sizeof(head1));
Ecnt1=0;
}
int main()
{
int n, m, i, j, u, v, t, f;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
init();
while(m--){
scanf("%d%d",&u,&v);
add(u,v);
}
for(i=1;i<=n;i++){
if(!dfn[i])
tarjan(i);
}
init1();
for(i=1;i<=n;i++){
for(j=head[i];j!=-1;j=edge[j].next){
v=edge[j].v;
if(belong[i]!=belong[v]){
add1(belong[i],belong[v]);
deg[belong[v]]++;
//printf("%d %d\n",belong[i],belong[v]);
}
}
}
if(topo(scc)) puts("Yes");
else puts("No");
}
return 0;
}
POJ 2762 Going from u to v or from v to u?(强连通分量+拓扑排序)的更多相关文章
- poj 2762(强连通分量+拓扑排序)
题目链接:http://poj.org/problem?id=2762 题意:给出一个有向图,判断任意的两个顶点(u,v)能否从u到达v,或v到达u,即单连通,输出Yes或No. 分析:对于同一个强连 ...
- POJ-2762-Going from u to v or from v to u(强连通, 拓扑排序)
链接: https://vjudge.net/problem/POJ-2762 题意: In order to make their sons brave, Jiajia and Wind take ...
- poj 2186 "Popular Cows"(强连通分量入门题)
传送门 参考资料: [1]:挑战程序设计竞赛 题意: 每头牛都想成为牛群中的红人. 给定N头牛的牛群和M个有序对(A, B),(A, B)表示牛A认为牛B是红人: 该关系具有传递性,所以如果牛A认为牛 ...
- POJ - 1236 Network of Schools(有向图的强连通分量)
d.各学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输, 问题1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件. 问题2:至少需要添加几条传输线 ...
- 图论之拓扑排序 poj 2367 Genealogical tree
题目链接 http://poj.org/problem?id=2367 题意就是给定一系列关系,按这些关系拓扑排序. #include<cstdio> #include<cstrin ...
- 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?(强连通分量+缩点重构图+拓扑排序)
http://poj.org/problem?id=2762 Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: ...
- POJ 2762 Going from u to v or from v to u? (判断单连通)
http://poj.org/problem?id=2762 题意:给出有向图,判断任意两个点u和v,是否可以从u到v或者从v到u. 思路: 判断图是否是单连通的. 首先来一遍强连通缩点,重新建立新图 ...
- [ tarjan + dfs ] poj 2762 Going from u to v or from v to u?
题目链接: http://poj.org/problem?id=2762 Going from u to v or from v to u? Time Limit: 2000MS Memory L ...
随机推荐
- 【SICP归纳】2 高阶函数和数据抽象
上一篇博文相应的是书中的第一章的一二两节,我们已经大致的有了一种构造的感觉不是么. 书中展示了非常多有趣的句法(syntax). 如今我们要让思想进一步的抽象.写这篇博客的时候并未学完整本书.更不敢说 ...
- Hdu 5256 系列转换
主题链接: HDU5236 代码: #include<iostream> #include<cstdio> #include<cstring> #include&l ...
- POJ 2250 Compromise (UVA 531)
LCS问题.基金会DP. 我很伤心WA非常多.就在LCS问题,需要记录什么路. 反正自己的纪录path错误,最后,就容易上当. 没有优化,二维阵列,递归打印,cin.eof() 来识别 end of ...
- cer, pfx 创建,而且读取公钥/密钥,加解密 (C#程序实现)
PKI技术(public key infrastructure)里面,cer文件和pfx文件是非经常见的.通常cer文件中面保存着公钥以及用户的一些信息,pfx里面则含有私钥和公钥. 用makecer ...
- 重写onBackPressed方法
android手机back按键响应方法重构: long exitTime = System.currentTimeMillis() - 2000; public void onBackPressed( ...
- CF417D--- Cunning Gena(序列+像缩进dp)
A boy named Gena really wants to get to the "Russian Code Cup" finals, or at least get a t ...
- ArcPad 10 的安装部署
ArcPad是安装在手持设备或者移动终端的一个外业ArcGIS产品,也就是说ArcPad是Esri的一款软件产品,而不是硬件设备哦.尽管不比ArcGIS Desktop功能复杂缤纷,可是对于野外作业. ...
- Linux的proc文件系统
proc,用户空间和内核空间能够通过该接口通信, 与普通文件不同的是.这些虚拟文件的内容都是动态创建的. proc文件系统是一个伪文件系统,它仅仅存在内存其中,而不占用外存空间. 它以文件系统的方式为 ...
- Apple Watch 1.0 开发介绍 2.1 WatchKit Apps UI要点
实现app的开始是定义storyboard场景.每个场景定义了app的一部分界面.可以为不同的尺寸自定义场景. 组装storyboard界面 WatchKit app和iOS app的布局模式不同.组 ...
- 【Bible for kids】 儿童圣经 App
[Bible for kids] 儿童圣经App 除了<The Bible>这个由YouVersion团队开发的全球下载量和安装数目第一的圣经类.安装量已逾1亿8千万的App之外,YouV ...