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 ...
随机推荐
- 使用parquet-hadoop.jar包解析hive parquet文件时,遇到FIXED_LEN_BYTE_ARRAY转换为Decimal 以及 INT96转换为timestamp问题
在使用parquet-hadoop.jar包解析parquet文件时,遇到decimal类型的数据为乱码,具体解决方法如下: 使用parquet-Hadoop.jar解析httpfs服务提供的parq ...
- Java Lock的使用
+ ReentrantLock类的使用 + ReentrantReadWriteLock类的使用 1. 使用ReentrantLock类 ReentrantLock类能够实现线程之间同步互斥,并且在扩 ...
- Mysql学习(二)之通过homebrew安装mysql后,为什么在系统偏好设置里没有mysql
原因 用brew install packagename是用来安装命令行工具的,一般不可能影响到图形界面. mysql官方文档是通过dmg文件安装的: The MySQL Installation P ...
- python 利用已有Ner模型进行数据清洗合并
# -*- coding: utf-8 -*- from kashgari.corpus import DataReader import re from tqdm import tqdm def c ...
- RabbitMQ核心技术总结
RabbitMQ和kafka类似,也是一个消息服务.RabbitMQ是轻量级的,易于部署在内部和云端.RabbitMQ支持多种消息协议,可以部署在分布式集群中,能够满足高规模,高可用性要求.Rabbi ...
- 给Tomcat打开远程debug端口
>cd apache-tomcat-8.5.24 >cd conf >vim catalina.sh 在文件开始处添加: CATALINA_OPTS="-server -X ...
- Nginx(web服务器)与Tomcat(应用服务器)搭建集群
Nginx作为互联网最常用的web服务器,高性能的HTTP和反向代理使它经常作为Tomcat集群的方案.Nginx官方只支持使用HTTP协议的集成,但是如果你想使用AJP协议集成,可以使用阿里开源的n ...
- java多线程ExecutorService
1.new Thread的弊端 执行一个异步任务你还只是如下new Thread吗? new Thread(new Runnable() { @Override public void run() { ...
- rabbitmq一键部署脚本
1.新建一个名字叫 auto_install_rabbitmq.sh 的文件 2.将下面脚本拷贝到文件中,具体操作步骤在注释里面 #环境 linux #一键安装rabitmq,在linux环境中使用 ...
- orcle_day02
第三章:单值函数 函数分为: 1.单值函数 1.字符函数 2.日期函数 3.转换函数 4.数字函数 2.分组函数(后面的章节再做学习) 哑表dual dual是一个虚拟表,用来构成select的语法规 ...