[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 ...
随机推荐
- 李洪强iOS开发之Foundation框架—集合
Foundation框架—集合 一.NSArray和NSMutableArray (一)NSArray不可变数组 (1)NSArray的基本介绍 NSArray是OC中使用的数组,是面向对象的,以面向 ...
- Python批量读取人脸图片与数据互相转换
读取部分结果 程序 # -*- coding: utf-8 -*- import numpy as np import matplotlib.pyplot as plt from PIL import ...
- java如何得到GET和POST请求URL和参数列表
转载:http://blog.csdn.net/yaerfeng/article/details/18942739 在servlet中GET请求可以通过HttpServletRequest的getRe ...
- MIT算法导论——第四讲.Quicksort
本栏目(Algorithms)下MIT算法导论专题是个人对网易公开课MIT算法导论的学习心得与笔记.所有内容均来自MIT公开课Introduction to Algorithms中Charles E. ...
- 使用WM_COPYDATA跨进程发送数据
进程之间通讯的几种方法: 在Windows程序中,各个进程之间常常需要交换数据,进行数据通讯.常用的方法有 使用内存映射文件 通过共享内存DLL共享内存 使用SendMessage向另一进程发送WM_ ...
- Java String.compareTo()方法
描述:java.lang.String.compareTo() 方法比较两个字符串的字典. 比较是基于字符串中的每个字符的Unicode值.此String对象表示的字符序列的 参数字符串表示的字符序列 ...
- Java API —— Calendar类
1.Calendar类概述 Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR.MONTH.DAY_OF_MONTH.HOUR 等 日历字段之间的转换提供了一些方法,并为操作日历字段 ...
- python写的第一个简单小游戏-猜数字
#Filename:game1.py guess=10 running=True while running: try: answer=int(raw_input('Guess what i thin ...
- POJ 1681 Painter's Problem (高斯消元 枚举自由变元求最小的步数)
题目链接 题意: 一个n*n 的木板 ,每个格子 都 可以 染成 白色和黄色,( 一旦我们对也个格子染色 ,他的上下左右 都将改变颜色): 给定一个初始状态 , 求将 所有的 格子 染成黄色 最少需要 ...
- Fedora20 优化体验
玩了些许天的fedora系统,到底是加深了对于linux系统的了解 为了便于大家对于fedora系统支持,我将这些天对于fedora的一些不适之处及改进的策略进行了一下小总结.便于新手对于fedora ...