tarjan模板
tarjan
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <iostream>
using namespace std; #define ll long long const int maxn=1e4+;
const int inf=1e9;
const double eps=1e-; struct node
{
int d;
node *to;
}*e[maxn]; int id,dfn[maxn],low[maxn],tot_st,st[maxn],m,group[maxn];
bool vis[maxn],vis_st[maxn]; /**
id:编号 每增加一个点,++id
每增加一个点,加入栈中
low:点的编号
dfn:点以及后续点通过一条边能到的最小编号点的编号 同一个类 dfn[d]=low[d] 栈中,第x个数d到栈顶 任意点的low值为dfn[d]
st:栈 若点变为在一个类中时,从栈中弹出
vis_st : 当点还未标记到类中时,可以使用 m:类的个数
group:每个点所在的类
**/ void tarjan(int d)
{
int dd;
vis[d]=;
dfn[d]=low[d]=++id;
st[++tot_st]=d;
node *p=e[d];
while (p)
{
dd=p->d;
if (!vis[dd])
{
tarjan(dd);
low[d]=min(low[d],low[dd]);
}
else if (!vis_st[dd])
low[d]=min(low[d],dfn[dd]);
p=p->to;
}
if (dfn[d]==low[d])
{
m++;
int g=tot_st;
while (st[tot_st]!=d)
{
group[st[tot_st]]=m;
vis_st[st[tot_st]]=;
tot_st--;
}
group[st[tot_st]]=m;
vis_st[st[tot_st]]=;
tot_st--;
g-=tot_st; ///类的大小
}
} int main()
{
node *p;
int n,q,x,y,i;
scanf("%d%d",&n,&q);
while (q--)
{
scanf("%d%d",&x,&y);
///注意单边还是双边
p=new node();
p->d=y;
p->to=e[x];
e[x]=p;
}
for (i=;i<=n;i++)
if (!vis[i])
tarjan(i);
return ;
}
/* */
luogu P1726 上白泽慧音
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <iostream>
using namespace std; #define ll long long const int maxn=1e4+;
const int inf=1e9;
const double eps=1e-; struct node
{
int d;
node *to;
}*e[maxn]; int id,dfn[maxn],low[maxn],tot_st,st[maxn],m,group[maxn]; ///m:类的个数
bool vis[maxn],vis_st[maxn]; ///st:栈,存储未被'类‘标签的点 int ind,maxg=,md; void tarjan(int d)
{
int dd;
vis[d]=;
dfn[d]=low[d]=++id;
st[++tot_st]=d;
node *p=e[d];
while (p)
{
dd=p->d;
if (!vis[dd])
{
tarjan(dd);
low[d]=min(low[d],low[dd]);
}
else if (!vis_st[dd])
low[d]=min(low[d],dfn[dd]);
p=p->to;
}
if (dfn[d]==low[d])
{
m++;
int g=tot_st,mind=inf;
while (st[tot_st]!=d)
{
group[st[tot_st]]=m;
mind=min(mind,st[tot_st]);
vis_st[st[tot_st]]=;
tot_st--;
}
group[st[tot_st]]=m;
mind=min(mind,st[tot_st]);
vis_st[st[tot_st]]=;
tot_st--;
g-=tot_st;
if (maxg<g || (maxg==g && mind<md))
ind=m,maxg=g,md=mind;
}
} int main()
{
node *p;
int n,q,x,y,z,i;
scanf("%d%d",&n,&q);
while (q--)
{
scanf("%d%d%d",&x,&y,&z);
p=new node();
p->d=y;
p->to=e[x];
e[x]=p;
if (z==)
{
p=new node();
p->d=x;
p->to=e[y];
e[y]=p;
}
}
for (i=;i<=n;i++)
if (!vis[i])
tarjan(i);
printf("%d",maxg);
bool v=;
for (i=;i<=n;i++)
if (group[i]==ind)
{
if (!v)
printf("\n"),v=;
else
printf(" ");
printf("%d",i);
}
return ;
}
/* */
luogu P3387 【模板】缩点
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <iostream>
using namespace std; #define ll long long const int maxn=1e4+;
const int inf=1e9;
const double eps=1e-; /*
题目描述
给定一个n个点m条边有向图,每个点有一个权值,求一条路径,使路径经过的点权值之和最大。你只需要求出这个权值和。
允许多次经过一条边或者一个点,但是,重复经过的点,权值只计算一次。 第一行,n,m
第二行,n个整数,依次代表点权
第三至m+2行,每行两个整数u,v,表示u->v有一条有向边 共一行,最大的点权之和。 2 2
1 1
1 2
2 1 2
*/ struct node
{
int d;
node *to;
}*e[maxn],*gr[maxn]; int id,dfn[maxn],low[maxn],tot_st,st[maxn],m,group[maxn];
bool vis[maxn],vis_st[maxn];
int v[maxn],ans[maxn];/// /**
id:编号 每增加一个点,++id
每增加一个点,加入栈中
low:点的编号
dfn:点以及后续点通过一条边能到的最小编号点的编号 同一个类 dfn[d]=low[d] 栈中,第x个数d到栈顶 任意点的low值为dfn[d]
st:栈 若点变为在一个类中时,从栈中弹出
vis_st : 当点还未标记到类中时,可以使用 m:类的个数
group:每个点所在的类
**/ void tarjan(int d)
{
int dd;
vis[d]=;
dfn[d]=low[d]=++id;
st[++tot_st]=d;
node *p=e[d];
while (p)
{
dd=p->d;
if (!vis[dd])
{
tarjan(dd);
low[d]=min(low[d],low[dd]);
}
else if (!vis_st[dd])
low[d]=min(low[d],dfn[dd]);
p=p->to;
}
if (dfn[d]==low[d])
{
m++;
while (st[tot_st]!=d)
{
group[st[tot_st]]=m; ///
vis_st[st[tot_st]]=;
tot_st--;
}
group[st[tot_st]]=m; ///
vis_st[st[tot_st]]=;
tot_st--;
}
} void dfs(int d)
{
node *p=gr[d];
int dd,add=;
vis[d]=;
while (p)
{
dd=p->d;
if (!vis[dd])
dfs(dd);
add=max(add,ans[dd]); ///点d到达下一个点,最大的值 千万注意这个是放在外面的
p=p->to;
}
ans[d]+=add;
} int main()
{
node *p,*r;
int n,q,x,y,i,d;
scanf("%d%d",&n,&q); for (i=;i<=n;i++) ///点权
scanf("%d",&v[i]); while (q--)
{
scanf("%d%d",&x,&y);
///注意单边还是双边
p=new node();
p->d=y;
p->to=e[x];
e[x]=p;
}
for (i=;i<=n;i++)
if (!vis[i])
tarjan(i); for (i=;i<=n;i++)
{
ans[group[i]]+=v[i]; ///同一类,能互相访问所有的点
p=e[i];
while (p)
{
d=p->d;
if (group[i]!=group[d])
{
r=new node(); ///变量名要有区分
r->d=group[d];
r->to=gr[group[i]];
gr[group[i]]=r; // printf("%d %d\n",group[i],group[d]);
}
p=p->to;
}
} ///tarjan+缩点后无环
memset(vis,,sizeof(vis));
for (i=;i<=n;i++)
if (!vis[i])
dfs(i);
int maxa=;
for (i=;i<=n;i++)
maxa=max(maxa,ans[i]);
printf("%d",maxa);
return ;
}
/*
3 2
1 2 3
1 2
1 3
*/
luogu P3388 【模板】割点(割顶)
tarjan模板的更多相关文章
- 图论算法-Tarjan模板 【缩点;割顶;双连通分量】
图论算法-Tarjan模板 [缩点:割顶:双连通分量] 为小伙伴们总结的Tarjan三大算法 Tarjan缩点(求强连通分量) int n; int low[100010],dfn[100010]; ...
- UOJ #146. 【NOIP2015】信息传递 连通分量 tarjan模板题
http://uoj.ac/problem/146 题解:强连通分量 tarjan模板题.同时试了一下codeblock #include<bits/stdc++.h> using nam ...
- 学渣乱搞系列之Tarjan模板合集
学渣乱搞系列之Tarjan模板合集 by 狂徒归来 一.求强连通子图 #include <iostream> #include <cstdio> #include <cs ...
- 洛谷1726 上白泽慧音 tarjan模板
题目描述 在幻想乡,上白泽慧音是以知识渊博闻名的老师.春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄.因此慧音决定换一个能够聚集最多人数的村庄作为新的教学地点.人间 ...
- 算法问题实战策略 MEETINGROOM 附一份tarjan模板
地址 https://algospot.com/judge/problem/read/MEETINGROOM 解答 2-sat 代码样例过了 没有ac. 我又没有正确代码对拍..... 已确认是输出 ...
- POJ 2186:Popular Cows Tarjan模板题
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25945 Accepted: 10612 De ...
- Tarjan 模板,高级并查集
第一个模板有误!!!! 请见谅!!! 要怪就怪HDU吧,竟然让我过了 第二个模板是正确的.请翻到下面看更新 HDU 1269 评论区居然有人说用并查集过了,其实回想一下 求无向图的连通分量,就是并查集 ...
- 强连通分量(Tarjan)模板
贴模板,备忘. 模板1: #include<iostream> #include<cstring> #include<cmath> #include<cstd ...
- HDU:1269-迷宫城堡(tarjan模板)
迷宫城堡 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem Descri ...
随机推荐
- Go-Ethereum 1.7.2 结合 Mist 0.9.2 实现代币智能合约的实例
目录 目录 1.什么是 Mist 2.Mist 在哪里下载? 3.Mist 有哪些依赖? 4.如何安装 Mist? 4.1.安装 Mist 依赖工具包 4.2.安装 Mist 4.3.启动 Mist, ...
- 启动期间的内存管理之引导分配器bootmem--Linux内存管理(十)
在内存管理的上下文中, 初始化(initialization)可以有多种含义. 在许多CPU上, 必须显式设置适用于Linux内核的内存模型. 例如在x86_32上需要切换到保护模式, 然后内核才能检 ...
- python3.x pool.map方法的实质
我使用多进程的一般方式,都是multiprocessing模块中的Pool.map()方法.下面写一个简单的示例和解析.至于此种方法使用多进程的效率问题,还希望大佬予以指正. 示例: "&q ...
- 指定IP地址进行远程访问服务器设置方法(windows系统)
我们有很多服务器经常受到外界网络的干扰,入侵者们通过扫描3389端口爆破密码非法进入我们的服务器,这时,我们可以配置服务器IP 安全策略来限制一些IP访问,大大提高了服务器的安全. 实验环境: ...
- Thinkphp volist 多重循环原样输出数组key值的使用总结
最近因为项目的缘故,要使用到volist.在这个过程中,遇到了一些小问题,主要就是volist在循环输出多重数据的时候,如何输出key.网上查阅了不少资料,很失望的是,大多资料就是粘贴复制Thinkp ...
- Windows下的Nessus安装与启动
Windows下的Nessus安装与启动 一.安装 在https://www.tenable.com/downloads/nessus下载对应windows版本 双击安装,完成后,访问 https:/ ...
- 本机不安装Oracle客户端,使用PL/SQL Developer和 Instant Client 工具包连接oracle 11g远程数据库
一.先到Oracle网站下载Instant Client 下载地址:http://www.oracle.com/technetwork/cn/database/features/instant-cli ...
- socketServer并发处理socket
socketserver简单介绍 ''' socketserver:是对socket的封装,实现并发处理 前两个TCP,UDP常用,后两个不常用 ''' import socketserver soc ...
- org.apache.ibatis.builder.IncompleteElementException: Could not find result map com.hp.entity.Emp
错误提示代码: org.apache.ibatis.builder.IncompleteElementException: Could not find result map com.hp.entit ...
- web框架开发-模板层
你可能已经注意到我们在例子视图中返回文本的方式有点特别. 也就是说,HTML被直接硬编码在 Python代码之中. def current_datetime(request): now = datet ...