洛谷 P3119 [USACO15JAN]草鉴定Grass Cownoisseur
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的更多相关文章
- 洛谷 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 || 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 ...
- 洛谷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,最多能经过几个点. 思路 (一)首先先缩点.自己在缩 ...
随机推荐
- 3.7-3.9 HBase表属性
一.表压缩 1.HBase Sanppy HBase Sanppy 1)配置Haodop压缩 [beifeng@hadoop-senior hadoop-2.5.0]$ bin/hadoop chec ...
- 牛客月赛 G-many sum(筛因子)
many sum 链接:https://ac.nowcoder.com/acm/contest/879/G来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K, ...
- jquery获取元素对应高度
https://www.cnblogs.com/sntetwt/p/3823592.html 获取点击元素距离页面的高度 $(select).offset().top - $(document).sc ...
- Automake使用(中级)
工程地址 automake语言国际化 最初工程目录结构 $ ls -l total 16 drwxrwxr-x. 2 fedora fedora 4096 May 10 10:38 build-aux ...
- Animation Starter Pack中动画蓝图事件添加的位置
可以直接在动画状态机的详情页添加简单事件,跟在动画里添加的通知事件效果一致
- Solr 6.7学习笔记(02)-- 配置文件 managed-schema (schema.xml) - filter(5)
自定义fieldType时,通常还会用到filter.filter必须跟在tokenizer或其它filter之后.如: <fieldType> <analyzer> < ...
- 在Visual Studio中编译Linux的一些问题
相对路径: 在windows下,和当前文件同一个目录下的引用会这么写: #include "SubDirectory\header.h" 或者 #include "Sub ...
- if-else判断语句中经常犯的一个错误
假设题目为:随便给定一个数,三种情况:(1)若小于0,输出为“小于0”:(2)若在0-50之间,则输出“在0-50之间”.(3)若大于50,则输出“大于50”. 解法:如果我这么写,运行一下看看. i ...
- SQL Server插入中文乱码
如果数据库的Collocation是英文的,字段是varchar类型,向表中插入中文数据,会出现乱码. 解决方法: 方法1. 修改varchar 为 nvarchar类型, 并在插入数据前加N,例如: ...
- 常用CMD指令
快捷方式: dcomcnfg.exe 打开windows的组件服务. regedit 打开windows的注册表的界面,进行管理. services.msc 打开service面板 calc ...