屠龙宝刀点击就送

Tarjan缩点+拓扑排序

以后缩点后建图看n范围用vector ,或者直接用map+vector

结构体里数据要清空

代码:

#include <cstring>
#include <cctype>
#include <cstdio>
#include <vector>
#include <queue>
#define N 100005
using namespace std;
vector<int>G[N];
inline void Read(int &x)
{
register char ch=getchar();
for(x=;!isdigit(ch);ch=getchar());
for(;isdigit(ch);x=x*+ch-'',ch=getchar());
}
bool instack[N];
int n,m,ans,stack[N],low[N],dfn[N],col[N],sum[N],f1[N],f2[N],in1[N],in2[N],sumcol,tim,top;
inline int min(int a,int b) {return a>b?b:a;}
inline int max(int a,int b) {return a>b?a:b;}
void tarjan(int x)
{
low[x]=dfn[x]=++tim;
stack[++top]=x;
instack[x]=true;
for(int i=;i<G[x].size();++i)
{
int v=G[x][i];
if(!dfn[v])
{
tarjan(v);
low[x]=min(low[x],low[v]);
}
else if(instack[v]) low[x]=min(low[x],dfn[v]);
}
if(low[x]==dfn[x])
{
int k;
sumcol++;
do
{
k=stack[top--];
instack[k]=false;
col[k]=sumcol;
sum[sumcol]++;
}while(k!=x);
}
}
struct node
{
int f[N],in[N],cnt;
struct Edge
{
int to;
Edge * next;
}*head[N],edge[N];
void ins(int u,int v)
{
in[v]++;
edge[++cnt].next=head[u];
edge[cnt].to=v;
head[u]=cnt+edge;
}
void tppx()
{
queue<int>q;
for(int i=;i<=sumcol;++i) {f[i]=0xefefefef;if(!in[i]) q.push(i);}
f[col[]]=sum[col[]];
for(int now;!q.empty();)
{
now=q.front();
q.pop();
for(Edge * u=head[now];u;u=u->next)
{
int v=u->to;
f[v]=max(f[v],f[now]+sum[v]);
in[v]--;
if(!in[v]) q.push(v);
}
}
}
node ()
{
cnt=;
memset(in,,sizeof(in));
memset(head,NULL,sizeof(head));
}
};
node a,b;
int main()
{
Read(n);
Read(m);
for(int x,y;m--;)
{
Read(x);
Read(y);
G[x].push_back(y);
}
for(int i=;i<=n;++i) if(!dfn[i]) tarjan(i);
for(int i=;i<=n;++i)
{
for(int j=;j<G[i].size();++j)
{
int v=G[i][j];
if(col[i]!=col[v]) a.ins(col[i],col[v]),b.ins(col[v],col[i]);
}
}
a.tppx();
b.tppx();
ans=sum[col[]];
for(int i=;i<=n;++i)
{
for(int j=;j<G[i].size();++j)
{
int v=G[i][j];
ans=max(ans,a.f[col[v]]+b.f[col[i]]);
}
}
printf("%d\n",ans-sum[col[]]);
return ;
}

洛谷 P3119 [USACO15JAN]草鉴定Grass Cownoisseur的更多相关文章

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

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

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

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

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

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

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

  5. 洛谷3119 [USACO15JAN]草鉴定Grass Cownoisseur

    原题链接 显然一个强连通分量里所有草场都可以走到,所以先用\(tarjan\)找强连通并缩点. 对于缩点后的\(DAG\),先复制一张新图出来,然后对于原图中的每条边的终点向新图中该边对应的那条边的起 ...

  6. P3119 [USACO15JAN]草鉴定Grass Cownoisseur

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

  7. 洛谷P3119 USACO15JAN 草鉴定

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

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

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

  9. P3119 [USACO15JAN]草鉴定Grass Cownoisseur 分层图或者跑两次最长路

    https://www.luogu.org/problemnew/show/P3119 题意 有一个有向图,允许最多走一次逆向的路,问从1再走回1,最多能经过几个点. 思路 (一)首先先缩点.自己在缩 ...

随机推荐

  1. CF-822B

    B. Crossword solving time limit per test 1 second memory limit per test 256 megabytes input standard ...

  2. Hadoop中Partition解析

    1.解析Partition Map的结果,会通过partition分发到Reducer上,Reducer做完Reduce操作后,通过OutputFormat,进行输出,下面我们就来分析参与这个过程的类 ...

  3. dpdk数据包捕获技术笔记1

    1 高效捕包技术的重要性 高性能系统需要在很短的时间内,成功的收集和处理大量的数据,目标系统的实时数据需要被收集,管里和控制. 2 传统的数据包捕获机制 Inter指出,影响数据包捕获性能主要原因是系 ...

  4. 计蒜课/UCloud 的安全秘钥(hash)

    题目链接:https://nanti.jisuanke.com/t/15768 题意:中文题诶- 思路:直接hash就好了,当时zz了没想到... 代码: #include <iostream& ...

  5. 51nod1412(dp)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1412 代码: #include <bits/stdc+ ...

  6. 洛谷P2257 YY的GCD(莫比乌斯反演)

    传送门 原来……莫比乌斯反演是这么用的啊……(虽然仍然不是很明白) 首先,题目所求如下$$\sum_{i=1}^n\sum_{j=1}^m[gcd(i,j)=prim]$$ 我们设$f(d)$表示$g ...

  7. 基于canvas绘图 缩放 做标记

    技术要点: 1.img 绘制到canvas 2.绘制完成以后进行拖拽,缩放 3.使用canvas画图,在绘制的img上进行标记划线,当然可以实现跟过功能,例如百度地图的功能,做单个标记,区域标记等. ...

  8. PJzhang:安全小课堂-安全软件为什么很重要,看这里!

    猫宁!!! 参考链接: http://www.360.cn/webzhuanti/mianyigongju.html https://www.freebuf.com/fevents/204100.ht ...

  9. jQuery EasyUI/TopJUI输入框事件监听

    jQuery EasyUI/TopJUI输入框事件监听 代码如下: <div data-toggle="topjui-panel" title="" da ...

  10. git小白的学习

    1.1 Git介绍 1.1.1 什么是Git Git是一个开源的分布式版本控制系统,可以有效.高速地处理从很小到非常大的项目版本管理.Git 是 Linus Torvalds 为了帮助管理 Linux ...