CDOJ 图论专题 A.不是图论 强连通分量+拓扑排序 经典
题目链接 在其中纠错第一次wa代码
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long Ull;
#define MM(a,b) memset(a,b,sizeof(a));
const double eps = 1e-;
const int inf =0x7f7f7f7f;
const double pi=acos(-);
const int maxn=; vector<int> G[maxn+],GG[maxn+];
int n,m,vlue[maxn+],pre[maxn+],deg[maxn],dfs_clock,scc_cnt,sccno[maxn+],lowlink[maxn+];
stack<int> S;
ll ori[maxn+],dp[maxn+];
ll maxx(ll a,ll b)
{
return a>b?a:b;
}
void tarjan(int u)
{
pre[u]=lowlink[u]=++dfs_clock;
S.push(u);
for(int i=;i<G[u].size();i++)
{
int v=G[u][i];
if(!pre[v])
{
tarjan(v);
lowlink[u]=min(lowlink[u],lowlink[v]);
}
else if(!sccno[v])
lowlink[u]=min(lowlink[u],pre[v]);
} if(lowlink[u]==pre[u])
{
scc_cnt++;
while()
{
int x=S.top();S.pop();
sccno[x]=scc_cnt;
if(x==u) break;
}
}
} void find_scc()
{
MM(pre,);
MM(sccno,);
scc_cnt=dfs_clock=;
for(int i=;i<=n;i++)
if(!pre[i])
tarjan(i);
}
set<int> st[maxn];
int main()
{
while(~scanf("%d %d",&n,&m))
{
for(int i=;i<=n;i++)
{
st[i].clear();
G[i].clear();
scanf("%d",&vlue[i]);
} for(int i=;i<=m;i++)
{
int u,v;
scanf("%d %d",&u,&v);
G[u].push_back(v);
} find_scc(); for(int i=;i<=scc_cnt;i++) {
deg[i]=dp[i]=ori[i]=;
GG[i].clear();
}
for(int i=;i<=n;i++)
ori[sccno[i]]+=vlue[i];
if(scc_cnt==) {printf("%lld\n",ori[]);continue;}//需要特判,因为ans初始化为0或者
//不特判但将ans初始化为dp[1]
for(int i=;i<=n;i++)
for(int j=;j<G[i].size();j++)
if(sccno[i]!=sccno[G[i][j]])
{
int u=sccno[i],v=sccno[G[i][j]];
if(!st[u].count(v))//set判断图的连通性
{
GG[u].push_back(v);
st[u].insert(v);
deg[v]++;
}
} ll ans=;
for(int i=;i<=scc_cnt;i++) dp[i]=ori[i]; queue<int> q;//拓扑排序是需要借助BFS的
for(int i=;i<=scc_cnt;i++)
if(!deg[i]) q.push(i); while(q.size())
{
int u=q.front();q.pop();
for(int i=;i<GG[u].size();i++)
{
int v=GG[u][i];
dp[v]=maxx(dp[v],dp[u]+ori[v]);
ans=maxx(ans,dp[v]);
deg[v]--;
if(!deg[v]) q.push(v);
}
}
printf("%lld\n",ans);
}
return ;
}
第一次wa代码:
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long Ull;
#define MM(a,b) memset(a,b,sizeof(a));
const double eps = 1e-;
const int inf =0x7f7f7f7f;
const double pi=acos(-);
const int maxn=; vector<int> G[maxn+],GG[maxn+];
int n,m,vlue[maxn+],ori[maxn+],dp[maxn+],pre[maxn+],dfs_clock,scc_cnt,sccno[maxn+],lowlink[maxn+];
stack<int> S; void tarjan(int u)
{
pre[u]=lowlink[u]=++dfs_clock;
S.push(u);
for(int i=;i<G[u].size();i++)
{
int v=G[u][i];
if(!pre[v])
{
tarjan(v);
lowlink[u]=min(lowlink[u],lowlink[v]);
}
else if(!sccno[v])
lowlink[u]=min(lowlink[u],pre[v]);
} if(lowlink[u]==pre[u])
{
scc_cnt++;
while()
{
int x=S.top();S.pop();
sccno[x]=scc_cnt;
if(x==u) break;
}
}
} void find_scc()
{
MM(pre,);
MM(sccno,);
scc_cnt=dfs_clock=;
for(int i=;i<=n;i++)
if(!pre[i])
tarjan(i);
} int main()
{
while(~scanf("%d %d",&n,&m))
{
for(int i=;i<=n;i++)
{
G[i].clear();
GG[i].clear();
scanf("%d",&vlue[i]);
} for(int i=;i<=m;i++)
{
int u,v;
scanf("%d %d",&u,&v);
G[u].push_back(v);
} find_scc(); for(int i=;i<=scc_cnt;i++) dp[i]=ori[i]=;
for(int i=;i<=n;i++)
ori[sccno[i]]+=vlue[i]; for(int i=;i<=n;i++)
for(int j=;j<G[i].size();j++)
if(sccno[i]!=sccno[G[i][j]])
{
int u=sccno[i],v=sccno[G[i][j]];
if(lower_bound(GG[u].begin(),GG[u].end(),v)==GG[u].end())
GG[u].push_back(v);
}
int ans=;
for(int i=;i<=scc_cnt;i++) dp[i]=ori[i];
for(int i=;i<=scc_cnt;i++)
for(int j=;j<GG[i].size();j++)
{
int v=GG[i][j];
//printf("1::%d %d %d\n",i,v,ori[i],ori[v]);
dp[v]=max(dp[v],dp[i]+ori[v]);
ans=max(ans,dp[v]);
}
printf("%d\n",ans);
}
return ;
}
CDOJ 图论专题 A.不是图论 强连通分量+拓扑排序 经典的更多相关文章
- BZOJ_3887_[Usaco2015 Jan]Grass Cownoisseur_强连通分量+拓扑排序+DP
BZOJ_3887_[Usaco2015 Jan]Grass Cownoisseur_强连通分量+拓扑排序+DP Description In an effort to better manage t ...
- BZOJ_2208_[Jsoi2010]连通数_强连通分量+拓扑排序+手写bitset
BZOJ_2208_[Jsoi2010]连通数_强连通分量+拓扑排序+手写bitset Description Input 输入数据第一行是图顶点的数量,一个正整数N. 接下来N行,每行N个字符.第i ...
- poj 2762(强连通分量+拓扑排序)
题目链接:http://poj.org/problem?id=2762 题意:给出一个有向图,判断任意的两个顶点(u,v)能否从u到达v,或v到达u,即单连通,输出Yes或No. 分析:对于同一个强连 ...
- BZOJ1924:[SDOI2010]所驼门王的宝藏(强连通分量,拓扑排序)
Description Input 第一行给出三个正整数 N, R, C. 以下 N 行,每行给出一扇传送门的信息,包含三个正整数xi, yi, Ti,表示该传送门设在位于第 xi行第yi列的藏宝宫室 ...
- 2019ICPC(银川) - Delivery Route(强连通分量 + 拓扑排序 + dijkstra)
Delivery Route 题目:有n个派送点,x条双向边,y条单向边,出发点是s,双向边的权值均为正,单向边的权值可以为负数,对于单向边给出了一个限制:如果u->v成立,则v->u一定 ...
- POJ 2762 Going from u to v or from v to u?(强连通分量+拓扑排序)
职务地址:id=2762">POJ 2762 先缩小点.进而推断网络拓扑结构是否每个号码1(排序我是想不出来这点的. .. ).由于假如有一层为2的话,那么从此之后这两个岔路的点就不可 ...
- 图论:POJ2186-Popular Cows (求强连通分量)
Popular Cows Description Every cow's dream is to become the most popular cow in the herd. In a herd ...
- 图论相关知识(DFS、BFS、拓扑排序、最小代价生成树、最短路径)
图的存储 假设是n点m边的图: 邻接矩阵:很简单,但是遍历图的时间复杂度和空间复杂度都为n^2,不适合数据量大的情况 邻接表:略微复杂一丢丢,空间复杂度n+m,遍历图的时间复杂度为m,适用情况更广 前 ...
- POJ-2762-Going from u to v or from v to u(强连通, 拓扑排序)
链接: https://vjudge.net/problem/POJ-2762 题意: In order to make their sons brave, Jiajia and Wind take ...
随机推荐
- [转帖]ASML发布Q1季度财报 营收22.3亿欧元,EUV光刻机下半年产能大增 ...
ASML发布Q1季度财报营收22.3亿欧元,EUV光刻机下半年产能大增 ... 孟宪瑞发布于2019-4-18 10:32 https://www.expreview.com/67969.html 一 ...
- Ruby学习中(条件判断, 循环, 异常处理)
一. 条件判断 详情参看:https://www.runoob.com/ruby/ruby-decision.html 1.详情实例(看看就中了) #---------------# # LOL场均人 ...
- Ruby学习中(哈希变量/python的字典, 简单的类型转换)
一. 哈希变量(相当于Python中的字典) 详情参看:https://www.runoob.com/ruby/ruby-hash.html 1.值得注意的 (1). 创建Hash时需注意 # 创建一 ...
- solr学习笔记-入门
solr学习笔记 1.安装前准备 solr依赖java 8 运行环境,所以我们先安装java.如果没有java环境无法启动solr服务,并且会看到如下提示: [root@localhost solr- ...
- myBatis+Spring+SpringMVC框架面试题整理
myBatis+Spring+SpringMVC框架面试题整理(一) 2018年09月06日 13:36:01 新新许愿树 阅读数 14034更多 分类专栏: SSM 版权声明:本文为博主原创文章 ...
- python+minicap(二)
一.push文件至手机中 minicap 的使用有很强的针对性,针对不同架构的CPU和SDK制作了不同的 "minicap" 和 "minicap.so" 文件 ...
- JS基础_标识符
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 一款完美代替微信小程序原生客服消息的工具:
一.设置:无需开发,多种回复(自动+人工) 自动回复形式有3种: 打开客服消息(用户只要和客服互动过一次,再次点击进入,会收到设置好的自动回复) 关键词回复(用户在小程序中回复某个关键词内容时,会 ...
- 总线(bus)简介
内容来自于<Computer Organization>,这是我的一篇学习笔记
- 关于redis的几件小事(七)redis缓存雪崩与穿透
1.缓存雪崩 (1)什么是缓存雪崩 缓存雪崩指的是在同一时刻,缓存大量失效,导致大量的请求直接到了数据库,数据库压力剧增,引起系统崩溃.可能出现的情况有: ①大量的key设置了相同的过期时间,导致在缓 ...