[GRYZ2015]Graph
题目描述
给出 N 个点,M 条边的有向图,对于每个点 v,求 A(v) 表示从点 v 出发,能到达的编号最大的点。
输入格式
第 1 行,2 个整数 N,M。 接下来 M 行,每行 2 个整数 Ui,Vi,表示边 ⟨Ui,Vi⟩。点用 1,2,...,N 编号。
输出格式
N 个整数 A(1),A(2),...,A(N)。
样例输入
4 3
1 2
2 4
4 3
样例输出
4 4 3 4
数据范围
对于 60% 的数据,1 ≤ N,K ≤ 10^3
对于 100% 的数据,1 ≤ N,M ≤ 10^5。
思路
图的遍历。DFS。
用邻接矩阵不开动态数组60分。
var f:array[..,..] of boolean;
ff:packed array[..] of boolean;
a:array[..] of longint;
n,m,i,sum,y,z:longint; function max(a,b:longint):longint;
begin
if a>b then exit(a) else exit(b);
end; procedure dfs(x:longint);
var i:longint;
begin
ff[x]:=true;
sum:=max(sum,x);
for i:= to n do
if (f[x,i])and(not ff[i]) then
dfs(i);
end; procedure intt;
begin
assign(input,'graph.in');
assign(output,'graph.out');
reset(input);
rewrite(output);
end; procedure outt;
begin
close(input);
close(output);
end; begin
//intt;
writeln(sizeof(f) div div );
fillchar(f,sizeof(f),false);
readln(n,m);
for i:= to m do
begin
readln(y,z);
f[y,z]:=true;
end;
for i:= to n do
begin
fillchar
(ff,sizeof(ff),false);
sum:=;
dfs(i);
a[i]:=sum;
end;
for i:= to n do write(a[i],' ');
//outt;
end.
如果可以用动态数组的话或许可以多卡几个点。
用边表存储可以A掉。
program df;
var head,f:array[..]of longint;
next,v:array[..]of longint;
b:array[..]of boolean;
n,i,m,j,a1,a2,max:longint;
procedure dfs(a1:longint);
var b1:longint;
begin
if b[a1]
then exit;
b[a1]:=true;
f[a1]:=max;
b1:=head[a1];
while b1<> do
begin
dfs(v[b1]);
b1:=next[b1];
end;
end;
begin
assign(input,'graph.in');
assign(output,'graph.out');
reset(input);
rewrite(output);
read(n,m);
for i:= to m do
begin
read(a1,a2);
next[i]:=head[a2];
head[a2]:=i;
v[i]:=a1;
end;
for i:=n downto do
if not b[i]
then
begin
max:=i;
dfs(i);
end;
for i:= to n- do
write(f[i],' ');
writeln(f[n]);
close(input);
close(output);
end.
强连通分量
拓扑排序
DP/BFS
同样可以求解此题。
[GRYZ2015]Graph的更多相关文章
- [开发笔记] Graph Databases on developing
TimeWall is a graph databases github It be used to apply mathematic model and social network with gr ...
- Introduction to graph theory 图论/脑网络基础
Source: Connected Brain Figure above: Bullmore E, Sporns O. Complex brain networks: graph theoretica ...
- POJ 2125 Destroying the Graph 二分图最小点权覆盖
Destroying The Graph Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8198 Accepted: 2 ...
- [LeetCode] Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- [LeetCode] Graph Valid Tree 图验证树
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- [LeetCode] Clone Graph 无向图的复制
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...
- 讲座:Influence maximization on big social graph
Influence maximization on big social graph Fanju PPT链接: social influence booming of online social ne ...
- zabbix利用api批量添加item,并且批量配置添加graph
关于zabbix的API见,zabbixAPI 1item批量添加 我是根据我这边的具体情况来做的,本来想在模板里面添加item,但是看了看API不支持,只是支持在host里面添加,所以我先在一个ho ...
- Theano Graph Structure
Graph Structure Graph Definition theano's symbolic mathematical computation, which is composed of: A ...
随机推荐
- 检测系统是X86系统,还是X64系统
function IsWin64: Boolean; var Kernel32Handle: THandle; IsWow64Process: function(Handle: Windows.THa ...
- esriFeatureType与esriGeometryType的区别与联系
esriFeatureType通常用来表示数据的存储结构,即物理层: esriGeometryType通常用来表示数据的几何形状,即表现层. esriGeometryType枚举类型详解 常量 值 对 ...
- JAX-RS 方式的 RESTful Web Service 开发
JAX-RS 方式的 RESTful Web Service 开发 ——基于 CXF+Spring 的实现 Web Service 目前在风格上有两大类,一个是基于 SOAP 协议,一个是完全遵循 H ...
- HDU5088——Revenge of Nim II(高斯消元&矩阵的秩)(BestCoder Round #16)
Revenge of Nim II Problem DescriptionNim is a mathematical game of strategy in which two players tak ...
- SPOJ 1435 Vertex Cover 树形DP
i 表示节点 i ,j=0表示不选择其父节点,j=1表示选择其父节点.f 为其父节点. 取 每个节点选择/不选择 两者中较小的那个. 一组数据: 151 21 31 41 1010 910 1112 ...
- Spring Data JPA教程, 第三部分: Custom Queries with Query Methods(翻译)
在本人的Spring Data JPA教程的第二部分描述了如何用Spring Data JPA创建一个简单的CRUD应用,本博文将描述如何在Spring Data JPA中使用query方法创建自定义 ...
- ubuntu install rpm package
Using command 'alien' instead of 'rpm'. sudo apt-get install alien alien -i tst.rpm 'man alien' for ...
- Android开发之事件分发和Listener
参考:http://blog.csdn.net/zhongkejingwang/article/details/38141719 http://blog.csdn.net/zhongkejingwan ...
- 函数 page_dir_get_n_heap
查看某page中含有的记录个数 #define PAGE_N_HEAP 4 /* number of records in the heap, bit =flag: new-style compact ...
- UVa 11330 (置换 循环的分解) Andy's Shoes
和UVa11077的分析很类似. 我们固定左脚的鞋子不动,然后将右脚的鞋子看做一个置换分解. 对于一个长度为l的循环节,要交换到正确位置至少要交换l-1次. #include <cstdio&g ...