题目大意:

各一个奇环内向森林,求每两个点对间的距离之和。无法到达则距离为-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

  1. #pragma GCC optimize("O3")
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. namespace IO{
  5. template<typename T>
  6. inline void read(T &x){
  7. T i = 0, f = 1; char ch = getchar();
  8. for(; (ch < '0' || ch > '9') && ch != '-'; ch = getchar());
  9. if(ch == '-') f = -1, ch = getchar();
  10. for(; ch <= '9' && ch >= '0'; ch = getchar()) i = (i << 3) + (i << 1) + (ch - '0');
  11. x = i * f;
  12. }
  13. template<typename T>
  14. inline void wr(T x){
  15. if(x < 0) x = -x, putchar('-');
  16. if(x > 9) wr(x / 10);
  17. putchar(x % 10 + '0');
  18. }
  19. }using namespace IO;
  20. const int N = 5e5 + 50, mod = 1e9 + 7;
  21. typedef long long ll;
  22. int n, vt;
  23. struct node{
  24. node *to;
  25. ll dis;
  26. ll bin;
  27. int from_size;
  28. int to_size;
  29. int low;
  30. int id;
  31. int dfn;
  32. int deg;
  33. int in_deg;
  34. int sccno;
  35. int vst;
  36. int mark;
  37. 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){}
  38. }*point[N], pool[N], *tail = pool;
  39. ll clk, scc_cnt;
  40. ll ans, bin[N];
  41. vector<node*> cir[N];
  42. stack<node*> stk;
  43. queue<node*> que;
  44. inline void Tarjan(node *u){
  45. u->low = u->dfn = ++clk;
  46. stk.push(u);
  47. node *v = u->to;
  48. if(!v->dfn){
  49. Tarjan(v);
  50. u->low = min(u->low, v->low);
  51. }
  52. else if(!v->sccno)
  53. u->low = min(u->low, v->dfn);
  54. if(u->low == u->dfn){
  55. node *x;
  56. scc_cnt++;
  57. for(; ; ) {
  58. x = stk.top();
  59. stk.pop();
  60. x->sccno = scc_cnt;
  61. cir[scc_cnt].push_back(x);
  62. if(x == u) break;
  63. }
  64. }
  65. }
  66. inline void dfs(node *u){
  67. u->to_size = cir[u->sccno].size();
  68. u->vst = vt;
  69. if(u->to == u) return;
  70. if(cir[u->to->sccno].size() > 1) {
  71. u->to_size += cir[u->to->sccno].size();
  72. return;
  73. }
  74. if(u->to->vst == vt){
  75. u->to_size += u->to->to_size;
  76. return;
  77. }
  78. dfs(u->to);
  79. u->to_size += u->to->to_size;
  80. }
  81. inline void dfs2(node *u){
  82. u->vst = vt;
  83. if(u->to == u) return;
  84. if(cir[u->to->sccno].size() > 1) {
  85. u->to->mark += 1ll*u->from_size;
  86. return;
  87. }
  88. if(u->to->vst == vt) return;
  89. dfs2(u->to);
  90. }
  91. inline void init_bin(int k){
  92. ll sum = 0;
  93. for(register int i = 0, s = cir[k].size(); i < s; i++) sum = (sum + cir[k][i]->dis) % mod;
  94. node *now = cir[k][0], *last;
  95. 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;
  96. last = now;
  97. now = cir[k][0]->to;
  98. for(register int i = 1, s = cir[k].size(); i < s; i++){
  99. now->bin = (last->bin - 1ll*last->dis * (s - 1) + sum - last->dis) % mod;
  100. last = now, now = now->to;
  101. }
  102. }
  103. int main(){
  104. int _q=50<<20;
  105. char *_p=(char*)malloc(_q)+_q;
  106. __asm__("movl %0, %%esp\n"::"r"(_p));
  107. read(n);
  108. for(register int i = 1; i <= n; i++) bin[i] = (bin[i - 1] + i) % mod;
  109. for(register int i = 1; i <= n; i++) point[i] = tail++, point[i]->id = i;
  110. for(register int i = 1; i <= n; i++){
  111. int x;
  112. ll dis;
  113. read(x);
  114. read(dis);
  115. point[i]->to = point[x];
  116. point[i]->dis = dis;
  117. if(x != i) point[x]->in_deg++, point[x]->deg++;
  118. }
  119. for(register int i = 1; i <= n; i++)
  120. if(!point[i]->dfn)
  121. Tarjan(point[i]);
  122. vt++;
  123. for(register int i = 1; i <= n; i++){
  124. if(cir[point[i]->sccno].size() <= 1) point[i]->from_size = 1;
  125. if(!point[i]->in_deg)
  126. dfs(point[i]), que.push(point[i]);
  127. }
  128. while(!que.empty()){
  129. node *u = que.front(); que.pop();
  130. if(cir[u->to->sccno].size() > 1) continue;
  131. u->to->from_size += u->from_size;
  132. if(!(--u->to->deg)) que.push(u->to);
  133. }
  134. vt++;
  135. for(register int i = 1; i <= n; i++)
  136. if(!point[i]->in_deg)
  137. dfs2(point[i]);
  138. for(register int i = 1; i <= n; i++)
  139. ans = (ans + 1ll*point[i]->dis * (point[i]->to_size - 1) * point[i]->from_size) % mod;
  140. for(register int i = 1; i <= scc_cnt; i++){
  141. if(cir[i].size() <= 1) continue;
  142. init_bin(i);
  143. for(register int j = 0, s = cir[i].size(); j < s; j++){
  144. ans = (ans + 1ll*cir[i][j]->mark * cir[i][j]->bin) % mod;
  145. ans = (ans + 1ll*cir[i][j]->dis * bin[s - 1]) % mod;
  146. }
  147. }
  148. for(register int i = 1; i <= n; i++){
  149. if(cir[point[i]->sccno].size() > 1) point[i]->to_size = cir[point[i]->sccno].size();
  150. ans = ((ans - (n - point[i]->to_size)) % mod + mod) % mod;
  151. }
  152. wr((ans % mod + mod) % mod);
  153. return 0;
  154. }

NOIP 模拟 路径求和 - Tarjan+dfs+码的更多相关文章

  1. 2018.11.08 NOIP模拟 班车(倍增+dfs+bit)

    传送门 对于每个点离线处理出向上走2i2^i2i班车到的最上面的点. 然后每个询问(u,v)(u,v)(u,v)先把(u,v)(u,v)(u,v)倍增到刚好走不到lcalcalca的情况(有一个点如果 ...

  2. 2018.10.04 NOIP模拟 航班(tarjan+树形dp)

    传送门 考场上自己yy了一个双连通只有40分. 然后换根dp求最长路就行了. 代码

  3. 6.17考试总结(NOIP模拟8)[星际旅行·砍树·超级树·求和]

    6.17考试总结(NOIP模拟8) 背景 考得不咋样,有一个非常遗憾的地方:最后一题少取膜了,\(100pts->40pts\),改了这么多年的错还是头一回看见以下的情景... T1星际旅行 前 ...

  4. 【noip模拟赛4】Matrix67的派对 暴力dfs

    [noip模拟赛4]Matrix67的派对   描述 Matrix67发现身高接近的人似乎更合得来.Matrix67举办的派对共有N(1<=N<=10)个人参加,Matrix67需要把他们 ...

  5. NOIP模拟赛20161022

    NOIP模拟赛2016-10-22 题目名 东风谷早苗 西行寺幽幽子 琪露诺 上白泽慧音 源文件 robot.cpp/c/pas spring.cpp/c/pas iceroad.cpp/c/pas ...

  6. 2019.7.29 NOIP模拟测试10 反思总结【T2补全】

    这次意外考得不错…但是并没有太多厉害的地方,因为我只是打满了暴力[还没去推T3] 第一题折腾了一个小时,看了看时间先去写第二题了.第二题尝试了半天还是只写了三十分的暴力,然后看到第三题是期望,本能排斥 ...

  7. 队爷的讲学计划 CH Round #59 - OrzCC杯NOIP模拟赛day1

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的讲学计划 题解:刚开始理解题意理解了好半天,然后发 ...

  8. 10.16 NOIP模拟赛

    目录 2018.10.16 NOIP模拟赛 A 购物shop B 期望exp(DP 期望 按位计算) C 魔法迷宫maze(状压 暴力) 考试代码 C 2018.10.16 NOIP模拟赛 时间:2h ...

  9. 2014-10-31 NOIP模拟赛

        10.30 NOIp  模拟赛   时间 空间 测试点 评测方式 挖掘机(dig.*) 1s 256M 10 传统 黑红树(brtree.*) 2s 256M 10 传统 藏宝图(treas. ...

随机推荐

  1. Behavioral模式之Visitor模式

    1.意图 表示一个作用于某对象结构中的各元素的操作.它使你能够在不改变各元素的类的前提下定义作用于这些元素的新操作. 2.别名 无 3.动机 考虑一个编译器.他将源程序表示为一个抽象语法树.该编译器须 ...

  2. amazeui学习笔记二(进阶开发1)--项目结构structure

    amazeui学习笔记二(进阶开发1)--项目结构structure 一.总结 1.项目结构:是说的amazeui在github上面的项目结构,二次开发amazeui用 二.项目结构structure ...

  3. 3.十分钟读懂——App开发规范的业务流程

    转自:http://www.itdaan.com/blog/2017/12/08/6bc06b3387a8d1238504355a6a1c6743.html 一.主要流程   二.产品立项 工作概述: ...

  4. Project Euler 389 Platonic Dice (概率)

    题目链接: https://projecteuler.net/problem=389 题意: 掷一个正四面体骰子,记点数为\(T\). 掷\(T\)个正六面体骰子,记点数和为\(C\). 掷\(C\) ...

  5. 【习题 6-5 UVA-1600】Patrol Robot

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 设dis[x][y][z]表示到(x,y)连续走了z个墙的最短路 bfs一下就ok [代码] /* 1.Shoud it use l ...

  6. <meta name="viewport" content="width=device-width,initial-scale=1.0">

    meta name="viewport" content="width=device-width,initial-scale=1.0" 解释  <meta ...

  7. keytool用法总结

    一.keytool的概念 keytool 是个密钥和证书管理工具.它使用户能够管理自己的公钥/私钥对及相关证书,用于(通过数字签名)自我认证(用户向别的用户/服务认证自己)或数据完整性以及认证服务.在 ...

  8. 这一篇sigmoid和softmax的比较,讲的不错

    文章: http://blog.csdn.net/u014422406/article/details/52805924 sigmoid函数(也叫逻辑斯谛函数):  引用wiki百科的定义: A lo ...

  9. Spring Boot 2.x 使用 jpa 连接 mysql

    在spring boot网站上生成一个项目,如图: 我使用的是Maven项目,java使用是jdk8(spring boot 2.x必须要jdk8及以上),dependencies分别输入选择 web ...

  10. SQL Server 用链接server 同步MySQL

    --測试环境SQL 2014 在MySql环境: use test ; Create Table Demo(ID int,Name varchar(50)) 在控制面板-管理工具-数据源(ODBC)- ...