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 ... 
随机推荐
- 【Python】【demo实验3】【显示素数,显示两个数范围内的所有素数】
			打印两个整数之间的所有素数: (使用平方根来判断 是否应停止验证该数值是否为素数) for i in range(956253526252,9956253526252): k = 1 if i == ... 
- Centos 7 下Gitlab 自启动设置
			禁止 Gitlab 开机自启动: systemctl disable gitlab-runsvdir.service 启用 Gitlab 开机自启动: systemctl enable gitlab- ... 
- Oacle常用语句
			1.建表语句 ) NOT NULL, region_id ) NOT NULL, salesperson_id ) NOT NULL, ) NOT NULL, ) NOT NULL, tot_orde ... 
- %300为0的个数(牛客第四场)--	number
			题意: 给你一串数,问你如题. 思路: 我不是这样的作法,从后往前,先取00,再算%3==0的个数,往前推的时候有递推关系: #define IOS ios_base::sync_with_stdio ... 
- CSS3鼠标悬停翻转按钮
			在线演示 本地下载 
- 富文本编辑器-Ueditor传值
			前两天研究了一下富文本编辑器Ueditor的使用和配置,并且我们已经可以正常的在页面上编辑内容到富文本编辑器中,那么我们如何将输入的内容传到数据库中呢 ? Listen carefully. 首先介绍 ... 
- LoadRunner之使用JSEESIONID访问网站
			LoadRunner使用笔记 JSESSIONID的含义:https://www.cnblogs.com/caiwenjing/p/8081391.html 1.使用JSESSIONID访问网站 Ac ... 
- 2018年4月份,阿里最新的java程序员面试题目,仅供参考。
			目录 技术一面(23问) 技术二面(3大块) 性能优化(21点) 项目实战(34块) JAVA方向技术考察点(15点) JAVA开发技术面试中可能问到的问题(17问) 阿里技术面试1 1.Java I ... 
- 物联网的语言c,python,go等
			日本生鱼片 电热水器的使用方法http://www.hiry.cn/b/mt/33959.html 物联网层次很多,首先要看你从事哪个层级的工作了.既然你问语言,那么肯定是开发类的工作,开发类的对象中 ... 
- docker使用国内镜像加速
			在daemon.json文件里以下国内镜像 { "registry-mirrors": [ "https://registry.docker-cn.com", ... 
