题目描述

给出 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. winserver2008,运行可执行文件,提示 激活上下文生成失败。 找不到从属程序集 Microsoft.VC90.DebugCRT,processorArchitecture="x86"

    首先我运行的可执行文件是在win7下使用vs2008生成的release版本的文件,为什么在运行的时候提示需要DebugCRT? 另外我在winserver2008上是安装了vc2008的运行环境的. ...

  2. 实用Photoshop快捷键

    面板快捷键:shift+对应的快捷键调用同类工具 Ctrl + 点击面板------获取选取 Shift + F6-----------羽化 Alt + Delete---------填充前景色 Ct ...

  3. Android 自定义Android带图片和文字的ImageButton

    经过分析,上述按钮效果实际上就是一个布局,一个最简单不过的垂直线性布局,上部分是一个ImageView,下部分是一个TextView,这个布局可点击.可设置监听. 我们首先要编写自己的ImageBut ...

  4. Android AIDL-跨进程

    Android在设计理念上强调组件化,组件之间的依赖性很小.我们往往发一个Intent请求就可以启动另一个应用的Activity,或者一个你不知道在哪个进程的Service,或者可以注册一个广播,只要 ...

  5. command-t插件使用说明

    类似于SourceInsight的Project Window,快速浏览项目里的文件 \t或:CommandT打开该插件 g:CommandTTraverseSCM设置搜索工程的根目录 tab在提示窗 ...

  6. apk反编译(1)用apktool破解apk

    1,下载 http://ibotpeaches.github.io/Apktool/ 2,破解 把下载的apktool_2.0.3.jar 和 weixin638android680.apk  拷贝到 ...

  7. 8天学通MongoDB——第四天 索引操作

    这些天项目改版,时间比较紧,博客也就没跟得上,还望大家见谅. 好,今天分享下mongodb中关于索引的基本操作,我们日常做开发都避免不了要对程序进行性能优化,而程序的操作无非就是CURD,通常我们 又 ...

  8. How can I add a new user as sudoer using the command line?

    Two ways to use sudo command for a standard user account: First, If you want to use sudo command for ...

  9. bzoj2431: [HAOI2009]逆序对数列

    dp. f[i][j]表示放置第i个数有j个逆序对的方案数. s[i][j]维护前缀和(f[i][0]~f[i][j]). 状态转移方程 f[i][j]=s[i-1][j]-s[i-1][max(j- ...

  10. NYOJ 536 开心的mdd【矩阵链乘】

    题意:给出n个矩阵组成的序列,问最少的运算量 看的紫书: dp[i][j]表示从第i个矩阵到第j个矩阵最少的乘法次数 dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j] ...