题目大意:

为了锻炼自己的儿子 Jiajia 和Wind 把自己的儿子带入到一个洞穴内,洞穴有n个房间,洞穴的道路是单向的。
每一次Wind 选择两个房间  x 和 y,   让他的儿子从一个房间走到另一个房间,(要么从 x->y  或者 y->x), Wind承诺这个是一定可以走到的。但是他不知道如何判断这个 xy一定是互通的,现在给你一个洞穴,问随机给你两个洞穴的编号,是否是相通的。
题目分析:题目意思有点偏移,其实意思是(只要能从x->y  或者 y->x 这个都算是通的), 求一下TopSort,判断能否直接连成一串。
(集训前恶补了一下数据结构,再写TopSort的时候拿以前笔记,看一下就懂了了。)
 
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <vector>
usingnamespace std;
#define INF 0x7ffffff
#define maxn 1005
typedef longlong LL;
#define Min(a,b) (a<b?a:b)
#define MOD 1000000007
int m, n, Time, top, ans;
int Stack[maxn], dfn[maxn], low[maxn], P[maxn][maxn], blocks[maxn], In[maxn];
bool InStack[maxn];
vector<vector<int> > G;
void init()
{
memset(In, 0, sizeof(In));
memset(dfn, 0, sizeof(dfn));
memset(low, 0, sizeof(low));
memset(P, 0, sizeof(P));
ans = Time = top = 0;
G.clear();
G.resize(n+2);
}
bool TopSort()
{
int cnt = 0;
top = 0;
for(int i=0; i<ans; i++)
{
if(In[i] == 0)
{
Stack[top ++] = i;
cnt ++;
break;
}
} while(top)
{
int v = Stack[--top];
for(int i=0; i<ans; i++)
{
if(P[v][i] && ! --In[i])
{
Stack[top++] = i;
cnt ++;
}
}
}
return cnt == ans;
} void Tarjan(int u)
{
dfn[u] = low[u] = ++Time;
Stack[top++] = u;
InStack[u] = true;
int len = G[u].size(), v; for(int i=0; i<len; i++)
{
v = G[u][i];
if( !low[v] )
{
Tarjan(v);
low[u] = min(low[u], low[v]);
}
elseif( InStack[v] )
low[u] = min(low[u], dfn[v]);
}
if(low[u] == dfn[u])
{
do
{
v = Stack[--top];
InStack[v] = false;
blocks[v] = ans;
}
while(u != v);
ans ++;
}
} void solve()
{
for(int i=1; i<=n; i++)
{
if(!low[i])
Tarjan(i);
} for(int i=1; i<=n; i++)
{
int len = G[i].size(), v;
for(int j=0; j<len; j++)
{
v = G[i][j];
if(blocks[i] != blocks[v])
{
int a = blocks[i], b = blocks[v];
P[a][b] ++;
if(P[a][b] == 1)
{
In[b] ++;
break;
} }
}
}
if( !TopSort() )
printf("No\n");
else
puts("Yes");
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d %d",&n, &m);
init();
while(m --)
{
int a, b;
scanf("%d %d",&a, &b);
G[a].push_back(b);
}
solve();
}
return0;
}

POJ 2762 Going from u to v or from v to u?(强联通 + TopSort)的更多相关文章

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

  2. 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:  ...

  3. POJ 2762 Going from u to v or from v to u?(强连通分量+拓扑排序)

    职务地址:id=2762">POJ 2762 先缩小点.进而推断网络拓扑结构是否每个号码1(排序我是想不出来这点的. .. ).由于假如有一层为2的话,那么从此之后这两个岔路的点就不可 ...

  4. POJ 2762 Going from u to v or from v to u? (判断单连通)

    http://poj.org/problem?id=2762 题意:给出有向图,判断任意两个点u和v,是否可以从u到v或者从v到u. 思路: 判断图是否是单连通的. 首先来一遍强连通缩点,重新建立新图 ...

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

  6. POJ 2762 Going from u to v or from v to u?(强联通,拓扑排序)

    id=2762">http://poj.org/problem?id=2762 Going from u to v or from v to u? Time Limit: 2000MS ...

  7. [强连通分量] 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: 17089 ...

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

  9. POJ 2762 Going from u to v or from v to u? Tarjan算法 学习例题

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17104   Accepted: 4594 Description In o ...

随机推荐

  1. sizeToFit的用法和用途

    最近有遇到过sizeToFit的方法,比较好奇,所以查了点资料 在官方文档中 - (void)sizeToFit; // calls sizeThatFits: with current view b ...

  2. 在Linux下用netstat查看网络状态、端口状态

    在Linux下用netstat查看网络状态.端口状态 在linux一般使用netstat 来查看系统端口使用情况步. netstat命令是一个监控TCP/IP网络的非常有用的工具,它可以显示路由表.实 ...

  3. sqlserver 误删数据恢复

    ----创建存储过程 CREATE PROCEDURE Recover_Deleted_Data_Proc @Database_Name NVARCHAR(MAX) , @SchemaName_n_T ...

  4. 如何在安卓/data(而不是/data/data)目录下进行文件的读写操作

    分析:Android默认是无法直接操作/data目录的,只能读写程序自己的私有目录,也就是/data/data/package name/下,默认只能操作这个目录下的文件,也就是我们想直接读写/dat ...

  5. Log4j简单配置

    Log4j是一组强大的日志组件,在项目中时常需要用它提供一些信息,这两天学习了一下它的简单配置. 第一步,我们需要导入log4j-1.2.14.jar到lib目录下 第二步,在src下建立log4j. ...

  6. sqlserver时间字符串的截取

    昨天同学问了个sqlserver的问题,写了个简单的示例,如下: 问题:“15:00-16:30”拆分成“15:00-15:30”.“15:30-16:00”.“16:00-16:30”? 代码: d ...

  7. 文字排版--下划线(text-decoration:underline)

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  8. hdoj 1087 (DP)

    代码: #include<iostream>   #include<cmath>   using namespace std;  int a[1005], dp[1005];  ...

  9. 改进《完美让IE兼容input placeholder属性的jquery实现》的不完美

    <完美让IE兼容input placeholder属性的jquery实现>中的代码在IE9以下浏览器中会出错,原因是因为ie9以下的浏览器对input的标签的解释不同. 例如对以下文本框的 ...

  10. html 中 #include file 的用法

    有两个文件a.htm和b.htm,在同一目录下a.htm内容如下 <!-- #include file="b.htm" --> b.htm内容如下 今天:雨 31 ℃- ...