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 ...
随机推荐
- node.js中的url.parse方法使用说明
node.js中的url.parse方法使用说明:https://blog.csdn.net/swimming_in_it_/article/details/77439975 版权声明:本文为博主原创 ...
- PreparedStatement 以及事务的注意事项
a).PreparedStatement 可以进行批量操作,但是与Statement有一定的区别 1. Statement可以进行不同sql语句的批量操作 即可以同时进行 crud 操作. Strin ...
- JWT了解和实际使用
一.JWT JSON Web Token(JWT)是目前最流行的跨域身份验证解决方案.虫虫今天给大家介绍JWT的原理和用法. 1.跨域身份验证 Internet服务无法与用户身份验证分开.一般过程如下 ...
- Ubuntu14.04安装Caffe(CPU)
一 安装Ubuntu14.04LTS Ubuntu分区 1.SWAP 交换分区:与物理内存相当. 2.“/” 根目录分区:该区大小由硬盘大小而定,10-100G. 3.“HOME” 家目录分区:该区也 ...
- C语言经典100例(1-50)
[程序1] 题目:有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 分析:可填在百位.十位.个位的数字都是1.2.3.4.组成所有的排列后再去掉不满足条件的排列. main ...
- 十大经典排序算法(Python,Java实现)
参照:https://www.cnblogs.com/wuxinyan/p/8615127.html https://www.cnblogs.com/onepixel/articles/7674659 ...
- O023、理解Nova架构
参考https://www.cnblogs.com/CloudMan6/p/5410447.html Compute Service Nova 是OpenStack最核心的服务,负责维护和管理云环 ...
- mysql中有条件的插入语句
今天在参加笔试的过程中,看到一道题,大概意思就是说,当满足了条件就执行插入语句,当时就蒙了,之前从来都没有考虑过满足条件才插入的情况,所以一直都是这样写的 insert into table_name ...
- 部署Flannel网络
部署Flannel网络 部署flannel网络需要执行以下步骤: 1)写入分配的子网段到etcd,供flanneld使用 2)下载二进制包 3)配置Flannel 4)systemd管理Flannel ...
- 12 Python之函数进阶
1. 动态传参 *, ** : 形参: 聚合 位置参数* -> 元组 def func(*args, a, b, c): print(a, b , c, args) func(1,2,3,4,5 ...