题目描述

给出 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分。

  1. var f:array[..,..] of boolean;
  2. ff:packed array[..] of boolean;
  3. a:array[..] of longint;
  4. n,m,i,sum,y,z:longint;
  5.  
  6. function max(a,b:longint):longint;
  7. begin
  8. if a>b then exit(a) else exit(b);
  9. end;
  10.  
  11. procedure dfs(x:longint);
  12. var i:longint;
  13. begin
  14. ff[x]:=true;
  15. sum:=max(sum,x);
  16. for i:= to n do
  17. if (f[x,i])and(not ff[i]) then
  18. dfs(i);
  19. end;
  20.  
  21. procedure intt;
  22. begin
  23. assign(input,'graph.in');
  24. assign(output,'graph.out');
  25. reset(input);
  26. rewrite(output);
  27. end;
  28.  
  29. procedure outt;
  30. begin
  31. close(input);
  32. close(output);
  33. end;
  34.  
  35. begin
  36. //intt;
  37. writeln(sizeof(f) div div );
  38. fillchar(f,sizeof(f),false);
  39. readln(n,m);
  40. for i:= to m do
  41. begin
  42. readln(y,z);
  43. f[y,z]:=true;
  44. end;
  45. for i:= to n do
  46. begin
  47. fillchar
  48. (ff,sizeof(ff),false);
  49. sum:=;
  50. dfs(i);
  51. a[i]:=sum;
  52. end;
  53. for i:= to n do write(a[i],' ');
  54. //outt;
  55. end.

  如果可以用动态数组的话或许可以多卡几个点。

  用边表存储可以A掉。

  1. program df;
  2. var head,f:array[..]of longint;
  3. next,v:array[..]of longint;
  4. b:array[..]of boolean;
  5. n,i,m,j,a1,a2,max:longint;
  6. procedure dfs(a1:longint);
  7. var b1:longint;
  8. begin
  9. if b[a1]
  10. then exit;
  11. b[a1]:=true;
  12. f[a1]:=max;
  13. b1:=head[a1];
  14. while b1<> do
  15. begin
  16. dfs(v[b1]);
  17. b1:=next[b1];
  18. end;
  19. end;
  20. begin
  21. assign(input,'graph.in');
  22. assign(output,'graph.out');
  23. reset(input);
  24. rewrite(output);
  25. read(n,m);
  26. for i:= to m do
  27. begin
  28. read(a1,a2);
  29. next[i]:=head[a2];
  30. head[a2]:=i;
  31. v[i]:=a1;
  32. end;
  33. for i:=n downto do
  34. if not b[i]
  35. then
  36. begin
  37. max:=i;
  38. dfs(i);
  39. end;
  40. for i:= to n- do
  41. write(f[i],' ');
  42. writeln(f[n]);
  43. close(input);
  44. close(output);
  45. 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. 李洪强iOS开发之Foundation框架—集合

    Foundation框架—集合 一.NSArray和NSMutableArray (一)NSArray不可变数组 (1)NSArray的基本介绍 NSArray是OC中使用的数组,是面向对象的,以面向 ...

  2. Python批量读取人脸图片与数据互相转换

    读取部分结果 程序 # -*- coding: utf-8 -*- import numpy as np import matplotlib.pyplot as plt from PIL import ...

  3. java如何得到GET和POST请求URL和参数列表

    转载:http://blog.csdn.net/yaerfeng/article/details/18942739 在servlet中GET请求可以通过HttpServletRequest的getRe ...

  4. MIT算法导论——第四讲.Quicksort

    本栏目(Algorithms)下MIT算法导论专题是个人对网易公开课MIT算法导论的学习心得与笔记.所有内容均来自MIT公开课Introduction to Algorithms中Charles E. ...

  5. 使用WM_COPYDATA跨进程发送数据

    进程之间通讯的几种方法: 在Windows程序中,各个进程之间常常需要交换数据,进行数据通讯.常用的方法有 使用内存映射文件 通过共享内存DLL共享内存 使用SendMessage向另一进程发送WM_ ...

  6. Java String.compareTo()方法

    描述:java.lang.String.compareTo() 方法比较两个字符串的字典. 比较是基于字符串中的每个字符的Unicode值.此String对象表示的字符序列的 参数字符串表示的字符序列 ...

  7. Java API —— Calendar类

    1.Calendar类概述  Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR.MONTH.DAY_OF_MONTH.HOUR 等 日历字段之间的转换提供了一些方法,并为操作日历字段 ...

  8. python写的第一个简单小游戏-猜数字

    #Filename:game1.py guess=10 running=True while running: try: answer=int(raw_input('Guess what i thin ...

  9. POJ 1681 Painter's Problem (高斯消元 枚举自由变元求最小的步数)

    题目链接 题意: 一个n*n 的木板 ,每个格子 都 可以 染成 白色和黄色,( 一旦我们对也个格子染色 ,他的上下左右 都将改变颜色): 给定一个初始状态 , 求将 所有的 格子 染成黄色 最少需要 ...

  10. Fedora20 优化体验

    玩了些许天的fedora系统,到底是加深了对于linux系统的了解 为了便于大家对于fedora系统支持,我将这些天对于fedora的一些不适之处及改进的策略进行了一下小总结.便于新手对于fedora ...