http://www.lydsy.com/JudgeOnline/problem.php?id=3887||

https://www.luogu.org/problem/show?pid=3119

Description

In an effort to better manage the grazing patterns of his cows, Farmer John has installed one-way cow paths all over his farm. The farm consists of N fields, conveniently numbered 1..N, with each one-way cow path connecting a pair of fields. For example, if a path connects from field X to field Y, then cows are allowed to travel from X to Y but not from Y to X. Bessie the cow, as we all know, enjoys eating grass from as many fields as possible. She always starts in field 1 at the beginning of the day and visits a sequence of fields, returning to field 1 at the end of the day. She tries to maximize the number of distinct fields along her route, since she gets to eat the grass in each one (if she visits a field multiple times, she only eats the grass there once). As one might imagine, Bessie is not particularly happy about the one-way restriction on FJ's paths, since this will likely reduce the number of distinct fields she can possibly visit along her daily route. She wonders how much grass she will be able to eat if she breaks the rules and follows up to one path in the wrong direction. Please compute the maximum number of distinct fields she can visit along a route starting and ending at field 1, where she can follow up to one path along the route in the wrong direction. Bessie can only travel backwards at most once in her journey. In particular, she cannot even take the same path backwards twice.
给一个有向图,然后选一条路径起点终点都为1的路径出来,有一次机会可以沿某条边逆方向走,问最多有多少个点可以被经过?(一个点在路径中无论出现多少正整数次对答案的贡献均为1)

Input

The first line of input contains N and M, giving the number of fields and the number of one-way paths (1 <= N, M <= 100,000). The following M lines each describe a one-way cow path. Each line contains two distinct field numbers X and Y, corresponding to a cow path from X to Y. The same cow path will never appear more than once.

Output

A single line indicating the maximum number of distinct fields Bessie
can visit along a route starting and ending at field 1, given that she can
follow at most one path along this route in the wrong direction.
 

Sample Input

7 10
1 2
3 1
2 5
2 4
3 7
3 5
3 6
6 5
7 2
4 7

Sample Output

6

HINT

 

Source

Gold&鸣谢18357

先把原图缩点,跑出从1到n和从n到1的最多可以遍历的牧场数,

枚举每个边做无向边的情况更新答案

SPFA跑最多牧场数

  1. #include <cstdio>
  2. #include <queue>
  3. #define min(a,b) (a<b?a:b)
  4. #define max(a,b) (a>b?a:b)
  5.  
  6. const int INF(0x3f3f3f3f);
  7. const int N(1e5+);
  8. int n,head[N],sumedge;
  9. struct Edge
  10. {
  11. int v,next;
  12. Edge(int v=,int next=):v(v),next(next){}
  13. }edge[N];
  14. inline void ins(int u,int v)
  15. {
  16. edge[++sumedge]=Edge(v,head[u]);
  17. head[u]=sumedge;
  18. }
  19.  
  20. int tim,dfn[N],low[N];
  21. int top,Stack[N],instack[N];
  22. int sumcol,col[N],point[N];
  23. void DFS(int u)
  24. {
  25. low[u]=dfn[u]=++tim;
  26. Stack[++top]=u; instack[u]=;
  27. for(int v,i=head[u];i;i=edge[i].next)
  28. {
  29. v=edge[i].v;
  30. if(!dfn[v]) DFS(v),low[u]=min(low[u],low[v]);
  31. else if(instack[v]) low[u]=min(low[u],dfn[v]);
  32. }
  33. if(low[u]==dfn[u])
  34. {
  35. col[u]=++sumcol;
  36. point[sumcol]++;
  37. for(;Stack[top]!=u;top--)
  38. {
  39. point[sumcol]++;
  40. col[Stack[top]]=sumcol;
  41. instack[Stack[top]]=;
  42. }
  43. instack[u]=; top--;
  44. }
  45. }
  46.  
  47. int hed[N],had[N],sum;
  48. struct E
  49. {
  50. int v,next,w;
  51. E(int v=,int next=,int w=):v(v),next(next),w(w){}
  52. }e[N][];
  53. inline void insert(int u,int v)
  54. {
  55. e[++sum][]=E(v,hed[u],point[v]);
  56. hed[u]=sum;
  57. e[sum][]=E(u,had[v],point[u]);
  58. had[v]=sum;
  59. }
  60.  
  61. bool inq[N];
  62. int v1[N],v2[N];
  63. void SPFA(int op,int s,int *val,int *head)
  64. {
  65. for(int i=;i<=sumcol;i++)
  66. inq[i]=,val[i]=-INF;
  67. val[s]=point[s];
  68. std::queue<int>que;
  69. que.push(s);
  70. for(int u,v;!que.empty();)
  71. {
  72. u=que.front(); que.pop(); inq[u]=;
  73. for(int i=head[u];i;i=e[i][op].next)
  74. {
  75. v=e[i][op].v;
  76. if(val[v]<val[u]+e[i][op].w)
  77. {
  78. val[v]=val[u]+e[i][op].w;
  79. if(!inq[v]) inq[v]++,que.push(v);
  80. }
  81. }
  82. }
  83. }
  84.  
  85. inline void read(int &x)
  86. {
  87. x=; register char ch=getchar();
  88. for(;ch>''||ch<'';) ch=getchar();
  89. for(;ch>=''&&ch<='';ch=getchar()) x=x*+ch-'';
  90. }
  91.  
  92. int AC()
  93. {
  94. int m; read(n),read(m);
  95. for(int u,v;m--;)
  96. read(u),read(v),ins(u,v);
  97. for(int i=;i<=n;i++)
  98. if(!dfn[i]) DFS(i);
  99. for(int v,u=;u<=n;u++)
  100. for(int i=head[u];i;i=edge[i].next)
  101. {
  102. v=edge[i].v;
  103. if(col[u]!=col[v]) insert(col[u],col[v]);
  104. }
  105. int ans=-INF;
  106. SPFA(,col[],v1,hed);
  107. SPFA(,col[],v2,had);
  108. for(int v,u=;u<=sum;u++)
  109. for(int i=hed[u];i;i=e[i][].next)
  110. {
  111. v=e[i][].v;
  112. ans=max(ans,v1[v]+v2[u]);
  113. }
  114. for(int v,u=;u<=sum;u++)
  115. for(int i=had[u];i;i=e[i][].next)
  116. {
  117. v=e[i][].v;
  118. ans=max(ans,v1[u]+v2[v]);
  119. }
  120. printf("%d\n",ans-point[col[]]);
  121. return ;
  122. }
  123.  
  124. int Hope=AC();
  125. int main(){;}

SPFA AC

Topsort跑最多牧场数

  1. #include <cstdio>
  2. #include <queue>
  3. #define min(a,b) (a<b?a:b)
  4. #define max(a,b) (a>b?a:b)
  5.  
  6. const int INF(0x3f3f3f3f);
  7. const int N(1e5+);
  8. int n,head[N],sumedge;
  9. struct Edge
  10. {
  11. int v,next;
  12. Edge(int v=,int next=):v(v),next(next){}
  13. }edge[N];
  14. inline void ins(int u,int v)
  15. {
  16. edge[++sumedge]=Edge(v,head[u]);
  17. head[u]=sumedge;
  18. }
  19.  
  20. int tim,dfn[N],low[N];
  21. int top,Stack[N],instack[N];
  22. int sumcol,col[N],point[N],rd[N],cd[N];
  23. void DFS(int u)
  24. {
  25. low[u]=dfn[u]=++tim;
  26. Stack[++top]=u; instack[u]=;
  27. for(int v,i=head[u];i;i=edge[i].next)
  28. {
  29. v=edge[i].v;
  30. if(!dfn[v]) DFS(v),low[u]=min(low[u],low[v]);
  31. else if(instack[v]) low[u]=min(low[u],dfn[v]);
  32. }
  33. if(low[u]==dfn[u])
  34. {
  35. col[u]=++sumcol;
  36. point[sumcol]++;
  37. for(;Stack[top]!=u;top--)
  38. {
  39. point[sumcol]++;
  40. col[Stack[top]]=sumcol;
  41. instack[Stack[top]]=;
  42. }
  43. instack[u]=; top--;
  44. }
  45. }
  46.  
  47. int hed[N],had[N],sum;
  48. struct E
  49. {
  50. int v,next,w;
  51. E(int v=,int next=,int w=):v(v),next(next),w(w){}
  52. }e[N][];
  53. inline void insert(int u,int v)
  54. {
  55. e[++sum][]=E(v,hed[u],point[v]);
  56. hed[u]=sum;
  57. e[sum][]=E(u,had[v],point[u]);
  58. had[v]=sum;
  59. }
  60.  
  61. int v1[N],v2[N];
  62. #define max(a,b) (a>b?a:b)
  63. void Topsort(int op,int s,int *val,int *head,int *du)
  64. {
  65. std::queue<int>que;
  66. for(int i=;i<=sumcol;i++)
  67. {
  68. if(!du[i]) que.push(i);
  69. val[i]=-INF;
  70. }
  71. val[s]=point[s];
  72. for(int u,v;!que.empty();)
  73. {
  74. u=que.front(); que.pop();
  75. for(int i=head[u];i;i=e[i][op].next)
  76. {
  77. v=e[i][op].v;
  78. val[v]=max(val[v],val[u]+e[i][op].w);
  79. if(--du[v]==) que.push(v);
  80. }
  81. }
  82. }
  83.  
  84. inline void read(int &x)
  85. {
  86. x=; register char ch=getchar();
  87. for(;ch>''||ch<'';) ch=getchar();
  88. for(;ch>=''&&ch<='';ch=getchar()) x=x*+ch-'';
  89. }
  90.  
  91. int AC()
  92. {
  93. int m; read(n),read(m);
  94. for(int u,v;m--;)
  95. read(u),read(v),ins(u,v);
  96. for(int i=;i<=n;i++)
  97. if(!dfn[i]) DFS(i);
  98. for(int v,u=;u<=n;u++)
  99. for(int i=head[u];i;i=edge[i].next)
  100. {
  101. v=edge[i].v;
  102. if(col[u]==col[v]) continue;
  103. rd[col[v]]++,cd[col[u]]++;
  104. insert(col[u],col[v]);
  105. }
  106. int ans=-INF;
  107. Topsort(,col[],v1,hed,rd);
  108. Topsort(,col[],v2,had,cd);
  109. for(int v,u=;u<=sum;u++)
  110. for(int i=hed[u];i;i=e[i][].next)
  111. {
  112. v=e[i][].v;
  113. ans=max(ans,v1[v]+v2[u]);
  114. }
  115. for(int v,u=;u<=sum;u++)
  116. for(int i=had[u];i;i=e[i][].next)
  117. {
  118. v=e[i][].v;
  119. ans=max(ans,v1[u]+v2[v]);
  120. }
  121. printf("%d\n",ans-point[col[]]);
  122. return ;
  123. }
  124.  
  125. int Hope=AC();
  126. int main(){;}

Topsort AC

洛谷—— P3119 [USACO15JAN]草鉴定Grass Cownoisseur || BZOJ——T 3887: [Usaco2015 Jan]Grass Cownoisseur的更多相关文章

  1. 洛谷 P3119 [USACO15JAN]草鉴定Grass Cownoisseur 解题报告

    P3119 [USACO15JAN]草鉴定Grass Cownoisseur 题目描述 约翰有\(n\)块草场,编号1到\(n\),这些草场由若干条单行道相连.奶牛贝西是美味牧草的鉴赏家,她想到达尽可 ...

  2. 洛谷——P3119 [USACO15JAN]草鉴定Grass Cownoisseur

    P3119 [USACO15JAN]草鉴定Grass Cownoisseur 题目描述 In an effort to better manage the grazing patterns of hi ...

  3. 洛谷 P3119 [USACO15JAN]草鉴定Grass Cownoisseur (SCC缩点,SPFA最长路,枚举反边)

    P3119 [USACO15JAN]草鉴定Grass Cownoisseur 题目描述 In an effort to better manage the grazing patterns of hi ...

  4. 洛谷 P3119 [USACO15JAN]草鉴定Grass Cownoisseur

    屠龙宝刀点击就送 Tarjan缩点+拓扑排序 以后缩点后建图看n范围用vector ,或者直接用map+vector 结构体里数据要清空 代码: #include <cstring> #i ...

  5. 洛谷P3119 USACO15JAN 草鉴定

    题目描述 In an effort to better manage the grazing patterns of his cows, Farmer John has installed one-w ...

  6. 洛谷3119 [USACO15JAN]草鉴定Grass Cownoisseur

    原题链接 显然一个强连通分量里所有草场都可以走到,所以先用\(tarjan\)找强连通并缩点. 对于缩点后的\(DAG\),先复制一张新图出来,然后对于原图中的每条边的终点向新图中该边对应的那条边的起 ...

  7. P3119 [USACO15JAN]草鉴定Grass Cownoisseur

    题目描述 In an effort to better manage the grazing patterns of his cows, Farmer John has installed one-w ...

  8. luogu P3119 [USACO15JAN]草鉴定Grass Cownoisseur

    题目描述 In an effort to better manage the grazing patterns of his cows, Farmer John has installed one-w ...

  9. P3119 [USACO15JAN]草鉴定Grass Cownoisseur 分层图或者跑两次最长路

    https://www.luogu.org/problemnew/show/P3119 题意 有一个有向图,允许最多走一次逆向的路,问从1再走回1,最多能经过几个点. 思路 (一)首先先缩点.自己在缩 ...

随机推荐

  1. Android+Jquery Mobile学习系列(1)-开发环境

    开发环境是老生常谈的问题了,网上有很多关于Android环境安装的文章,我这里也就简单说明一下,不做过多分析. 想了解详细的安装说明,可以参见[百度经验] Java环境安装直接跳过,说一下Androi ...

  2. 南海区行政审批管理系统接口规范v0.3(规划) 2.业务申报API 2.1.businessApply【业务申报】

    {"v_interface":"2015987654327","c_project":"NH09A102"," ...

  3. 前端分页功能实现(PC)

    <!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>加 ...

  4. netcore发布到centos 验证码Zkweb.system.drawing不显示及乱码的问题

    netcore发布到centos 使用的是Zkweb.system.drawing生成验证码,发布后可能会出现不显示及乱码的情况 1.验证码图片不显示(通过日志会发现生成图片时代码已经异常) Zkwe ...

  5. Blender Python UV 学习

    Blender Python UV 学习 1. bmesh面转换 bm = bmesh.from_edit_mesh(bpy.context.edit_object.data) bm.faces.en ...

  6. LeetCode Weekly Contest 24

    1, 543. Diameter of Binary Tree 维护左右子树的高度,同时更新结果,返回以该节点结束的最大长度.递归调用. /** * Definition for a binary t ...

  7. Java图片的压缩

    1.如果在springMvc中,会自带生成MultipartFile文件,将MultipartFile转化为File MultipartFile file1 = file; CommonsMultip ...

  8. ASCII和ASCII扩展表

  9. hiho一下 第172周

    题目1 : Matrix Sum 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 You are given an N × N matrix. At the beginn ...

  10. js手机网络检测

    <!DOCTYPE HTML> <html lang="en"> <head> <meta charset=UTF-8"> ...