链接:

https://vjudge.net/problem/POJ-2553

题意:

We will use the following (standard) definitions from graph theory. Let V be a nonempty and finite set, its elements being called vertices (or nodes). Let E be a subset of the Cartesian product V×V, its elements being called edges. Then G=(V,E) is called a directed graph.

Let n be a positive integer, and let p=(e1,...,en) be a sequence of length n of edges ei∈E such that ei=(vi,vi+1) for a sequence of vertices (v1,...,vn+1). Then p is called a path from vertex v1 to vertex vn+1 in G and we say that vn+1 is reachable from v1, writing (v1→vn+1).

Here are some new definitions. A node v in a graph G=(V,E) is called a sink, if for every node w in G that is reachable from v, v is also reachable from w. The bottom of a graph is the subset of all nodes that are sinks, i.e., bottom(G)={v∈V|∀w∈V:(v→w)⇒(w→v)}. You have to calculate the bottom of certain graphs.

求哪些点能到的点都可以到自己

思路:

一个强连通分量内的点互相可达,所有一个强连通分量没有出度,则这个强连通内的点都满足条件。

代码:

#include <iostream>
#include <cstdio>
#include <vector>
#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
using namespace std;
const int MAXN = 5e3+10; vector<int> G[MAXN];
stack<int> St;
int Dfn[MAXN], Low[MAXN];
int Dis[MAXN], Vis[MAXN];
int Fa[MAXN];
int times, cnt;
int n, m; void Tarjan(int x)
{
Dfn[x] = Low[x] = ++times;
St.push(x);
Vis[x] = 1;
for (int i = 0;i < G[x].size();i++)
{
int nextnode = G[x][i];
if (Dfn[nextnode] == 0)
{
Tarjan(nextnode);
Low[x] = min(Low[x], Low[nextnode]);
}
else if (Vis[nextnode])
Low[x] = min(Low[x], Dfn[nextnode]);
}
if (Low[x] == Dfn[x])
{
cnt++;
while (St.top() != x)
{
Fa[St.top()] = cnt;
Vis[St.top()] = 0;
St.pop();
}
Fa[St.top()] = cnt;
Vis[St.top()] = 0;
St.pop();
}
} void Init()
{
memset(Dis, 0, sizeof(Dis));
memset(Vis, 0, sizeof(Vis));
memset(Dfn, 0, sizeof(Dfn));
for (int i = 1;i <= n;i++)
G[i].clear(), Fa[i] = i;
times = cnt = 0;
while (!St.empty())
St.pop();
} int main()
{
while (~scanf("%d", &n) && n)
{
Init();
scanf("%d", &m);
int l, r;
for (int i = 1;i <= m;i++)
{
scanf("%d%d", &l, &r);
G[l].push_back(r);
}
for (int i = 1;i <= n;i++)
if (Dfn[i] == 0)
Tarjan(i);
for (int i = 1;i <= n;i++)
{
for (int j = 0;j < G[i].size();j++)
{
int node = G[i][j];
if (Fa[i] != Fa[node])
Dis[Fa[i]]++;
}
}
int flag = 0;
for(int i=1;i<=n;i++)
{
if (Dis[Fa[i]] == 0)
{
if (!flag)
{
printf("%d", i);
flag = 1;
}
else printf(" %d", i);
}
}
printf("\n");
} return 0;
}

最新文章

  1. java关于ArrayList中toArray方法的使用
  2. iOS-即时通讯-环信
  3. 20145205《Java程序设计》课程总结
  4. 数位DP入门
  5. windows phone Datepicker Timepicker
  6. iOS - OC Category 分类
  7. MongoDB 基础
  8. 【Linux常用工具】02. 创建启动定时任务工具cron
  9. JavaScript触摸与手势事件
  10. C++基本要点复习--------coursera程序设计实习(PKU)的lecture notes
  11. 字符编码介绍及java中的应用
  12. Linux了解进程的地址空间
  13. Stylus-NodeJS下构建更富表现力/动态/健壮的CSS
  14. 201521123032 《Java程序设计》第3周学习总结
  15. Maven2插件开发入门
  16. 安全体系(一)—— DES算法详解
  17. dell-7559安装deepin15.8
  18. Application Constants
  19. Codeforces Round #550 (Div. 3) E. Median String (模拟)
  20. javascript解析器原理

热门文章

  1. APP自动化测试,判断页面与预期是否相同
  2. mysql双主双从技术
  3. python 并发编程 多线程 死锁现象与递归锁
  4. [转帖]X86_64平台上利用qemu安装aarch64架构的虚拟机
  5. awk 命令使用指南
  6. HDU-4332-Constructing Chimney
  7. qt对plot柱状图颜色设置
  8. MySQL数据库入门多实例配置
  9. 可以提升幸福感的js小技巧(下)
  10. js 学习二 字符串常用方法