题目描述

给出 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. 只有innoDB才允许使用外键

    1.只有InnoDB引擎才允许使用外键,所以,我们的数据表必须使用InnoDB引擎. 2.注意: 1.必须使用InnoDB引擎: 2.外键必须建立索引(INDEX): 3.外键绑定关系这里使用了“ O ...

  2. Shell 是个什么玩意

    Shell的定义: 计算机分为软件和硬件,管理计算机硬件的是操作系统,也就是我们说的kernel,而这个核心是需要被保护起来的. 那我们如何通过软件与硬件进行沟通,让硬件执行我们要完成的指令呢? 这个 ...

  3. 8、双向一对多的关联关系(等同于双向多对一。1的一方有对n的一方的集合的引用,同时n的一方有对1的一方的引用)

    双向一对多关联关系 “双向一对多关联关系”等同于“双向多对一关联关系”:1的一方有对n的一方的集合的引用,同时n的一方有对1的一方的引用. 还是用客户Customer和订单Order来解释: “一对多 ...

  4. Intellij IDEA 快速创建Spring Web 项目

    相关软件: Intellij Idea14:http://pan.baidu.com/s/1nu16VyD JDK7:http://pan.baidu.com/s/1dEstJ5f Tomcat(ap ...

  5. jpa-hibernate配置转换

    persistence.xml spring-demo-cfg.xml hibernate配置文件 Spring Data JPA:为简化JPA的开发提供帮助.EntityManager的创建与销毁. ...

  6. github的large file storeage

    https://git-lfs.github.com/ 1.从这个网址下载git-lfs-windows-amd64-1.1.0.exe,运行这个安装包 2.然后打开git bash 输入git lf ...

  7. ios 开发中 developer tools access 总是要输入密码问题的解决

    我一直没有想法去解决这个问题:打开iphone模拟器的时候,老是弹出developer tools access 让我输入密码, 今天我在打开模拟器的时候又弹出这个对话框,我愤怒了,于是我在网上查了一 ...

  8. Struts2系列——struts2的result

    在action的指定方法执行完毕后总会返回一个字符串,struts2根据返回的字符串去action的配置中的result去找匹配的名字,根据配置执行下一步的操作. 在ActionSupport基类中定 ...

  9. hdu 4674 Trip Advisor(缩点+倍增lca)

    花了一天半的时间,才把这道题ac= = 确实是道好题,好久没敲这么长的code了,尤其是最后的判定,各种销魂啊~ 题目中给出的条件最值得关注的就是:每个点最多只能在一个环内->原图是由一个个边连 ...

  10. codeforces 333B - Chips

    注意:横向纵向交叉时,只要两条边不是正中的边(当n&1!=1),就可以余下两个chip. 代码里数组a[][]第二维下标 0表示横向边,1表示纵向边. #include<stdio.h& ...