题目描述

给出 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的更多相关文章

  1. [开发笔记] Graph Databases on developing

    TimeWall is a graph databases github It be used to apply mathematic model and social network with gr ...

  2. Introduction to graph theory 图论/脑网络基础

    Source: Connected Brain Figure above: Bullmore E, Sporns O. Complex brain networks: graph theoretica ...

  3. POJ 2125 Destroying the Graph 二分图最小点权覆盖

    Destroying The Graph Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8198   Accepted: 2 ...

  4. [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), ...

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

  6. [LeetCode] Clone Graph 无向图的复制

    Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...

  7. 讲座:Influence maximization on big social graph

    Influence maximization on big social graph Fanju PPT链接: social influence booming of online social ne ...

  8. zabbix利用api批量添加item,并且批量配置添加graph

    关于zabbix的API见,zabbixAPI 1item批量添加 我是根据我这边的具体情况来做的,本来想在模板里面添加item,但是看了看API不支持,只是支持在host里面添加,所以我先在一个ho ...

  9. Theano Graph Structure

    Graph Structure Graph Definition theano's symbolic mathematical computation, which is composed of: A ...

随机推荐

  1. XML DOS 攻击

    内容来自此文:http://msdn.microsoft.com/en-us/magazine/ee335713.aspx DoS(Denial of service:拒绝服务)攻击由来已久(1992 ...

  2. Java并发:Callable、Future和FutureTask

    Java并发编程:Callable.Future和FutureTask 在前面的文章中我们讲述了创建线程的2种方式,一种是直接继承Thread,另外一种就是实现Runnable接口. 这2种方式都有一 ...

  3. Spring AOP: Spring之面向方面编程

    Spring AOP: Spring之面向方面编程 面向方面编程 (AOP) 提供从另一个角度来考虑程序结构以完善面向对象编程(OOP). 面向对象将应用程序分解成 各个层次的对象,而AOP将程序分解 ...

  4. Android LayoutInflater.from(context).inflate

    在应用中自定义一个view,需要获取这个view的布局,需要用到 (LinearLayout) LayoutInflater.from(context).inflate(R.layout.conten ...

  5. NET Remoting 示例

    .NET Remoting是.NET平台上允许存在于不同应用程序域中的对象相互知晓对方并进行通讯的基础设施.调用对象被称为客户端,而被调用对象则被称为服务器或者服务器对象.简而言之,它就是.NET平台 ...

  6. c# webbrowser 随机点击链接

    HtmlElementCollection hec = webBrowser1.Document.All; ; i < hec.Count; i++) { if (hec[i].GetAttri ...

  7. 函数flst_remove

    移除 node, node->prev直接指向node->next /*********************************************************** ...

  8. [LA 3887] Slim Span

    3887 - Slim SpanTime limit: 3.000 seconds Given an undirected weighted graph G <tex2html_verbatim ...

  9. 纯CSS3带动画效果导航菜单

    随着互联网的发展,网页能表现的东西越来越多.由最开始单纯的文字和链接构成的网页,到后来的表格布局,再到div+css模式,现在发展到了html+css3.网页能表达的东西越来越多,css3兴起已经很多 ...

  10. CImage 获取图片RGB 、图片高和宽;

    1 CImage img , img1 ,imDest; 2 img1.Load( 图片路径); 3 img.Load( 图片路径); 4 为了防止图片失真,先处理一下在把图片显示出来 5 SetSt ...