题意

有一个有向图,允许最多走一次逆向的路,问从1再走回1,最多能经过几个点。

思路

(一)
首先先缩点。自己在缩点再建图中犯了错误,少连接了大点到其他点的边。
跑两次最长路,一次以1为起点,一次以1为终点(跑一遍反图)
然后枚举边,判断可否形成一个环。
(二)
分层图的思想
以为只有一次逆向的机会,可以建两层图,第一层向第二层连翻转的情况。

#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert> /* ⊂_ヽ
  \\ Λ_Λ 来了老弟
   \('ㅅ')
    > ⌒ヽ
   /   へ\
   /  / \\
   レ ノ   ヽ_つ
  / /
  / /|
 ( (ヽ
 | |、\
 | 丿 \ ⌒)
 | |  ) /
'ノ )  Lノ */ using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define boost ios::sync_with_stdio(false);cin.tie(0)
#define rep(a, b, c) for(int a = (b); a <= (c); ++ a)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c); const ll oo = 1ll<<;
const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = 1e9+;
const double esp = 1e-;
const double PI=acos(-1.0);
const double PHI=0.61803399; //黄金分割点
const double tPHI=0.38196601; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} inline void cmax(int &x,int y){if(x<y)x=y;}
inline void cmax(ll &x,ll y){if(x<y)x=y;}
inline void cmin(int &x,int y){if(x>y)x=y;}
inline void cmin(ll &x,ll y){if(x>y)x=y;} /*-----------------------showtime----------------------*/
const int maxn = 1e5+;
vector<int>mp1[maxn],mp2[][maxn];
int dfn[maxn],low[maxn],vis[maxn],gtot,nn,dp[maxn];
int col[maxn],vv[maxn];
stack<int>st; void tarjan(int u){
dfn[u] = low[u] = ++gtot;
st.push(u); vis[u] = ;
for(int i=; i<mp1[u].size(); i++){
int v = mp1[u][i];
if(dfn[v] == ) {
tarjan(v);
low[u] = min(low[u], low[v]);
}
else if(vis[v]){
low[u] = min(low[u], dfn[v]);
}
}
if(low[u] == dfn[u]){
int x;nn++;
while(!st.empty()){
int x = st.top(); st.pop();
col[x] = nn;
vis[x] = ;
dp[nn]++;
if(x == u) break;
}
}
}
int ans = ;
int dis[maxn][];
pii edge[maxn];
void dji(int s,int id){
priority_queue<pii>que; dis[s][id] = dp[s];
que.push(pii(dis[s][id], s));
while(!que.empty()){
int u = que.top().se; que.pop(); for(int i=; i<mp2[id][u].size(); i++) {
int v = mp2[id][u][i],w = dp[v]; if(dis[v][id] < dis[u][id] + w){
dis[v][id] = dis[u][id] + w;
que.push(pii(dis[v][id], v));
}
}
}
}
int main(){
int n,m;
scanf("%d%d", &n, &m);
rep(i, , m) {
int x,y; scanf("%d%d", &x, &y);
mp1[x].pb(y);
edge[i] = pii(x, y);
}
for(int i=; i<=n; i++)
if(!dfn[i])tarjan(i); int s = col[];
int pp = ;
memset(vis, , sizeof(vis));
for(int i=; i<=n; i++){
int u = col[i];
if(u == ) continue; for(int j=; j<mp1[i].size(); j++){
int v = col[mp1[i][j]];
if(v == || u == v) continue; mp2[][u].pb(v);
mp2[][v].pb(u);
}
}
dji(s, ); dji(s, ); int ans = dp[s];
for(int i=; i<=m; i++){
int u = col[edge[i].fi],v = col[edge[i].se];
if(dis[v][] && dis[u][])ans = max(ans, dis[v][] + dis[u][] - dp[s]);
} printf("%d\n", ans);
return ;
}

P3119 [USACO15JAN]草鉴定Grass Cownoisseur 分层图或者跑两次最长路的更多相关文章

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

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

  2. [USACO15JAN]草鉴定Grass Cownoisseur (分层图,最长路,$Tarjan$)

    题目链接 Solution 水水的套路题. 可以考虑到一个环内的点是可以都到达的,所以 \(tajan\) 求出一个 \(DAG\) . 然后 \(DAG\) 上的点权值就是 \(scc\) 的大小. ...

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

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

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

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

  5. P3119 [USACO15JAN]草鉴定Grass Cownoisseur

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

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

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

  7. [Luogu P3119] [USACO15JAN]草鉴定Grass Cownoisseur (缩点+图上DP)

    题面 传送门:https://www.luogu.org/problemnew/show/P3119 Solution 这题显然要先把缩点做了. 然后我们就可以考虑如何处理走反向边的问题. 像我这样的 ...

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

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

  9. 洛谷—— 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 ...

随机推荐

  1. SpringBoot Kafka 整合使用

    前提 假设你了解过 SpringBoot 和 Kafka. 1.SpringBoot 如果对 SpringBoot 不了解的话,建议去看看 DD 大佬 和 纯洁的微笑 的系列博客. 2.Kafka K ...

  2. Vue项目的创建和UI资源

    Vue项目创建打包与UI资源 1.Vue项目创建 1.1 vue-cli脚手架 vue-cli是一个基于vue的构建工具,用于搭建vue项目的环境,有着兼容,方便,快速的优点,能够完全遵循前后端分离的 ...

  3. 8天入门docker系列 —— 第八天 让程序跑在swarm集群上

    真正的落地部署都是希望程序跑在集群下,而不是单机版下测测玩玩,所以这篇就来聊一下怎么使用docker swarm进行部署,因为是swarm是docker自带的, 所以部署起来还是非常简单的. 一:前置 ...

  4. Extjs的textfield的颜色设置和出现的问题笔记

    Ext.getCmp('alarmsLevelVal').setFieldStyle('background-color:#ff0000;background-p_w_picpath: none; ' ...

  5. RGW 学习 前言

    对于CEPH的学习已经有一段的时间了,最近的一段时间都是集中在RGW网关这一方面,所以准备将学习的过程以及源代码的分析,将以随笔的形式记录下来. 每天更新一章.

  6. java 动手动脑7

    ---恢复内容开始--- 一.动手动脑:多层的异常捕获-1 阅读以下代码(CatchWho.java),写出程序运行结果: ArrayIndexOutOfBoundsException/内层try-c ...

  7. Button 使用详解

    极力推荐文章:欢迎收藏 Android 干货分享 阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android 本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以 ...

  8. 【Java例题】3.5 级数之和

    5. 计算级数之和: y=3*1!/1-3^2*2!/2^2+3^3*3!/3^3-...+ (-1)^(n-1)*3^n*n!/n^n. 这里的"^"表示乘方,"!&q ...

  9. Linux下Docker以及portainer相关配置

    一.安装使用Docer CE 本文以CentOS 7为例,安装docker CE版本,docker有两种版本,社区版本CE和企业版本EE,此处学习研究以CE版本为例, 两种安装方式可选:1.使用yum ...

  10. ASP.NET Core MVC 之控制器(Controller)

    操作(action)和操作结果(action result)是 ASP.NET MVC 构建应用程序的一个基础部分. 在 ASP.NET MVC 中,控制器用于定义和聚合一组操作.操作是控制器中处理传 ...