[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 ...
随机推荐
- dbgrid显示access备注信息
procedure TfrmAllFind.DBGrid6DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; C ...
- lintcode :链表插入排序
题目: 链表插入排序 用插入排序对链表排序 样例 Given 1->3->2->0->null, return 0->1->2->3->null 解题: ...
- [hackerrank]Closest Number
https://www.hackerrank.com/contests/w5/challenges/closest-number 简单题. #include <iostream> #inc ...
- 入门视频采集与处理(BT656简介)
入门视频采集与处理(BT656简介) http://ticktick.blog.51cto.com/823160/553535 1. 帧的概念(Frame) 一个视频序列是由N个帧组成的,采集图像的 ...
- darwin转发时,摄像机在3G和4G模式下的参数设置
darwin转发时,摄像机在3G和4G模式下的参数设置 我们转发的是摄像机的子码流,因为在不同的网络环境下,为了达到当前网络环境下最清晰,最流畅的目标,在转发前要根据使用的是3G还是4G及信号强度来自 ...
- SQLServer中服务器角色和数据库角色权限详解
角色 当几个用户需要在某个特定的数据库中执行类似的动作时(这里没有相应的Windows用户组),就可以向该数据库中添加一个角色(role).数据库角色指定了可以访问相同数据库对象的一组数据库用户. 数 ...
- Eclipse项目和MyEclipse项目
因为Eclipse的项目结构和MyEclipse项目的结构不同,所以两者的项目之间不能直接运行的. 我们在创建Eclipse项目的时候可以进行一些设置,这样在Eclipse中创建的项目可以直接在MyE ...
- 72. Edit Distance
题目: Given two words word1 and word2, find the minimum number of steps required to convert word1 to w ...
- Android给listview的item设定高度
在item的layout文件中,用android:layout_height设置item的高度.运行,高度设置无效. 解决办法: 给item设定minHeight,即可. -------------- ...
- Android sendMessage 与 obtainMessage (sendToTarget)比较
话说在工作中第一次接触android 的Handler 的时候,不知道怎么去关注性能. 记得当时这么写的: Message msg = new Message() msg.what = xxx; ms ...