P3119 [USACO15JAN]草鉴定Grass Cownoisseur

P3119 [USACO15JAN]草鉴定Grass Cownoisseur

题目描述

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.

约翰有n块草场,编号1到n,这些草场由若干条单行道相连。奶牛贝西是美味牧草的鉴赏家,她想到达尽可能多的草场去品尝牧草。

贝西总是从1号草场出发,最后回到1号草场。她想经过尽可能多的草场,贝西在通一个草场只吃一次草,所以一个草场可以经过多次。因为草场是单行道连接,这给贝西的品鉴工作带来了很大的不便,贝西想偷偷逆向行走一次,但最多只能有一次逆行。问,贝西最多能吃到多少个草场的牧草。

输入输出格式

输入格式:

INPUT: (file grass.in)

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: (file grass.out)

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.

输入输出样例

输入样例#1: 复制

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

输出样例#1: 复制

6

说明

SOLUTION NOTES:

Here is an ASCII drawing of the sample input:

v---3-->6

7 |\ |

^\ v \ |

| \ 1 | | | v | v 5

4<--2---^

Bessie can visit pastures 1, 2, 4, 7, 2, 5, 3, 1 by traveling

backwards on the path between 5 and 3. When she arrives at 3 she

cannot reach 6 without following another backwards path.

分析

一道好题。

题意:一张有向图图,求可以改变一条边的方向,之后的最大的强联通分量(包含1)。

思路:首先求出强连通分量,缩点,这没啥好说的,

然后就是改变一条边的方向,使得这棵树(有向无环图)从1开始走,再返回1,所经过的点最多。

首先明确:因为只改一条边,所以可以枚举是那条边。因为从1开始走,再回到1,所以可以求出每个点到1的最大距离(所经过的点最多)。

那么问题是如何求每个点到1和1到每个点的最大距离,

可以dfs,不过有这样一种情况:在搜到2,及更新2的后代之后,又发现了3,那么2及2的后代又要全更新一次。

为了改变这种情况,所以可以拓扑排序。

之后枚举边,假设修改它,那么修改它的答案就是这条边的起点到1的距离,1到这条边的终点的距离。取个最大值

代码有点丑>o<

code

  1 #include<cstdio>
  2 #include<algorithm>
  3 #include<cstring>
  4 #include<cmath>
  5 #include<cstdlib>
  6 #include<vector>
  7 #include<iostream>
  8
  9 using namespace std;
 10
 11 const int N = 200100;
 12 const int M = 200100;
 13 struct Edge{
 14     int to,nxt;
 15 }e[N];
 16 int head[N],dfn[N],low[N],st[N],bel[N],sz[N];
 17 int dis[N],fdis[N],ru[N],fru[N];
 18 int q[1000000],L,R;
 19 bool vis[N],pd[N],fpd[N];
 20 int cnt,tn,tot,top;
 21 vector<int>s[N];
 22 vector<int>s2[N];
 23
 24 inline char nc() {
 25     static char buf[100000],*p1 = buf,*p2 = buf;
 26     return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2) ? EOF : *p1++;
 27 }
 28 inline int read() {
 29     int x = 0,f = 1;char ch = nc();
 30     for (; ch<‘0‘||ch>‘9‘; ch = nc())
 31         if (ch==‘-‘) f = -1;
 32     for (; ch>=‘0‘&&ch<=‘9‘; ch = nc())
 33         x = x*10+ch-‘0‘;
 34     return x * f;
 35 }
 36
 37 inline void add_edge(int u,int v) {
 38     e[++tot].to = v,e[tot].nxt = head[u],head[u] = tot;
 39 }
 40
 41 void tarjan(int u) {
 42     dfn[u] = low[u] = ++tn;
 43     st[++top] = u;
 44     vis[u] = true;
 45     for (int i=head[u]; i; i=e[i].nxt) {
 46         int v = e[i].to;
 47         if (!dfn[v]) {
 48             tarjan(v);
 49             low[u] = min(low[u],low[v]);
 50         }
 51         else if (vis[v])
 52             low[u] = min(low[u],dfn[v]);
 53     }
 54     if (low[u]==dfn[u]) {
 55         ++cnt;
 56         do {
 57             vis[st[top]] = false;
 58             bel[st[top]] = cnt;
 59             sz[cnt]++;
 60             top--;
 61         } while (st[top+1] != u);
 62     }
 63 }
 64 void search1(int u) {
 65     pd[u] = true;
 66     int tmp = s[u].size();
 67     for (int i=0; i<tmp; ++i) {
 68         int v = s[u][i];
 69         ru[v]++;
 70         if (!pd[v]) search1(v);
 71     }
 72 }
 73 void topo1() {
 74     search1(bel[1]);
 75     L = 1,R = 0;
 76     q[++R] = bel[1];
 77     dis[bel[1]] = sz[bel[1]];
 78     while (L <= R) {
 79         int u = q[L++];
 80         int tmp = s[u].size();
 81         for (int i=0; i<tmp; ++i) {
 82             int v = s[u][i];
 83             dis[v] = max(dis[v],dis[u]+sz[v]);
 84             ru[v]--;
 85             if (ru[v]==0) q[++R] = v;
 86         }
 87     }
 88 }
 89 void search2(int u) {
 90     fpd[u] = true;
 91     int tmp = s2[u].size();
 92     for (int i=0; i<tmp; ++i) {
 93         int v = s2[u][i];
 94         fru[v]++;
 95         if (!fpd[v]) search2(v);
 96     }
 97 }
 98 void topo2() {
 99     search2(bel[1]);
100     L = 1,R = 0;
101     q[++R] = bel[1];
102     fdis[bel[1]] = sz[bel[1]];
103     while (L <= R) {
104         int u = q[L++];
105         int tmp = s2[u].size();
106         for (int i=0; i<tmp; ++i) {
107             int v = s2[u][i];
108             fdis[v] = max(fdis[v],fdis[u]+sz[v]);
109             fru[v]--;
110             if (fru[v]==0) q[++R] = v;
111         }
112     }
113 }
114 int main() {
115
116     int n = read(),m = read();
117     for (int i=1; i<=m; ++i) {
118         int u = read(),v = read();
119         add_edge(u,v);
120     }
121     for (int i=1; i<=n; ++i)
122         if (!dfn[i]) tarjan(i);
123     for (int u=1; u<=n; ++u) {
124         for (int i=head[u]; i; i=e[i].nxt) {
125             int v = e[i].to;
126             if (bel[u]!=bel[v]) {
127                 s[bel[u]].push_back(bel[v]);
128                 s2[bel[v]].push_back(bel[u]);
129             }
130         }
131     }
132
133     topo1();
134     topo2();
135     int ans = 0;
136     for (int i=1; i<=cnt; ++i) {
137         int tmp = s[i].size();
138         for (int j=0; j<tmp; ++j) {
139             if (i==s[i][j]||!pd[s[i][j]] || !fpd[i]) continue;
140             ans = max(ans,dis[s[i][j]]+fdis[i]-sz[bel[1]]);
141         }
142     }
143     printf("%d",ans);
144     return 0;
145 }
时间: 2024-10-12 00:01:47

P3119 [USACO15JAN]草鉴定Grass Cownoisseur的相关文章

洛谷 P3119 [USACO15JAN]草鉴定Grass Cownoisseur

P3119 [USACO15JAN]草鉴定Grass Cownoisseur tarjan缩点,正反spfa,枚举边,更新最大值 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define maxn 1000000 4 #define inf 0x3f3f3f3f 5 int n,m,x[maxn],y[maxn],z,num,head[maxn],head2[maxn],tim,ans,tot,dis1[maxn],dis2[maxn

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

P3119 [USACO15JAN]草鉴定Grass Cownoisseur 题目描述 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 co

luogu P3119 [USACO15JAN]草鉴定Grass Cownoisseur

题目描述 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

[USACO15JAN]草鉴定Grass Cownoisseur(分层图+tarjan)

[USACO15JAN]草鉴定Grass Cownoisseur 题目描述 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

[USACO15JAN]草鉴定Grass Cownoisseur

[题目描述]: [USACO15JAN]草鉴定Grass Cownoisseur [思路]: 首先我们先思考贝茜不走那条反边,那么对于任意强连通分量\(E\)易知: \(\forall u,v \in E\),\(\exists u \to v \ and \ v \to u\) \(\because\)贝茜每次经过一个草场时只会吃一次草,\(\therefore\)可以进行缩点,缩点后得到一个\(DAG\),统计每一个强连通分量的\(size\)值,表示此强连通分量中有多少个点,然后在\(DA

Luogu 3119 [USACO15JAN]草鉴定Grass Cownoisseur

思路很乱,写个博客理一理. 缩点 + dp. 首先发现把一个环上的边反向是意义不大的,这样子不但不好算,而且相当于浪费了一次反向的机会.反正一个强连通分量里的点绕一遍都可以走到,所以我们缩点之后把一个强连通分量放在一起处理. 设$st$表示缩点之后$1$所在的点,设$f_{x}$表示从$st$走到$x$的最长链,$g_{x}$表示从$x$走到$st$的最长链,因为把一个$DAG$上的边反向一下并不会走重复的点,那么我们最后枚举一下边$(x, y)$,把它反向,这样子$f_{x} + g_{y}

BZOJ 3887[Usaco2015 Jan]Grass Cownoisseur

题面: 3887: [Usaco2015 Jan]Grass Cownoisseur Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 237  Solved: 130[Submit][Status][Discuss] Description In an effort to better manage the grazing patterns of his cows, Farmer John has installed one-way cow pat

bzoj3887: [Usaco2015 Jan]Grass Cownoisseur

题意: 给一个有向图,然后选一条路径起点终点都为1的路径出来,有一次机会可以沿某条边逆方向走,问最多有多少个点可以被经过?(一个点在路径中无论出现多少正整数次对答案的贡献均为1) =>有向图我们先考虑缩点.然后观察缩点后的图可以发现新的路径中必定只有一条边是反向的才符合条件.那么我们可以联想到某道最短路的题将边反向存一遍后分别从s和t跑一跑.那么这里bfs跑一跑就行了.然后有一个坑点:这种重建图的注意es和edges不然es会在中途就被修改掉了... #include<cstdio> #

【tarjan+拓扑】BZOJ3887-[Usaco2015 Jan]Grass Cownoisseur

[题目大意] 给一个有向图,然后选一条路径起点终点都为1的路径出来,有一次机会可以沿某条边逆方向走,问最多有多少个点可以被经过?(一个点在路径中无论出现多少正整数次对答案的贡献均为1) [思路] 首先缩点,对于每一个联通块求出正图和反图中节点1所在的联通块到它的最长节点数.这个用拓扑排序处理一下. 枚举每一条边取反,对于边(u,v),其取反后的距离就等于dis[u所在的联通快]+dis[v所在的联通块]-dis[1所在的联通块](因为会被重复计算不要忘记减去) 我一开始非常脑抽地在想会不会发生这