洛谷P3119 草鉴定
这个题调了一天。。
读完题目之后我们不难想出这个题是个tarjan缩点问题,因为尽量多的经过草场,所以一号点所在的强连通分量里左右的点都是不需要在进行走逆向边,所能到达的。
然后问题就落在怎么处理我们走这一次逆向边上。
仔细看题目要求,题目要求我们必须从一号点出发,最后回到一号点。所以我想到了建一个逆向的图,虚拟出cnt + 1 , cnt+2,cnt+3.....n+cnt号点,建一个逆向图(用进行缩点之后的点集重新构造),因为我们求出进过草场最多是多少,所以我们将强连通分量中有多少个点在缩点中记录下来(num[i]:表示第ii个强连通分量中有多少个点),将它作为边权。
最后我们只要从1号点所在的强连通分量开始,跑一边最长路就好了,最后输出从1号点到我们虚拟出来的1 + cnt号点就好了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn = 2e5 + ; inline int read(){
char ch = getchar();
int f = , x = ;
while(ch > '' || ch < ''){if(ch == '-')f = -;ch = getchar();}
while(ch >= '' && ch <= ''){x = (x << ) + (x << ) + ch - '';ch = getchar();}
return x * f;
} int n,m,x,y;
int head[maxn],tot,head2[maxn],tot2;
int ans; struct Edge{
int from,to,next;
}edge[maxn << ],edge2[maxn << ]; void add(int u,int v){
edge[++tot].from = u;
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot;
} void add_edge(int u,int v){
edge2[++tot2].from = u;
edge2[tot2].to = v;
edge2[tot2].next = head2[u];
head2[u] = tot2;
} int dfn[maxn],low[maxn],ind;
int belong[maxn],num[maxn],cnt,stack[maxn],top;
bool ins[maxn]; void tarjan(int x){
low[x] = dfn[x] = ++ind;
ins[x] = true;
stack[++top] = x;
for(int i=head[x];i;i=edge[i].next){
int v = edge[i].to;
if(ins[v]) low[x] = min(low[x] , dfn[v]);
if(!dfn[v]) {
tarjan(v);
low[x] = min(low[x] , low[v]);
}
}
int k = ;
if(dfn[x] == low[x]){
cnt++;
do{
k = stack[top];
num[cnt]++;
top--;
ins[k] = false;
belong[k] = cnt;
} while(k != x);
}
} int dis[maxn];
bool vis[maxn]; void spfa(int s){
queue<int> q;
q.push(s);
dis[s] = ;
vis[s] = true;
while(!q.empty()){
int cur = q.front();
q.pop(); vis[cur] = false;
for(int i=head2[cur];i;i=edge2[i].next){
int v = edge2[i].to;
if(dis[v] < dis[cur] + num[cur]){
dis[v] = dis[cur] + num[cur];
if(vis[v] == ){
q.push(v);
vis[v] = true;
}
}
}
}
} int main(){
n = read(); m = read();
for(int i=;i<=m;i++){
x = read(); y =read();
add(x , y);
}
for(int i=;i<=n;i++)
if(!dfn[i]) tarjan(i);
for(int i=;i<=n;i++)
num[i + cnt] = num[i];
for(int i=;i<=m;i++)
if(belong[edge[i].from] != belong[edge[i].to]){
add_edge(belong[edge[i].from] , belong[edge[i].to]);
add_edge(belong[edge[i].to] , belong[edge[i].from] + cnt);
add_edge(belong[edge[i].from] + cnt , belong[edge[i].to] + cnt);
}
spfa(belong[]);
printf("%d\n",dis[belong[] + cnt]);
return ;
}
洛谷P3119 草鉴定的更多相关文章
- 洛谷P3119草鉴定
题目 草鉴定,tarjan可以用来缩点,优化spfa的时间, 缩点之后就是一个\(DAG\)了,因此完全可以用来跑spfa上的最长路,然后枚举每条边,查看是否这条边的两个节点分别可以到达起点所在的强连 ...
- 洛谷3119 草鉴定(tarjan)
题目大意 约翰有\(n\)块草场,编号\(1\)到\(n\),这些草场由若干条单行道相连.奶牛贝西是美味牧草的鉴赏家,她想到达尽可能多的草场去品尝牧草. 贝西总是从\(1\)号草场出发,最后回到\(1 ...
- 【题解】洛谷P3119 Grass Cownoisseur G
题面:洛谷P3119 Grass Cownoisseur G 本人最近在熟悉Tarjan的题,刷了几道蓝题后,我飘了 趾高气扬地点开这道紫题,我一瞅: 哎呦!这不是分层图吗? 突然就更飘了~~~ 用时 ...
- 洛谷 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 (SCC缩点,SPFA最长路,枚举反边)
P3119 [USACO15JAN]草鉴定Grass Cownoisseur 题目描述 In an effort to better manage the grazing patterns of hi ...
- 【洛谷P3119】[USACO15JAN]草鉴定Grass Cownoisseur
草鉴定Grass Cownoisseur 题目链接 约翰有n块草场,编号1到n,这些草场由若干条单行道相连.奶牛贝西是美味牧草的鉴赏家,她想到达尽可能多的草场去品尝牧草. 贝西总是从1号草场出发,最后 ...
- 洛谷P3119 USACO15JAN 草鉴定
题目描述 In an effort to better manage the grazing patterns of his cows, Farmer John has installed one-w ...
- 洛谷 P3119 [USACO15JAN]草鉴定Grass Cownoisseur
屠龙宝刀点击就送 Tarjan缩点+拓扑排序 以后缩点后建图看n范围用vector ,或者直接用map+vector 结构体里数据要清空 代码: #include <cstring> #i ...
随机推荐
- 正确理解 LEAL (Load Effective Address) 指令
LEAL: leal S, D -> D ← &S 在 CSAPP (Computer Systems: A Programmer’s Perspective) 中,对 LE ...
- CentOS 6.6下配置本地yum源与网络yum源
一.本地yum源 1.系统默认已经安装了可使用yum的软件包,所以可以直接配置: [root@localhost ~]# cd /etc/yum.repos.d/ ...
- 2018-2019 ACM-ICPC ECfinal I. Misunderstood … Missing
题目链接 首先有两个个属性值:\(A,D\),其中\(A\)表示目前攻击力,\(D\)表示每回合攻击的增量. 现在一共有\(n\)个回合,每一回合\(i\),可以有以下三种操作: 1.进行攻击,造成\ ...
- php 性能优化
php 性能测试工具 ab(Apache Benchmark) ab 是由 Apache 提供的压力测试软件.安装 apache 服务器时会自带该压测软件. 如何使用: ab -n1000 -c100 ...
- 修改const保护的值
先看代码: #include <stdio.h> void main() { const int num = 10; int *a = (int *)(void *)# / ...
- Ansible5:常用模块
目录 ping模块 setup模块 file模块 copy模块 service模块 cron模块 yum模块 user模块与group模块 user模块 group示例 synchronize模块 f ...
- 1082 线段树练习 3 && 树状数组区间修改区间查询
1082 线段树练习 3 题意: 给定序列初值, 要求支持区间修改, 区间查询 Solution 用树状数组, 代码量小, 空间占用小 巧用增量数组, 修改时在 \(l\) 处 $ + val$ , ...
- bzoj千题计划122:bzoj1034: [ZJOI2008]泡泡堂BNB
http://www.lydsy.com/JudgeOnline/problem.php?id=1034 从小到大排序后 最大得分: 1.自己最小的>对方最小的,赢一场 2.自己最大的>对 ...
- linux scp上传文件到其他机器上
scp是secure copy的简写,用于在Linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行拷贝不能跨服务器,而且scp传输是加密的.可能会稍微影响一下速度.当你服务器 ...
- wait&waitpid状态值
[wait&waitpid状态值] 1. python 中 os.system 的返回值的format与wait的返回值status一致: On Unix, the return value ...