洛谷 P3119 [USACO15JAN]草鉴定Grass Cownoisseur (SCC缩点,SPFA最长路,枚举反边)
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.
输入:
第一行:草场数n,道路数m。
以下m行,每行x和y表明有x到y的单向边,不会有重复的道路出现。
输出格式
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复制
输出 #1复制
说明/提示
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.
思路:
首先用tarjian缩点,缩点后是一个有向无环图。每一个点的点权是他所在的SCC中节点的个数。
然后从一号节点所在的scc块进行SPFA找最长路,可以得到dis1[i] 代表从1所在联通块为始点走到scc_i的最大权值。
然后反向建边跑最长路,可以得到dis1[i] 代表从i所在联通块scc_i为始点,scc_1 为终点的最大权值。
然后枚举所有scc_i,如果scc_1可以到达scc_i,且 有scc_j为起点到scc_i 为终点的边,scc_j可以到达scc_1,则尝试逆行该边,更新答案,维护最大值。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {a %= MOD; if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
inline void getInt(int* p);
const int maxn = 500010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int From[maxn], Laxt[maxn], To[maxn << 2], Next[maxn << 2], cnt;
int low[maxn], dfn[maxn], times, qq[maxn], head, scc_cnt, scc[maxn];
bool inst[maxn];
vector<int>G[maxn];
void add(int u, int v)
{
Next[++cnt] = Laxt[u]; From[cnt] = u;
Laxt[u] = cnt; To[cnt] = v;
}
int c[maxn];
void tarjan(int u)
{
dfn[u] = low[u] = ++times;
qq[++head] = u;
inst[u] = 1;
for (int i = Laxt[u]; i; i = Next[i]) {
if (!dfn[To[i]]) {
tarjan(To[i]);
low[u] = min(low[u], low[To[i]]);
} else if (inst[To[i]]) {
low[u] = min(low[u], dfn[To[i]]);
}
}
if (low[u] == dfn[u]) {
scc_cnt++;
while (true) {
int x = qq[head--];
scc[x] = scc_cnt;
c[scc_cnt]++;
inst[x] = 0;
if (x == u) { break; }
}
}
}
int n, m;
std::vector<int> v1[maxn], v2[maxn];
int dis1[maxn];
int dis2[maxn];
queue<int> q;
bool vis[maxn];
void spfa1(int S)
{
dis1[S] = c[S];
q.push(S);
while (!q.empty())
{
int now = q.front();
q.pop();
for (auto y : v1[now])
{
if (dis1[y] < dis1[now] + c[y])
{
dis1[y] = dis1[now] + c[y];
if (!vis[y])
{
q.push(y);
vis[y] = 1;
}
}
}
vis[now] = 0;
}
}
void spfa2(int S)
{
dis2[S] = c[S];
q.push(S);
while (!q.empty())
{
int now = q.front();
q.pop();
for (auto y : v2[now])
{
if (dis2[y] < dis2[now] + c[y])
{
dis2[y] = dis2[now] + c[y];
if (!vis[y])
{
q.push(y);
vis[y] = 1;
}
}
}
vis[now] = 0;
}
}
int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
du2(n, m);
while (m--)
{
int x, y;
du2(x, y);
add(x, y);
}
repd(i, 1, n)
{
if (!dfn[i])
{
tarjan(i);
}
}
repd(u, 1, n)
{
for (int i = Laxt[u]; i; i = Next[i])
{
if (scc[u] != scc[To[i]])
{
// cout<<u<<" "<<To[i]<<" "<<scc[u]<<" "<<scc[To[i]]<<endl;
v1[scc[u]].push_back(scc[To[i]]);
v2[scc[To[i]]].push_back(scc[u]);
}
}
}
spfa1(scc[1]);
spfa2(scc[1]);
int ans = c[scc[1]];
repd(i, 1, scc_cnt)
{
if (vis[i] == 0 && dis1[i])
{
vis[i] = 1;
for (auto y : v2[i])
{
if (!dis2[y])
continue;
ans = max(ans, dis1[i] + dis2[y] - c[scc[1]]);
}
}
}
printf("%d\n", ans);
return 0;
}
inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}
洛谷 P3119 [USACO15JAN]草鉴定Grass Cownoisseur (SCC缩点,SPFA最长路,枚举反边)的更多相关文章
- 洛谷 P3119 [USACO15JAN]草鉴定Grass Cownoisseur 解题报告
P3119 [USACO15JAN]草鉴定Grass Cownoisseur 题目描述 约翰有\(n\)块草场,编号1到\(n\),这些草场由若干条单行道相连.奶牛贝西是美味牧草的鉴赏家,她想到达尽可 ...
- 洛谷——P3119 [USACO15JAN]草鉴定Grass Cownoisseur
P3119 [USACO15JAN]草鉴定Grass Cownoisseur 题目描述 In an effort to better manage the grazing patterns of hi ...
- 洛谷—— P3119 [USACO15JAN]草鉴定Grass Cownoisseur || BZOJ——T 3887: [Usaco2015 Jan]Grass Cownoisseur
http://www.lydsy.com/JudgeOnline/problem.php?id=3887|| https://www.luogu.org/problem/show?pid=3119 D ...
- 洛谷 P3119 [USACO15JAN]草鉴定Grass Cownoisseur
屠龙宝刀点击就送 Tarjan缩点+拓扑排序 以后缩点后建图看n范围用vector ,或者直接用map+vector 结构体里数据要清空 代码: #include <cstring> #i ...
- 洛谷3119 [USACO15JAN]草鉴定Grass Cownoisseur
原题链接 显然一个强连通分量里所有草场都可以走到,所以先用\(tarjan\)找强连通并缩点. 对于缩点后的\(DAG\),先复制一张新图出来,然后对于原图中的每条边的终点向新图中该边对应的那条边的起 ...
- P3119 [USACO15JAN]草鉴定Grass Cownoisseur
题目描述 In an effort to better manage the grazing patterns of his cows, Farmer John has installed one-w ...
- 洛谷P3119 USACO15JAN 草鉴定
题目描述 In an effort to better manage the grazing patterns of his cows, Farmer John has installed one-w ...
- luogu P3119 [USACO15JAN]草鉴定Grass Cownoisseur
题目描述 In an effort to better manage the grazing patterns of his cows, Farmer John has installed one-w ...
- P3119 [USACO15JAN]草鉴定Grass Cownoisseur 分层图或者跑两次最长路
https://www.luogu.org/problemnew/show/P3119 题意 有一个有向图,允许最多走一次逆向的路,问从1再走回1,最多能经过几个点. 思路 (一)首先先缩点.自己在缩 ...
随机推荐
- Volatility取证使用笔记
最近简单的了解了一下Volatility这个开源的取证框架,这个框架能够对导出的内存镜像镜像分析,能过通过获取内核的数据结构,使用插件获取内存的详细情况和运行状态,同时可以直接dump系统文件,屏幕截 ...
- Java学习笔记-Lambda表达式
Lambda表达式支持将代码块作为方法参数,Lambda表达式允许使用简洁的代码来创建只有一个抽象方法的接口(这种接口被称为函数是接口)的实例 意义 自从Java 8开始,Java支持Lambda表达 ...
- idea配置svn,maven,jdk和一些基础设置
1.idea配置svn 1.1 首先下载svn,百度云链接:链接:https://pan.baidu.com/s/1PvSBuHcHMrrBHfnOfVRC9Q 提取码:hs7l 1.2 开始安装 这 ...
- 学习笔记:CentOS7学习之十九:Linux网络管理技术
目录 学习笔记:CentOS7学习之十九:Linux网络管理技术 本文用于记录学习体会.心得,兼做笔记使用,方便以后复习总结.内容基本完全参考学神教育教材,图片大多取材自学神教育资料,在此非常感谢MK ...
- brew update慢,brew install慢如何解决?
主要是资源访问太慢造成的,替换默认源镜像就行. brew使用国内镜像源 这里用中科大的,另外还有清华的可用 1 2 3 4 5 6 7 8 9 10 # 步骤一 cd "$(brew ...
- docker中启动2个mysql实列
一.mac环境安装docker容器 在docker官网中下载docker容器,地址:https://www.docker.com/products/docker-desktop 具体安装教程及设置网络 ...
- IDEA插件之JProfiler
1.是什么?来用干嘛的? 一个商业授权的Java剖析工具. 用来剖析程序内存.CPU使用情况,找到性能瓶颈,快速定位问题所在. 2.IDEA安装JProfiler插件 (1)File -> Se ...
- 4.Linux系统命令及其使用详解
运维工程师必会的109个Linux命令 文件管理basename:从文件名中去掉路径和扩展名 cat:把档案串连接后传到基本输出(屏幕或加 > filename 到另一个档案)cd:切换目录 ...
- 【搜索】Partition problem
题目链接:传送门 题面: [题意] 给定2×n个人的相互竞争值,请把他们分到两个队伍里,如果是队友,那么竞争值为0,否则就为v[i][j]. [题解] 爆搜,C(28,14)*28,其实可以稍加优化, ...
- uni-app使用Canvas绘图
最近公司项目在用uni-app做小程序商城,其中商品和个人需要生成图片海报,经过摸索记录后将一些重点记录下来.这里有两种方式来生成 1.后台控制生成 2.前端用canvas合成图片 这里我们只讲使用c ...