首先先膜杜教orz

这里简单说一下支配树的概念

支配树是对一个有向图来讲的

规定一个起点s,如果s到v的路径上必须经过某些点u,那么离s最近的点u就是v的支配点

在树上的关系就是,v的父亲是u。

一般图的支配树需要使用tarjan算法,但是如果有向图是没有环的,可以采用另一种做法

按照拓扑序建立支配树,每次加点的时候,枚举能到它的所有点,求它们在当前支配树的最近公共祖先,那个点就是该点的支配点

这个题先建立一个最短路图,易知,这个图是没有环的有向图,所以建立支配树的时候就可以采用以上做法

orz 膜杜教代码,写得太飘逸了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <set>
//对pair的一种有效快捷的利用
#define fi first
#define se second
#define mp make_pair
using namespace std; typedef long long ll;
const int maxn = ;
ll dis[maxn];
int vis[maxn], ord[maxn], deep[maxn], sz[maxn];
int p[maxn][];
//dij时用的堆
set <pair<ll, int> > hs;
//利用pair简化邻接表
vector < pair<int, ll>> e[maxn];
const ll inf = 1ll<<;
void Dijk(int S, int n)
{
for(int i = ; i <= n; i++) dis[i] = inf, vis[i] = ;
dis[S] = ;
for(int i = ; i <= n; i++) hs.insert(mp(dis[i], i));
for(int i = ; i < n; i++)
{
int u = (hs.begin())->se; hs.erase(hs.begin());
vis[u] = ; ord[i] = u; //求最短路时顺便得到拓扑序orz
for(int j = ; j < e[u].size(); j++)
{
int v = e[u][j].fi;
if(dis[v] > dis[u] + e[u][j].se)
{
hs.erase(mp(dis[v], v));
dis[v] = dis[u] + e[u][j].se;
hs.insert(mp(dis[v], v));
}
}
}
} int lca(int u, int v) //二进制倍增求LCA
{
if(deep[u] > deep[v]) swap(u, v);
for(int i = ; i >= ; i--) if(deep[p[v][i]] >= deep[u]) v = p[v][i];
if(u == v) return u;
for(int i = ; i >= ; i--) if(p[v][i] != p[u][i]) u = p[u][i], v = p[v][i];
return p[u][];
}
int n, m, s, u, v, w;
int main()
{
//freopen("a.txt", "r", stdin);
cin>>n>>m>>s;
for(int i = ; i <= m; i++)
{
cin>>u>>v>>w;
e[u].push_back(mp(v, w));
e[v].push_back(mp(u, w));
}
Dijk(s, n);
p[s][] = ; deep[s] = ;
//构建最短路图的过程并建立支配树
for(int i = ; i <= n; i++)
{
int d = -, u = ord[i];
for(auto p : e[u])
{
if(dis[p.fi] + p.se == dis[u])
{
if(d == -) d = p.fi;
else d = lca(d, p.fi);
}
}
p[u][] = d; deep[u] = deep[d]+;
for(int j = ; j < ; j++) p[u][j] = p[p[u][j-]][j-]; //动态更新公共祖先
}
for(int i = ; i <= n; i++) sz[i] = ;
int ret = ;
for(int i = n-; i >= ; i--) //按照拓扑序dp求最大值
{
u = ord[i];
sz[p[u][]] += sz[u];
if(dis[u] <= (1ll<<)) ret = max(ret, sz[u]);
}
cout<<ret<<endl;
}

Codeforces Round #391 div1 757F (Dominator Tree)的更多相关文章

  1. Codeforces Round #543 Div1题解(并不全)

    Codeforces Round #543 Div1题解 Codeforces A. Diana and Liana 给定一个长度为\(m\)的序列,你可以从中删去不超过\(m-n*k\)个元素,剩下 ...

  2. Codeforces Round #545 Div1 题解

    Codeforces Round #545 Div1 题解 来写题解啦QwQ 本来想上红的,结果没做出D.... A. Skyscrapers CF1137A 题意 给定一个\(n*m\)的网格,每个 ...

  3. Codeforces Round #539 Div1 题解

    Codeforces Round #539 Div1 题解 听说这场很适合上分QwQ 然而太晚了QaQ A. Sasha and a Bit of Relax 翻译 有一个长度为\(n\)的数组,问有 ...

  4. Codeforces Round #499 (Div. 1) F. Tree

    Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...

  5. [Codeforces Round #254 div1] C.DZY Loves Colors 【线段树】

    题目链接:CF Round #254 div1 C 题目分析 这道题目是要实现区间赋值的操作,同时还要根据区间中原先的值修改区间上的属性权值. 如果直接使用普通的线段树区间赋值的方法,当一个节点表示的 ...

  6. Educational Codeforces Round 6 E. New Year Tree dfs+线段树

    题目链接:http://codeforces.com/contest/620/problem/E E. New Year Tree time limit per test 3 seconds memo ...

  7. Codeforces Round #353 (Div. 2) D. Tree Construction 二叉搜索树

    题目链接: http://codeforces.com/contest/675/problem/D 题意: 给你一系列点,叫你构造二叉搜索树,并且按输入顺序输出除根节点以外的所有节点的父亲. 题解: ...

  8. Codeforces Educational Codeforces Round 3 E. Minimum spanning tree for each edge LCA链上最大值

    E. Minimum spanning tree for each edge 题目连接: http://www.codeforces.com/contest/609/problem/E Descrip ...

  9. Codeforces Round #540 (Div. 3)--1118F1 - Tree Cutting (Easy Version)

    https://codeforces.com/contest/1118/problem/F1 #include<bits/stdc++.h> using namespace std; in ...

随机推荐

  1. dom4j解析器sax解析xml文件

    1.使用dom4j解析器解析xml ***解析器dom4j是由dom4j组织提供的,针对xml进行解析.dom4j不是Javase的一部分,使用时需要导入dom4j的jar包(官网下载) 在eclip ...

  2. 其他乱七八糟的css

    white-space:normal; word-break:break-all;字母数字强制换行表格宽度失效给上table-layout:fixed(display: table-cell;此元素会 ...

  3. js实现前端的搜索历史记录

    最近在对接前台页面(WEB端)时,产品要求需记录下客户的搜索记录,我们是前后台完全分离的项目,根本不能保存的session域中,没办法,虽然作为后台开发,遇到需求就自己研究了一通,先看一下最终效果图, ...

  4. Axure RP Extension for Chrome安装

    Axure RP Extension for Chrome安装 Axure RP Extension for Chrome是一款谷歌插件,主要可以用来查看原型文件.以前安装插件的时候总是找半天资源,很 ...

  5. mysql数据库的基本使用命令总结

    mysql数据库是一个常用的关系型数据库 关系型数据库核心元素有哪些? 主键:特殊字段,用来唯一标识记录的唯一性 字段:数据列 记录:数据行 数据表:数据行的集合 数据库:数据表的集合 安装.启动.停 ...

  6. css文本截字,超出文本省略号显示

    一.单行文本截字 p { text-overflow: ellipsis;/*显示省略号代替裁剪的文本*/ white-space: nowrap;/*空白处理方式 不换行*/ overflow: h ...

  7. hadoop生态搭建(3节点)-06.hbase配置

    # http://archive.apache.org/dist/hbase/1.2.4/ # ==================================================== ...

  8. pynlpir + pandas 文本分析

    pynlpir是中科院发布的一个分词系统,pandas(Python Data Analysis Library) 是python中一个常用的用来进行数据分析和统计的库,利用这两个库能够对中文文本数据 ...

  9. vue---day03

    1. Vue的生命周期 - 创建和销毁的时候可以做一些我们自己的事情 - beforeCreated - created - beforeMount - mounted - beforeUpdate ...

  10. [POJ1785]Binary Search Heap Construction(笛卡尔树)

    Code #include <cstdio> #include <algorithm> #include <cstring> #define N 500010 us ...