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 ...
随机推荐
- Android开发报错系列(一),java.lang.NullPointerException,at android.widget.ListView.setupChild
问题描述:运行代码是报空指针错误,java.lang.NullPointerException,at Android.widget.ListView.setupChild 问题定位:listview控 ...
- EXCEL插件
http://www.cnblogs.com/brooks-dotnet/category/233027.html http://www.360doc.com/content/15/0713/00/1 ...
- wpf提示背景,资源样式
查找资源时多用element.TryFindResource() <TextBox FontSize="17" Height="26" Margin=&q ...
- win 10 安装 mysql解压版 步骤
参考资料:win 10 安装 mysql 5.7 网址:http://blog.sina.com.cn/s/blog_5f39af320102wbk0.html 本文参考上面的网址的教程,感谢作者分享 ...
- Quartz Quick Start Guide
Welcome to the QuickStart guide for Quartz. As you read this guide, expect to see details of: Downlo ...
- Java中View游戏开发框架
java中游戏开发引擎View比较适合被动触发的游戏,不能使用于那种对战的游戏 Game01Activity.java 这里是调用的activity package cn.sun.syspro; i ...
- WPF FindName()查找命名注册的元素
一.查找xaml中命名注册的元素 <Button x:Name="btn1" Content="显示内容" HorizontalAlignment=&qu ...
- Creating a web application.
About creating web GIS applications As you learn and use ArcGIS for Server, you'll probably reach th ...
- .NET 4.6
http://referencesource.microsoft.com/ DownLoad 下载原代码
- 在Iframe框架下如何跳转到登录界面
在Iframe框架下跳转到登录界面总会跳到子界面中,类似于下图 试用Respon.Redirect()不行, 用Js函数,但我跳转代码都是写在cs文件中的,用Respose.write(),js函数根 ...