POJ 2762 Going from u to v or from v to u?(强联通 + 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)的更多相关文章
- 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?(强连通分量+拓扑排序)
职务地址:id=2762">POJ 2762 先缩小点.进而推断网络拓扑结构是否每个号码1(排序我是想不出来这点的. .. ).由于假如有一层为2的话,那么从此之后这两个岔路的点就不可 ...
- 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 ...
- 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 ...
- [强连通分量] 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 ...
- 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 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 ...
随机推荐
- 事件触发函数中的this,target,currentTarget,srcElement
要解释其中的区别,首先要理解浏览器的事件机制, 现在主流的浏览器事件基本是先捕获再冒泡,IE浏览器只有冒泡阶段 事件是在冒泡阶段触发的 看看这个HTML 当我点击"我是父节点的时候" ...
- Java 406
项目改名之后, 项目上传后,报错,406,可是项目本地是可以跑起来的, 联系管理员,管理员改了个/etc/httpd/conf/workers2.properties 里面,将本次的项目加入进去就OK ...
- C# 里窗体里(windows form)怎么播放音乐
在.NET的winform里面,没有托管的音乐播放器,API只能播放WAV格式,对于MP3等形式的音频文件,就要依赖于 MediaPlayer里,嘿嘿 使用的方法: 在toolbox上点右键,选择“选 ...
- SGU 190.Dominoes(二分图匹配)
时间限制:0.25s 空间限制:4M 题意: 给定一个N*N的棋盘,一些格子被移除,在棋盘上放置一些1*2的骨牌,判定能否放满,并且输出任意方案. Solution: 首先考虑对棋盘的一个格子黑白染色 ...
- ajax 文件上传,ajax
ajax 文件上传,ajax 啥也不说了,直接上代码! <input type="file" id="file" name="myfile&qu ...
- php 文件上传后缀名与文件类型对照表(几乎涵盖所有文件)
网上有很多php文件上传的类,文件上传处理是php的一个特色(至少手册上是将此作为php特点来展示的,个人认为php在数组方面的优异功能更有特 色),学php的人都知道文件上传怎么做,但很多人在编程中 ...
- Python核心编程2第六章课后练习
6-1 字符串 .string 模块中是否有一种字符串方法或者函数可以帮我鉴定一下一个字符串是否是另一个大字符串的一部分? #!/usr/bin/env python def contain(str1 ...
- js的引用顺序
注意:Bootstrap中的JS插件依赖于JQuery,因此JQuery要在Bootstrap之前引用!!! 把JS文件引用放入body的最下面,是为了使js在网页全部加载完后才起作用,比如你的js里 ...
- Vue 2.0基础
我们将会选择使用一些vue周边的库vue-cli, vue-router,vue-resource,vuex 1.使用vue-cli创建项目2.使用vue-router实现单页路由3.用vuex管理我 ...
- while死循环问题-输入字符就会死循环
问题: 是否会遇到这样的问题,在while循环中 sanf("%d",&a);如果输入的不是数字,是字符就会进入死循环. 解决方案:都是缓冲区惹的祸,输入字符后,字符会一直 ...