NOIP 模拟 路径求和 - Tarjan+dfs+码
题目大意:
各一个奇环内向森林,求每两个点对间的距离之和。无法到达则距离为-1.
分析:
首先Tarjan找出size大于1的连通分量(环),环中的边的贡献可以单独计算。
然后从入度为0的点向内dfs,直到遇见size大于1的环。记录每个点的to_size(朝向环方向有多少个节点),from_size(朝向入度为0的方向有多少个节点),还需要配合拓扑。这一步完成后,
单独的边的贡献就可以算出来了。
接下来计算单独的边与环接上的部分对环上的边的贡献增量。在上面dfs时,就可以将环上碰到的第一个点打上标记mark,表示有多少个点通过此点进入联通块,然后对于每个环中每个有标记的点,可以通过预处理得到bin(一个标记的增量),答案增量就是(mark * bin[size-1])。
最后就是减去不互通的点对。将size>1的联通块中的每个点的tosize置为1,贡献就是\(-\sum{(n - tosize_i)}\).
code
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
namespace IO{
template<typename T>
inline void read(T &x){
T i = 0, f = 1; char ch = getchar();
for(; (ch < '0' || ch > '9') && ch != '-'; ch = getchar());
if(ch == '-') f = -1, ch = getchar();
for(; ch <= '9' && ch >= '0'; ch = getchar()) i = (i << 3) + (i << 1) + (ch - '0');
x = i * f;
}
template<typename T>
inline void wr(T x){
if(x < 0) x = -x, putchar('-');
if(x > 9) wr(x / 10);
putchar(x % 10 + '0');
}
}using namespace IO;
const int N = 5e5 + 50, mod = 1e9 + 7;
typedef long long ll;
int n, vt;
struct node{
node *to;
ll dis;
ll bin;
int from_size;
int to_size;
int low;
int id;
int dfn;
int deg;
int in_deg;
int sccno;
int vst;
int mark;
node():to(NULL), from_size(0), to_size(0), in_deg(0), low(0), dfn(0), deg(0), sccno(0), vst(0), mark(0), dis(0), bin(0){}
}*point[N], pool[N], *tail = pool;
ll clk, scc_cnt;
ll ans, bin[N];
vector<node*> cir[N];
stack<node*> stk;
queue<node*> que;
inline void Tarjan(node *u){
u->low = u->dfn = ++clk;
stk.push(u);
node *v = u->to;
if(!v->dfn){
Tarjan(v);
u->low = min(u->low, v->low);
}
else if(!v->sccno)
u->low = min(u->low, v->dfn);
if(u->low == u->dfn){
node *x;
scc_cnt++;
for(; ; ) {
x = stk.top();
stk.pop();
x->sccno = scc_cnt;
cir[scc_cnt].push_back(x);
if(x == u) break;
}
}
}
inline void dfs(node *u){
u->to_size = cir[u->sccno].size();
u->vst = vt;
if(u->to == u) return;
if(cir[u->to->sccno].size() > 1) {
u->to_size += cir[u->to->sccno].size();
return;
}
if(u->to->vst == vt){
u->to_size += u->to->to_size;
return;
}
dfs(u->to);
u->to_size += u->to->to_size;
}
inline void dfs2(node *u){
u->vst = vt;
if(u->to == u) return;
if(cir[u->to->sccno].size() > 1) {
u->to->mark += 1ll*u->from_size;
return;
}
if(u->to->vst == vt) return;
dfs2(u->to);
}
inline void init_bin(int k){
ll sum = 0;
for(register int i = 0, s = cir[k].size(); i < s; i++) sum = (sum + cir[k][i]->dis) % mod;
node *now = cir[k][0], *last;
for(register int i = 0, s = cir[k].size(); i < s; i++) cir[k][0]->bin = (now->dis * (s - i - 1) + cir[k][0]->bin) % mod, now = now->to;
last = now;
now = cir[k][0]->to;
for(register int i = 1, s = cir[k].size(); i < s; i++){
now->bin = (last->bin - 1ll*last->dis * (s - 1) + sum - last->dis) % mod;
last = now, now = now->to;
}
}
int main(){
int _q=50<<20;
char *_p=(char*)malloc(_q)+_q;
__asm__("movl %0, %%esp\n"::"r"(_p));
read(n);
for(register int i = 1; i <= n; i++) bin[i] = (bin[i - 1] + i) % mod;
for(register int i = 1; i <= n; i++) point[i] = tail++, point[i]->id = i;
for(register int i = 1; i <= n; i++){
int x;
ll dis;
read(x);
read(dis);
point[i]->to = point[x];
point[i]->dis = dis;
if(x != i) point[x]->in_deg++, point[x]->deg++;
}
for(register int i = 1; i <= n; i++)
if(!point[i]->dfn)
Tarjan(point[i]);
vt++;
for(register int i = 1; i <= n; i++){
if(cir[point[i]->sccno].size() <= 1) point[i]->from_size = 1;
if(!point[i]->in_deg)
dfs(point[i]), que.push(point[i]);
}
while(!que.empty()){
node *u = que.front(); que.pop();
if(cir[u->to->sccno].size() > 1) continue;
u->to->from_size += u->from_size;
if(!(--u->to->deg)) que.push(u->to);
}
vt++;
for(register int i = 1; i <= n; i++)
if(!point[i]->in_deg)
dfs2(point[i]);
for(register int i = 1; i <= n; i++)
ans = (ans + 1ll*point[i]->dis * (point[i]->to_size - 1) * point[i]->from_size) % mod;
for(register int i = 1; i <= scc_cnt; i++){
if(cir[i].size() <= 1) continue;
init_bin(i);
for(register int j = 0, s = cir[i].size(); j < s; j++){
ans = (ans + 1ll*cir[i][j]->mark * cir[i][j]->bin) % mod;
ans = (ans + 1ll*cir[i][j]->dis * bin[s - 1]) % mod;
}
}
for(register int i = 1; i <= n; i++){
if(cir[point[i]->sccno].size() > 1) point[i]->to_size = cir[point[i]->sccno].size();
ans = ((ans - (n - point[i]->to_size)) % mod + mod) % mod;
}
wr((ans % mod + mod) % mod);
return 0;
}
NOIP 模拟 路径求和 - Tarjan+dfs+码的更多相关文章
- 2018.11.08 NOIP模拟 班车(倍增+dfs+bit)
传送门 对于每个点离线处理出向上走2i2^i2i班车到的最上面的点. 然后每个询问(u,v)(u,v)(u,v)先把(u,v)(u,v)(u,v)倍增到刚好走不到lcalcalca的情况(有一个点如果 ...
- 2018.10.04 NOIP模拟 航班(tarjan+树形dp)
传送门 考场上自己yy了一个双连通只有40分. 然后换根dp求最长路就行了. 代码
- 6.17考试总结(NOIP模拟8)[星际旅行·砍树·超级树·求和]
6.17考试总结(NOIP模拟8) 背景 考得不咋样,有一个非常遗憾的地方:最后一题少取膜了,\(100pts->40pts\),改了这么多年的错还是头一回看见以下的情景... T1星际旅行 前 ...
- 【noip模拟赛4】Matrix67的派对 暴力dfs
[noip模拟赛4]Matrix67的派对 描述 Matrix67发现身高接近的人似乎更合得来.Matrix67举办的派对共有N(1<=N<=10)个人参加,Matrix67需要把他们 ...
- NOIP模拟赛20161022
NOIP模拟赛2016-10-22 题目名 东风谷早苗 西行寺幽幽子 琪露诺 上白泽慧音 源文件 robot.cpp/c/pas spring.cpp/c/pas iceroad.cpp/c/pas ...
- 2019.7.29 NOIP模拟测试10 反思总结【T2补全】
这次意外考得不错…但是并没有太多厉害的地方,因为我只是打满了暴力[还没去推T3] 第一题折腾了一个小时,看了看时间先去写第二题了.第二题尝试了半天还是只写了三十分的暴力,然后看到第三题是期望,本能排斥 ...
- 队爷的讲学计划 CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的讲学计划 题解:刚开始理解题意理解了好半天,然后发 ...
- 10.16 NOIP模拟赛
目录 2018.10.16 NOIP模拟赛 A 购物shop B 期望exp(DP 期望 按位计算) C 魔法迷宫maze(状压 暴力) 考试代码 C 2018.10.16 NOIP模拟赛 时间:2h ...
- 2014-10-31 NOIP模拟赛
10.30 NOIp 模拟赛 时间 空间 测试点 评测方式 挖掘机(dig.*) 1s 256M 10 传统 黑红树(brtree.*) 2s 256M 10 传统 藏宝图(treas. ...
随机推荐
- 洛谷 P1724 东风谷早苗
P1724 东风谷早苗 题目描述 在幻想乡,东风谷早苗是以高达控闻名的高中生宅巫女.某一天,早苗终于入手了最新款的钢达姆模型.作为最新的钢达姆,当然有了与以往不同的功能了,那就是它能够自动行走,厉害吧 ...
- Windows Route 路由表命令整理
Windows Route 路由表命令 在本地 IP 路由表中显示和修改条目. 语法 route [-f] [-p] [Command [Destination] [mask Netmask] [Ga ...
- HTML基础-第二讲
转自:https://blog.csdn.net/likaier/article/details/326657 我们在第一讲里概括了一下网页的主要框架,现在我们来详细的研究网页的内部细则: 先从它的身 ...
- Centos7安装.Net Core 2.2环境以及部署.Net Core MVC程序(Apache+Jexus环境)
原文:Centos7安装.Net Core 2.2环境以及部署.Net Core MVC程序(Apache+Jexus环境) 1.双11抢购***VPS.配置如下: CPU:2 核 内存:2048 M ...
- Maven搭建Spring Security3.2项目详解
本来是打算在上一篇SpringMVC+Hibernate上写的,结果发现上面那篇 一起整合的,结果发现上一篇内容实在是太长了,就另起一篇,这篇主要是采用 Maven搭建Spring+SpringMVC ...
- 基于mpvue的小程序项目搭建的步骤
mpvue 是美团开源的一套语法与vue.js一致的.快速开发小程序的前端框架,按官网说可以达到小程序与H5界面使用一套代码.使用此框架,开发者将得到完整的 Vue.js 开发体验,同时为 H5 和小 ...
- [D3] Animate Transitions in D3 v4
D3 makes it easy to add meaningful animations to your data visualizations. Whether it’s fading in ne ...
- c编程:僵尸吃大脑
第一行输入一个正整数n 以下每一行输入僵尸已经吃了的大脑数量a,和需要生存必需要吃的大脑数量b.总共n行. 例子输入 3 4 5 3 3 4 3 例子输出 NO BRAINS MMM BRAINS M ...
- PAL相机
输入时钟:27M PCLK:54M SDRAM时钟:80M ADV7393时钟:27M 1024*768 60帧 65MHZ
- Qt开发程序在Windows 10应用须要管理员执行的解决思路
Qt开发程序在Windows 10应用须要管理员执行的解决思路 过了非常长的时间没有公布博客了.可是我依旧努力地开发Qt程序.眼下呢.我发现开发Qt程序在Windows 10上有一个怪现象--有些程序 ...