链接: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.

输入描述:

    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.
    The following n-1 lines, each contains three integers uiui, vivi, wiwi(1≤ui,vi≤n,1≤wi≤10001≤ui,vi≤n,1≤wi≤1000), the edge of cities.

输出描述:

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

输入

复制

2
4
1 2 1
2 3 2
2 4 2
4
1 2 3
2 3 3
2 4 3

输出

复制

2
6

题意:给你一个含有n个节点的树,问有多少对节点u,v,他们的距离dist(u,v)%3==0

思路:树上dp我们定义状态 dp[i][0/1/2] 表示第i个节点的子树中,距离第i个节点的距离%3的分别等于0、1、2三种情况的节点个数。然后根据节点之间的关系转移。对于一堆节点u,v。对答案的贡献是:dp[u][j]*dp[v][(3-w-j+3)%3];j 分别取0 1 2然后再把底层的节点信息转移给它的父节点即可、

细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int* p);
const int maxn = 100010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
struct edge
{int next;int to;int dis;edge(){}edge(int nn, int dd){next = nn;dis = dd;}
};
edge e[maxn << 1];
ll ans = 0ll;
int tot;
int head[maxn << 1];
void addedge(int a, int b, int c)
{e[tot].to = b;e[tot].dis = c;e[tot].next = head[a];head[a] = tot++;
}
void init()
{tot = 1;
}
ll dp[maxn][3];void dfs(int id, int pre)
{dp[id][0] = 1ll;for (int i = head[id]; i != 0; i = e[i].next){edge x = e[i];ll w = x.dis;if (x.to != pre){dfs(x.to, id);ans += dp[id][0] * dp[x.to][(3 - w - 0 + 3) % 3];ans += dp[id][1] * dp[x.to][(3 - w - 1 + 3) % 3];ans += dp[id][2] * dp[x.to][(3 - w - 2 + 3) % 3];dp[id][(0 + w) % 3] += dp[x.to][0];dp[id][(1 + w) % 3] += dp[x.to][1];dp[id][(2 + w) % 3] += dp[x.to][2];}}}
int main()
{//freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);//freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);int t;gbtb;cin >> t;while (t--){ans = 0ll;int n;cin >> n;init();int a, b, c;repd(i, 1, n){head[i] = 0;dp[i][0] = dp[i][1] = dp[i][2] = 0;}repd(i, 2, n){cin >> a >> b >> c;c %= 3;addedge(a, b, c);addedge(b, a, c);}dfs(1, 0);cout << ans << endl;}return 0;
}inline void getInt(int* p) {char ch;do {ch = getchar();} while (ch == ' ' || ch == '\n');if (ch == '-') {*p = -(getchar() - '0');while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 - ch + '0';}}else {*p = ch - '0';while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 + ch - '0';}}
}

 
 

转载于:https://www.cnblogs.com/qieqiemin/p/10933120.html

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校赛网络同步赛 Trial of Devil

    链接:https://ac.nowcoder.com/acm/contest/view-submission?submissionId=40669755 来源:牛客网 题目描述 As an acmer ...

  4. 2019长安大学ACM校赛网络同步赛 L XOR

    题意 求区间内有多少数x满足x^4x^5x=0 1≤l≤r≤1018. 题解 根据异或的性质可以推到 x^4x=5x -->x^4x=x+4x 即x和4x每一位都不同,即x和(x<< ...

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

    A,C,I签到题,只搞了8题,还一题是神仙做的,我不会 链接:https://www.nowcoder.com/acm/contest/122/B 来源:牛客网 取石子 时间限制:C/C++ 1秒,其 ...

  6. 南昌大学航天杯第二届程序设计竞赛校赛网络同步赛B 取石子(博弈SG函数模板)

    题目链接:取石子 链接:https://www.nowcoder.com/acm/contest/122/B 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65 ...

  7. 南昌大学航天杯第二届程序设计竞赛校赛网络同步赛 部分题解

    A-ID and password 签到题. #include <iostream> #include <cstdio> #include <bits/stdc++.h& ...

  8. 南昌大学航天杯第二届程序设计竞赛校赛网络同步赛 - 题解

    A - ID and password 题目描述 Users prefer simple passwords that are easy to remember, but such passwords ...

  9. 2016广东工业大学新生杯决赛网络同步赛暨全国新生邀请赛

    2016广东工业大学新生杯决赛网络同步赛暨全国新生邀请赛 Ploblem A :   pigofzhou的巧克力棒 原题链接:http://gdutcode.sinaapp.com/problem.p ...

最新文章

  1. Ubuntu 16.04.1 LTS上安装电源管理系统TLP
  2. php excel parser pro v4.2,php中使用ExcelFileParser处理excel获得数据(可作批量导入到数据库使用)...
  3. python装饰器作用-Python装饰器详解
  4. 无线通信原理及协议栈(ZigBee、蓝牙等)解析
  5. java protobuf 例子_用 Maven 实现一个 protobuf 的 Java语言例子
  6. 蜂鸟智游大数据:为什么出国购物慢慢“OUT”了?
  7. android 图片浏览控件_Android自动化测试23--Appium同步点
  8. 算法之最长公共子序列(LCS)问题
  9. 程序员为什么值得写博客
  10. python编程 书籍_Python编程十大最佳书籍
  11. python gzip压缩_Python gzip –压缩解压缩
  12. 使用BCC工具获取Linux内核空间read/write操作的文件名
  13. .Net1.x转换为.Net 2.0要注意的几个问题
  14. torch.utils.data.Dataset用法
  15. yaahp使用教程_yaahp(yaahp教程使用视频)
  16. 设置360极速浏览器的模式默认为极速模式
  17. python公历转农历_Python 农历公历相互转换
  18. 淘特,阿里在下沉市场的一把好刀
  19. filedownload实现
  20. struct dst_entry *dst

热门文章

  1. 案例研究 | 如何运用DTC模式打造酒店集团会员俱乐部
  2. java求最小公倍数
  3. V4L2+Qt5实现摄像头视频采集以及参数控制
  4. 实战:上亿数据如何秒查
  5. Auto.JS 解决火山、快手自动点赞找不到控件问题
  6. r5 3500u和r5 3550h性能差多少
  7. Spring Data JPA实现多表的关联查询
  8. 回望2020,那些在局里做题的日子
  9. 用JAVA写的21点游戏(BlackJack)
  10. 小米一季度净利暴涨 163.8% 创新高