P2634 [国家集训队]聪聪可可(树上启发式合并)
Posted Jozky86
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了P2634 [国家集训队]聪聪可可(树上启发式合并)相关的知识,希望对你有一定的参考价值。
题意:
一颗n个点的树,问其中两点之间的边上数的和加起来是3的倍数的点对有多少个?
输出这样的点对所占比例
题解:
没有修改,统计边长为3的倍数,经典的树上路径统计,树上启发式请求一战
但是调了一阵子没调出来,我对dsu的理解还是不够深,
代码:
待修改代码
#include <bits/stdc++.h>
#include <unordered_map>
#define debug(a, b) printf("%s = %d\\n", a, b);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
clock_t startTime, endTime;
//Fe~Jozky
const ll INF_ll= 1e18;
const int INF_int= 0x3f3f3f3f;
void read(){};
template <typename _Tp, typename... _Tps> void read(_Tp& x, _Tps&... Ar)
{
x= 0;
char c= getchar();
bool flag= 0;
while (c < '0' || c > '9')
flag|= (c == '-'), c= getchar();
while (c >= '0' && c <= '9')
x= (x << 3) + (x << 1) + (c ^ 48), c= getchar();
if (flag)
x= -x;
read(Ar...);
}
template <typename T> inline void write(T x)
{
if (x < 0) {
x= ~(x - 1);
putchar('-');
}
if (x > 9)
write(x / 10);
putchar(x % 10 + '0');
}
void rd_test()
{
#ifdef LOCAL
startTime= clock();
freopen("in.txt", "r", stdin);
#endif
}
void Time_test()
{
#ifdef LOCAL
endTime= clock();
printf("\\nRun Time:%lfs\\n", (double)(endTime - startTime) / CLOCKS_PER_SEC);
#endif
}
const int maxn= 1e5 + 9;
vector<PII> vec[maxn];
int sz[maxn];
int dis[maxn];
int son[maxn];
int Son;
int num[3];
int ans= 0;
void dfs_son(int u, int fa)
{
sz[u]= 1;
for (auto it : vec[u]) {
int v= it.first;
int w= it.second;
if (v == fa)
continue;
dis[u]= dis[fa] + w;
dfs_son(v, u);
sz[u]+= sz[v];
if (sz[v] > sz[son[u]])
son[u]= v;
}
}
void cal(int u, int fa, int LCA)
{
int x= ((dis[LCA] - dis[u]) % 3 + 3) % 3;
ans+= num[x];
for (auto it : vec[u]) {
int v= it.first;
if (v != fa && v != son[u])
cal(v, u, LCA);
}
}
void add(int u, int fa, int val, int LCA)
{
int Dis= (dis[u] - dis[LCA]) % 3;
num[Dis]+= val;
for (auto it : vec[u]) {
int v= it.first;
if (v != fa && v != son[u])
add(v, u, val, LCA);
}
}
void work(int u, int fa, int keep)
{
for (auto it : vec[u]) {
int v= it.first;
if (v != fa && v != son[u])
work(v, u, 0);
}
if (son[u]) {
work(son[u], u, 1);
Son= son[u];
}
add(u, fa, 1, u);
Son= 0;
cal(u, fa, u);
if (!keep) {
add(u, fa, -1, u);
}
}
int main()
{
//rd_test();
int n;
read(n);
for (int i= 1; i < n; i++) {
int u, v, w;
read(u, v, w);
vec[u].push_back({v, w});
vec[v].push_back({u, w});
}
dis[1]= 1;
dfs_son(1, 0);
work(1, 0, 1);
ll a= ans + n;
ll b= 1ll * n * n;
ll g= __gcd(a, b);
printf("%lld/%lld\\n", a / g, b / g);
//Time_test();
}
洛谷上的AC代码:
#include <bits/stdc++.h>
using namespace std;
#define For(pos) for (int k= First[pos]; k; k= Next[k])
const int Maxn= 2e4 + 5;
int n, First[Maxn], to[Maxn * 2], Next[Maxn * 2], W[Maxn * 2], cnt;
int son[Maxn], size[Maxn], deep[Maxn];
inline void add(int z, int y, int w)
{
Next[++cnt]= First[z];
First[z]= cnt;
to[cnt]= y;
W[cnt]= w;
}
int ans= 0, P;
inline int R()
{
char c;
int sign= 1, res= 0;
while ((c= getchar()) > '9' || c < '0')
if (c == '-')
sign= -1;
res+= c - '0';
while ((c= getchar()) >= '0' && c <= '9')
res= res * 10 + c - '0';
return res * sign;
}
void deal(int pos, int father)
{
size[pos]= 1;
For(pos)
{
if (to[k] == father)
continue;
deep[to[k]]= deep[pos] + W[k];
deal(to[k], pos);
size[pos]+= size[to[k]];
if (size[son[pos]] < size[to[k]])
son[pos]= to[k];
}
}
int q[4];
inline void cal(int pos, int LCA)
{
int x= (2 * deep[LCA] - deep[pos]) % 3 + 3;
x= x % 3;
ans+= q[x];
}
void work(int pos, int father, bool ca, int LCA)
{
if (ca)
cal(pos, LCA);
else
q[deep[pos] % 3]++;
For(pos)
{
if (to[k] == father)
continue;
work(to[k], pos, ca, LCA);
}
}
void dfs(int pos, int father, bool heavy)
{
For(pos) if (to[k] != father && to[k] != son[pos])
{
dfs(to[k], pos, 0);
}
if (son[pos])
dfs(son[pos], pos, 1);
For(pos)
{
if (to[k] == father || to[k] == son[pos])
continue;
work(to[k], pos, 1, pos);
work(to[k], pos, 0, pos);
}
cal(pos, pos);
q[deep[pos] % 3]++;
if (!heavy)
q[0]= q[1]= q[2]= 0;
}
int main()
{
n= R();
int a, b, w;
for (int i= 1; i < n; i++) {
a= R();
b= R();
w= R();
add(a, b, w);
add(b, a, w);
}
deal(1, 0);
dfs(1, 0, 1);
ans= ans * 2 + n;
int di= n * n;
for (int i= 2; i <= ans; i++)
while (ans % i == 0 && di % i == 0) {
ans/= i;
di/= i;
}
if (ans == di)
puts("1/1");
else
printf("%d/%d\\n", ans, di);
}
以上是关于P2634 [国家集训队]聪聪可可(树上启发式合并)的主要内容,如果未能解决你的问题,请参考以下文章