屠龙宝刀点击就送

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. bootstrap的popover()的使用

    有一些选项是通过 Bootstrap 数据 API(Bootstrap Data API)添加或通过 JavaScript 调用的.下表列出了这些选项: 选项名称 类型/默认值 Data 属性名称 描 ...

  2. Sharepoint2013搜索学习笔记之修改搜索拓扑(三)

    搜索服务新建好之后可以从管理中心,应用程序管理页面,进入搜索服务的管理页面,进入管理页面之后可以看到当前sharepoint场的搜索拓扑结构. 如果sharepoint场内有多台服务器,需要将搜索组件 ...

  3. 转换嵌套JSON数据为TABLE

    先准备一些数据: 创建一张临时表来存储: DECLARE @json_table AS TABLE ( [type] NVARCHAR(MAX), [desc] NVARCHAR(MAX) ) Sou ...

  4. HDU 1506【单调栈】

    思路: 转化成对于某一位置为最小值求向两边最远>=他的位置,用单调栈就能轻易完成. 那么ans=(left+right)*h[i]; 维护单调递增还是递减呢? 我们能很快反应到,一旦碰到一个比他 ...

  5. 一些我推荐的和想上的网络课程(Coursera, edX, Udacity)

    从面向找工作的角度出发,我觉得以下课程有很大帮助: 首推Robert Sedgewick,也是我觉得对我帮助最大的老师,讲课特点是能把复杂的算法讲解清楚(典型例子:红黑树,KMP算法) 他在Cours ...

  6. Corn Fields(模板)

    题目链接 #include <stdio.h> #include <algorithm> #include <string.h> #include <iost ...

  7. Theme Section

    题目链接 #include <cstdio> #include <iostream> #include <cstring> using namespace std; ...

  8. JS高级学习历程-2

    1.dom操作,利用dom创建无序列表.并追加到body里边,里面要求至少有四个项目. <!DOCTYPE html> <html lang="en"> & ...

  9. 破解百度翻译页面api参数加密

    我们的目标 https://fanyi.baidu.com/      找到获取翻译的请求 是这个 https://fanyi.baidu.com/v2transapi 查看一下post提交的表单,是 ...

  10. git 本地新建分支Push到远程

    1. 本地新建分支,并切换到新的分支 git branch local_branch; git checkout local_branch; 2.第一条的命令也可以简单的一条命令来替代 git che ...