洛谷—— P3119 [USACO15JAN]草鉴定Grass Cownoisseur || BZOJ——T 3887: [Usaco2015 Jan]Grass Cownoisseur...

news/2024/8/26 9:27:30 标签: php

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+5);
  8 int n,head[N],sumedge;
  9 struct Edge
 10 {
 11     int v,next;
 12     Edge(int v=0,int next=0):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]=1;
 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]]=0;
 42         }
 43         instack[u]=0; top--;
 44     }
 45 }
 46 
 47 int hed[N],had[N],sum;
 48 struct E
 49 {
 50     int v,next,w;
 51     E(int v=0,int next=0,int w=0):v(v),next(next),w(w){}
 52 }e[N][2];
 53 inline void insert(int u,int v)
 54 {
 55     e[++sum][0]=E(v,hed[u],point[v]);
 56     hed[u]=sum;
 57     e[sum][1]=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=1;i<=sumcol;i++)
 66         inq[i]=0,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]=0;
 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=0; register char ch=getchar();
 88     for(;ch>'9'||ch<'0';) ch=getchar();
 89     for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
 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=1;i<=n;i++)
 98         if(!dfn[i]) DFS(i);
 99     for(int v,u=1;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(0,col[1],v1,hed);
107     SPFA(1,col[1],v2,had);
108     for(int v,u=1;u<=sum;u++)
109         for(int i=hed[u];i;i=e[i][0].next)
110         {
111             v=e[i][0].v;
112             ans=max(ans,v1[v]+v2[u]);
113         }
114     for(int v,u=1;u<=sum;u++)
115         for(int i=had[u];i;i=e[i][1].next)
116         {
117             v=e[i][1].v;
118             ans=max(ans,v1[u]+v2[v]);
119         }
120     printf("%d\n",ans-point[col[1]]);
121     return 0;
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+5);
  8 int n,head[N],sumedge;
  9 struct Edge
 10 {
 11     int v,next;
 12     Edge(int v=0,int next=0):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]=1;
 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]]=0;
 42         }
 43         instack[u]=0; top--;
 44     }
 45 }
 46 
 47 int hed[N],had[N],sum;
 48 struct E
 49 {
 50     int v,next,w;
 51     E(int v=0,int next=0,int w=0):v(v),next(next),w(w){}
 52 }e[N][2];
 53 inline void insert(int u,int v)
 54 {
 55     e[++sum][0]=E(v,hed[u],point[v]);
 56     hed[u]=sum;
 57     e[sum][1]=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=1;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]==0) que.push(v);
 80         }
 81     }
 82 }
 83 
 84 inline void read(int &x)
 85 {
 86     x=0; register char ch=getchar();
 87     for(;ch>'9'||ch<'0';) ch=getchar();
 88     for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
 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=1;i<=n;i++)
 97         if(!dfn[i]) DFS(i);
 98     for(int v,u=1;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(0,col[1],v1,hed,rd);
108     Topsort(1,col[1],v2,had,cd);
109     for(int v,u=1;u<=sum;u++)
110         for(int i=hed[u];i;i=e[i][0].next)
111         {
112             v=e[i][0].v;
113             ans=max(ans,v1[v]+v2[u]);
114         }
115     for(int v,u=1;u<=sum;u++)
116         for(int i=had[u];i;i=e[i][1].next)
117         {
118             v=e[i][1].v;
119             ans=max(ans,v1[u]+v2[v]);
120         }
121     printf("%d\n",ans-point[col[1]]);
122     return 0;
123 }
124 
125 int Hope=AC();
126 int main(){;}
Topsort AC

 

转载于:https://www.cnblogs.com/Shy-key/p/7471460.html


http://www.niftyadmin.cn/n/1089656.html

相关文章

最近公共祖先(LCA)

最近公共祖先(LCA) 2017-09-06 好东西啊,今天终于会了qwq......(左右的dalao都会qaq) 吐槽:有dalao在旁边装萌新.....就我一条咸鱼 LCA主要是有3中方式:Tarjan,倍增,树链刨分 今天我学的是前两种; 说倍增:倍增一共有两种方法,(预处理),就是预处理是可以bfs队列或者dfs直接建立 …

Linux操作系统下的Oracle数据库编程详解

Linux操作系统下的Oracle数据库编程详解 发布时间&#xff1a;2007.07.20 06:44 来源&#xff1a;赛迪网 作者&#xff1a;sting 1&#xff0e;引言 由于PL/SQL不能用来开发面向普通用户的应用程序&#xff0c;必须借助其他语言或开发工具。在Linux操作系统下应该用什…

bzoj1011 遥远的行星

bzoj1011 遥远的行星 原题链接 题解 一道真正的玄学题。。。。 其实这题根本没法做 首先暴力这么跑&#xff1a;\[ans(s)\sum_{i1}^{\lfloor As\rfloor}\frac{M_sM_i}{s-i}\] 暴力复杂度\(O(n^2)\) 虽然跑不满&#xff0c;但常数至少\(\frac{0.35}{2}\) 所以玄学做法才能过。。…

Oracle数据库开发(二) Linux下配置使用ProC

Oralce数据库开发(二).Linux下配置使用ProC一、摘要上文简单介绍了Windows下ProC配置开发&#xff0c;这次我们使用Linux平台再次配置Oracle ProC开发环境(RedHat Linux 9 Oracle 92)。《ORACLE数据库开发(一).Windows下配置使用ProC》和《ORACLE数据库开发(二).Linux下配置使…

Oracle数据库开发(一) Windows下配置使用ProC

Oracle数据库开发(一).Windows下配置使用ProC一、摘要ProC是Oracle数据库提供的开发接口&#xff0c;支持多种语言。ProC虽然调试维护起来十分不便&#xff0c;但是依靠其结构化清晰的嵌入式SQL&#xff0c;也成为了C/C语言访问控制数据库的常用方式。本文首先介绍Windows平台下…

iphone照片删掉又出现_亲测有用:苹果/iPhone手机相册照片突然全部消失了,一张都没有了,如何恢复?...

亲测有用&#xff0c;但不敢保证每个人这样操作都有用。前些天&#xff0c;朋友跟我说她的iPhone抽风了&#xff0c;无缘无故手机里的照片全消失了&#xff0c;即相机胶卷和最近删除都显示为0。现拍几张照片&#xff0c;相册里也没有显示&#xff0c;万能操作重启也没能解决。我…

linux下c语言获取当前时间

和时间有关的函数定义在头文件”time.h”中 常用函数: time_t time(time_t *t); 函数说明&#xff1a;此函数会返回从公元 1970 年1 月1 日的UTC 时间从0 时0 分0 秒算起到现在所经过的秒数。如果t 并非空指针的话&#xff0c;此函数也会将返回值存到t 指针所指的内存。 char …

Oracle数据库开发(三) Pro*C/C++的编译参数

Oracle Database Development (4). Example Makefile for Pro*C<本文主要介绍Linux下使用Makefile编译ProC程序的方法>It is a pity that there is a few aritcle written in Chinese which talking about the material method of Makefile for Pro*C/C .Maybe its not d…