链接:https://ac.nowcoder.com/acm/contest/897/C
来源:牛客网

LaTale
时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld

题目描述

    Legend goes that in the heart of ocean, exists a gorgeous island called LaTale, which has n cities. Specially, there is only one way between every two cities.
    In other words, n cities construct a tree connected by n-1 edges. Each of the edges has a weight w.

Define d(u, v) the length between city u and city v. Under the condition of u differing from v, please answer how many pairs(u, v) in which d(u, v) can be divisible by 3.
    Pair(u,v) and pair(v,u) are considered the same.

输入描述:

  1.     The first line contains an integer number T, the number of test cases.
    For each test case:
    The first line contains an integer n(2≤n≤1052≤n≤105), the number of cities.
  1.     The following n-1 lines, each contains three integers uiui, vivi, wiwi(1ui,vin,1wi10001ui,vin,1wi1000), the edge of cities.

输出描述:

  1. For each test case print the number of pairs required.
示例1

输入

复制

  1. 2
  2. 4
  3. 1 2 1
  4. 2 3 2
  5. 2 4 2
  6. 4
  7. 1 2 3
  8. 2 3 3
  9. 2 4 3

输出

复制

  1. 2
  2. 6
  3.  
  4. 题意:
    给你一个含有n个节点的树,
    问有多少对节点uv,他们的距离dist(u,v)%3==0
  5.  
  6. 思路:
    树上dp
    我们定义状态 dp[i][0/1/2]
    表示第i个节点的子树中,距离第i个节点的距离%3的分别等于012三种情况的节点个数。
    然后根据节点之间的关系转移。
    对于一堆节点uv。对答案的贡献是:
    dp[u][j]*dp[v][(3-w-j+3)%3];
    j 分别取0 1 2
    然后再把底层的节点信息转移给它的父节点即可、
  7.  
  8. 细节见代码:
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <queue>
  7. #include <stack>
  8. #include <map>
  9. #include <set>
  10. #include <vector>
  11. #include <iomanip>
  12. #define ALL(x) (x).begin(), (x).end()
  13. #define rt return
  14. #define dll(x) scanf("%I64d",&x)
  15. #define xll(x) printf("%I64d\n",x)
  16. #define sz(a) int(a.size())
  17. #define all(a) a.begin(), a.end()
  18. #define rep(i,x,n) for(int i=x;i<n;i++)
  19. #define repd(i,x,n) for(int i=x;i<=n;i++)
  20. #define pii pair<int,int>
  21. #define pll pair<long long ,long long>
  22. #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
  23. #define MS0(X) memset((X), 0, sizeof((X)))
  24. #define MSC0(X) memset((X), '\0', sizeof((X)))
  25. #define pb push_back
  26. #define mp make_pair
  27. #define fi first
  28. #define se second
  29. #define eps 1e-6
  30. #define gg(x) getInt(&x)
  31. #define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
  32. using namespace std;
  33. typedef long long ll;
  34. ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
  35. ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
  36. ll powmod(ll a, ll b, ll MOD) {ll ans = ; while (b) {if (b % )ans = ans * a % MOD; a = a * a % MOD; b /= ;} return ans;}
  37. inline void getInt(int* p);
  38. const int maxn = ;
  39. const int inf = 0x3f3f3f3f;
  40. /*** TEMPLATE CODE * * STARTS HERE ***/
  41. struct edge
  42. {
  43. int next;
  44. int to;
  45. int dis;
  46. edge()
  47. {
  48.  
  49. }
  50. edge(int nn, int dd)
  51. {
  52. next = nn;
  53. dis = dd;
  54. }
  55. };
  56. edge e[maxn << ];
  57. ll ans = 0ll;
  58. int tot;
  59. int head[maxn << ];
  60. void addedge(int a, int b, int c)
  61. {
  62. e[tot].to = b;
  63. e[tot].dis = c;
  64. e[tot].next = head[a];
  65. head[a] = tot++;
  66. }
  67. void init()
  68. {
  69. tot = ;
  70. }
  71. ll dp[maxn][];
  72.  
  73. void dfs(int id, int pre)
  74. {
  75. dp[id][] = 1ll;
  76. for (int i = head[id]; i != ; i = e[i].next)
  77. {
  78. edge x = e[i];
  79. ll w = x.dis;
  80. if (x.to != pre)
  81. {
  82. dfs(x.to, id);
  83. ans += dp[id][] * dp[x.to][( - w - + ) % ];
  84. ans += dp[id][] * dp[x.to][( - w - + ) % ];
  85. ans += dp[id][] * dp[x.to][( - w - + ) % ];
  86. dp[id][( + w) % ] += dp[x.to][];
  87. dp[id][( + w) % ] += dp[x.to][];
  88. dp[id][( + w) % ] += dp[x.to][];
  89.  
  90. }
  91. }
  92.  
  93. }
  94. int main()
  95. {
  96. //freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
  97. //freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
  98.  
  99. int t;
  100. gbtb;
  101. cin >> t;
  102. while (t--)
  103. {
  104. ans = 0ll;
  105. int n;
  106. cin >> n;
  107. init();
  108. int a, b, c;
  109. repd(i, , n)
  110. {
  111. head[i] = ;
  112. dp[i][] = dp[i][] = dp[i][] = ;
  113. }
  114. repd(i, , n)
  115. {
  116. cin >> a >> b >> c;
  117. c %= ;
  118. addedge(a, b, c);
  119. addedge(b, a, c);
  120. }
  121. dfs(, );
  122. cout << ans << endl;
  123. }
  124.  
  125. return ;
  126. }
  127.  
  128. inline void getInt(int* p) {
  129. char ch;
  130. do {
  131. ch = getchar();
  132. } while (ch == ' ' || ch == '\n');
  133. if (ch == '-') {
  134. *p = -(getchar() - '');
  135. while ((ch = getchar()) >= '' && ch <= '') {
  136. *p = *p * - ch + '';
  137. }
  138. }
  139. else {
  140. *p = ch - '';
  141. while ((ch = getchar()) >= '' && ch <= '') {
  142. *p = *p * + ch - '';
  143. }
  144. }
  145. }
  1.  
  1.  
  1.  

2019长安大学ACM校赛网络同步赛C LaTale (树上DP)的更多相关文章

  1. 2019长安大学ACM校赛网络同步赛 L XOR (规律,数位DP)

    链接:https://ac.nowcoder.com/acm/contest/897/L 来源:牛客网 XOR 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6 ...

  2. 2019长安大学ACM校赛网络同步赛 J Binary Number(组合数学+贪心)

    链接:https://ac.nowcoder.com/acm/contest/897/J 来源:牛客网 Binary Number 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32 ...

  3. 2019长安大学ACM校赛网络同步赛 B Trial of Devil (递归)

    链接:https://ac.nowcoder.com/acm/contest/897/B来源:牛客网 Trial of Devil 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32 ...

  4. 2019长安大学ACM校赛网络同步赛 M LCM (数论)

    链接:https://ac.nowcoder.com/acm/contest/897/M来源:牛客网 LCM 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65 ...

  5. 南昌大学航天杯第二届程序设计竞赛校赛网络同步赛 I

    链接:https://www.nowcoder.com/acm/contest/122/I来源:牛客网 题目描述 小q最近在做一个项目,其中涉及到了一个计时器的使用,但是笨笨的小q却犯难了,他想请你帮 ...

  6. NOI 2018网络同步赛(游记?)

    刚中考完那段时间比较无聊,报名了一个同步赛,报完名才发现成绩单是要挂到网上的,而且因为报的早给了一个很靠前的考号...那布星啊,赶紧学点东西,于是在一周内学了网络流,Treap以及一些数论. Day1 ...

  7. 第十三届北航程序设计竞赛决赛网络同步赛 B题 校赛签到(建树 + 打标记)

    题目链接  校赛签到 对每个操作之间建立关系. 比较正常的是前$3$种操作,若第$i$个操作属于前$3$种,那么就从操作$i-1$向$i$连一条有向边. 比较特殊的是第$4$种操作,若第$i$个操作属 ...

  8. 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解

    题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...

  9. $NOI$ $2019$ 网络同步赛

    D2T1考试前测了自己造的“假”极限数据,看了看内存发现是118MB,感觉没问题就交上去了. 赛后用Lemon测了一下:MLE 88->0 对于别的题我已经不想再说什么了. update: 由于 ...

随机推荐

  1. 20175221 曾祥杰 数据库MySQL(课下作业,必做)

    数据库MySQL(课下作业,必做) 题目要求: 1. 下载附件中的world.sql.zip, 参考http://www.cnblogs.com/rocedu/p/6371315.html#SECDB ...

  2. 一款基于jQuery的分页插件

    特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...

  3. ACE官网

    ACE的官网是http://www.dre.vanderbilt.edu/~schmidt/ACE.html 文档的官网是https://htmlpreview.github.io/?https:// ...

  4. VLC for Android编译

    编译环境是ubuntu 64bit 全程参考https://wiki.videolan.org/AndroidCompile/ 一:环境准备 1.安装系统 尽量使用最新的ubuntu系统 可以省去很多 ...

  5. 1450:【例 3】Knight Moves

    1450:[例 3]Knight Moves  题解 这道题可以用双向宽度搜索优化(总介绍在  BFS ) 给定了起始状态和结束状态,求最少步数,显然是用BFS,为了节省时间,选择双向BFS. 双向B ...

  6. p3863 序列

    分析 按照时间为下标分块 块内按照大小排序 每次整块整体修改半块暴力重构即可 代码 #include<bits/stdc++.h> using namespace std; #define ...

  7. 创建Stream

    1.创建Stream create Stream from Collections; create Stream from values; create Stream from Arrays; cra ...

  8. Linux_Grub2、系统启动流程_RHEL7

    目录 目录 前言 系统启动流程 控制RHEL7启动过程 编辑gurbcfg RHEL7启动级别 修改系统运行级别 RHEL7破密码步骤 grup2加密防止破密码 initramfs文件 前言 RHEL ...

  9. Linux下去掉^M方法

    由于windows和Linux文件格式不同,windows下文件在Linux下行尾会有^M 去掉^M方法 sed -i ‘s/^M//g' filename #注意:^M的输入方式是 Ctrl + v ...

  10. 阶段3 1.Mybatis_05.使用Mybatis完成CRUD_2 Mybatis的CRUD-保存操作

    增加的方法 修改映射配置 id是方法名 按照原来jdbc的写法 values里面应该是一堆问号,现在这里不能再去写问号了因为要取值 从我们要执行的方法传的参数里面去取值 所以参数的类型我们必须要告诉这 ...