Codeforces Round #391 div1 757F (Dominator Tree)
首先先膜杜教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)的更多相关文章
- Codeforces Round #543 Div1题解(并不全)
Codeforces Round #543 Div1题解 Codeforces A. Diana and Liana 给定一个长度为\(m\)的序列,你可以从中删去不超过\(m-n*k\)个元素,剩下 ...
- Codeforces Round #545 Div1 题解
Codeforces Round #545 Div1 题解 来写题解啦QwQ 本来想上红的,结果没做出D.... A. Skyscrapers CF1137A 题意 给定一个\(n*m\)的网格,每个 ...
- Codeforces Round #539 Div1 题解
Codeforces Round #539 Div1 题解 听说这场很适合上分QwQ 然而太晚了QaQ A. Sasha and a Bit of Relax 翻译 有一个长度为\(n\)的数组,问有 ...
- Codeforces Round #499 (Div. 1) F. Tree
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...
- [Codeforces Round #254 div1] C.DZY Loves Colors 【线段树】
题目链接:CF Round #254 div1 C 题目分析 这道题目是要实现区间赋值的操作,同时还要根据区间中原先的值修改区间上的属性权值. 如果直接使用普通的线段树区间赋值的方法,当一个节点表示的 ...
- 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 ...
- Codeforces Round #353 (Div. 2) D. Tree Construction 二叉搜索树
题目链接: http://codeforces.com/contest/675/problem/D 题意: 给你一系列点,叫你构造二叉搜索树,并且按输入顺序输出除根节点以外的所有节点的父亲. 题解: ...
- 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 ...
- 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 ...
随机推荐
- 【linux基于Postfix和Dovecot邮件系统的搭建】
一:PostFixe和Dovecot的简单介绍 Postfix postfix是Wietse Venema在IBM的GPL协议之下开发的MTA(邮件传输代理)软件.postfix是Wietse Ven ...
- GET POST 请求的详细区别
前言: 作为最常见的请求方式 在面试很有可能会被问到 所以在这里做一个简单总结 GET get方法向页面请求发送参数 地址和参数之间用?进行分割 例如 localhost:80/download.ht ...
- hdcms v5.7.0学习笔记
hdcms v5.7.0学习笔记 https://note.youdao.com/ynoteshare1/index.html?id=c404d63ac910eb15a440452f73d6a6db& ...
- mysql5.7数据库与5.7之前版本比较
数据库初始化方式变更 <5.7 版本 mysql_install_db >5.7 版本 bin/mysqld --initialize --user =mysql --basedir=/u ...
- python3 练习题100例 (二十二)输入两个字符串,输出两个字符串集合的并集
题目内容: 输入两个字符串,输出两个字符串集合的并集. 为保证输出结果一致,请将集合内元素排序之后再输出, 如对于集合aset,可输出sorted(aset). 输入格式: 共两行,每一行为一个字符串 ...
- Leecode刷题之旅-C语言/python-118杨辉三角
/* * @lc app=leetcode.cn id=118 lang=c * * [118] 杨辉三角 * * https://leetcode-cn.com/problems/pascals-t ...
- 阴影效果的小 demo
早上没事干,感觉字体阴影的效果还是好看的,那么就来一个小demo吧! 1.这是html 简单的有一个标签或者盒子都可以 <div class="demo11">我爱考试 ...
- ES6 export,import报错
问题描述: 现有两个文件: profile.js const firstName = 'Michael'; const lastName = 'Jackson'; const year = 2018; ...
- C++学习014函数值传递和地址传递
当我们给一个函数传参数的时候,可以直接值传入函数,也给可以把一个地址传入函数 区别就是一个本身不被改变,而另一本身也在改变, 在开发时候都会用到, 这里做下记录 #include <iostre ...
- Selenium页面工厂+数据驱动测试框架
工程的目录结构: pom.xml文件: <?xml version="1.0" encoding="UTF-8"?><project xmln ...