链接: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 = ; while (b) {if (b % )ans = ans * a % MOD; a = a * a % MOD; b /= ;} return ans;}
inline void getInt(int* p);
const int maxn = ;
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 << ];
ll ans = 0ll;
int tot;
int head[maxn << ];
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 = ;
}
ll dp[maxn][]; void dfs(int id, int pre)
{
dp[id][] = 1ll;
for (int i = head[id]; i != ; i = e[i].next)
{
edge x = e[i];
ll w = x.dis;
if (x.to != pre)
{
dfs(x.to, id);
ans += dp[id][] * dp[x.to][( - w - + ) % ];
ans += dp[id][] * dp[x.to][( - w - + ) % ];
ans += dp[id][] * dp[x.to][( - w - + ) % ];
dp[id][( + w) % ] += dp[x.to][];
dp[id][( + w) % ] += dp[x.to][];
dp[id][( + w) % ] += dp[x.to][]; }
} }
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, , n)
{
head[i] = ;
dp[i][] = dp[i][] = dp[i][] = ;
}
repd(i, , n)
{
cin >> a >> b >> c;
c %= ;
addedge(a, b, c);
addedge(b, a, c);
}
dfs(, );
cout << ans << endl;
} return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}

 
 

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. 分布式工作流任务调度系统Easy Scheduler正式开源

    分布式工作流任务调度系统Easy Scheduler正式开源 1.背景 在多位技术小伙伴的努力下,经过近2年的研发迭代.内部业务剥离及重构,也经历一批种子用户试用一段时间后,EasyScheduler ...

  2. [学习笔记] Gibbs Sampling

    Gibbs Sampling Intro Gibbs Sampling 方法是我最近在看概率图模型相关的论文的时候遇见的,采样方法大致为:迭代抽样,最开始从随机样本中抽样,然后将此样本作为条件项,按条 ...

  3. leetcode-easy-array-122 best time to buy and sell stocks II

    mycode  69.45% class Solution(object): def maxProfit(self, prices): """ :type prices: ...

  4. maven依赖传递和排除依赖冲突

    1 依赖的传递 假如 A项目 依赖 a.jar 1.0.1,b.jar 1.0.1,没有直接依赖c.jar 1.0.1,但是b.jar 1.0.1依赖了c.jar 1.0.1,可以说A项目间接依赖了c ...

  5. 让DOM元素自动滚到视野内ScrollIntoView

    概述 项目中需要把一个DOM元素自动滚动到视野内,百思不得其解,最后再element库里面发现了这个方法,记录下来供以后开发时参考,相信对其他人也有用. 参考资料:element scroll-int ...

  6. 六:flask-自定义URL转换器

    flask进行url参数匹配的时候,是继承的werkzeug.routing.BaseConverter库进行重写的 导入看源码 里面有所有的URL参数数据类型的判断 也就是说,可以继承过后实现自己的 ...

  7. Jmeter之非GUI模式(命令行)执行

    在使用Jmeter进行性能测试时,建议使用非GUI模式执行. 命令行启动 1.进入jmeter安装的bin目录 2.执行Jmeter命令 如下: (1.jmeter.bat -n -t E:\apac ...

  8. 32 位bitmap 内存存储 顺序 bgra 前3位 与23位一致。 都是 bgr 呵呵 与rgb 相反

    32 位bitmap     内存存储 顺序   bgra       前3位 与23位一致.   都是 bgr  呵呵 与rgb 相反

  9. CentOS7 yum报 Cannot retrieve metalink for repository: epel/x86_64. Please verify its path解决方法

    打开/etc/yum.repos.d/epel.repo,将 [epel] name=Extra Packages for Enterprise Linux 6 – $basearch baseurl ...

  10. 封装一个windows转发端口的脚本

    使用方法: 1.打开文本编辑工具如(Notepad++) 2.新建文件 3.注意:修改文本的编码字符集为:gb2312 4.将下面代码 复制入文件 5.保存文件名为:transmit.bat 6.双击 ...