首先先膜杜教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. Document .load与Document .ready的区别

    页面加载完成有两种事件 1.load是当页面所有资源全部加载完成后(包括DOM文档树,css文件,js文件,图片资源等),执行一个函数 问题:如果图片资源较多,加载时间较长,onload后等待执行的函 ...

  2. Windows下安装Mysql5.5.27(社区版)

    所有平台的 MySQL 下载地址为: MySQL 下载. 挑选你需要的 MySQL Community Server 版本及对应的平台. 运行mysql-5.5.27-win32.msi 进入欢迎界面 ...

  3. 332. Reconstruct Itinerary

    class Solution { public: vector<string> path; unordered_map<string, multiset<string>& ...

  4. Go语言使用百度翻译api

    Go语言使用百度翻译api 之前做过一个使用百度翻译api的工具,这个工具用于用户的自动翻译功能,是使用C#调用百度翻译api接口,既然在学习Go语言,那必然也是要使用Go来玩耍一番.这里我是这么安排 ...

  5. Python 基本文件操作

    文件模式 'r' 读模式 'w' 写模式 (清除掉旧有数据并重新开始) 'a' 追加模式 'b' 二进制模式 '+' 读/写模式 注意: 'b'   : 二进制模式 可添加到其他模式中使用 '+'  ...

  6. python csv 模块的使用

    python csv 模块的使用 歌曲推荐:攀登(live) csv 是用逗号分隔符来分隔列与列之间的. 1. csv的写入 1.简单的写入,一次写入一行 import csv with open(& ...

  7. JQuery UI 日历加时间

    写一个面试时间通知.用jquery ui 具体功能已经可以了,不过样式还没调 一.需要引入的文件,这些可以到官网下载 <link rel="stylesheet" href= ...

  8. CSP201503-1:图像旋转

    引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的"计算机职业资格认证"考试, ...

  9. SDOI2013森林

    题面 主席树启发式合并,每次连边维护并查集,集合大小,求lca所需信息,合并两个树上的主席树, 重点看代码. #include <iostream> #include <algori ...

  10. ardupilot_gazebo仿真(四)

    ardupilot_gazebo仿真(四) 标签(空格分隔): 未分类 Multi-MAV simulation 参考官网给出的multi-vehicle-simulation的方法 在每次打开sim ...