聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃、两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一般情况下石头剪刀布就好了,可是他们已经玩儿腻了这种低智商的游戏。他们的爸爸快被他们的争吵烦死了,所以他发明了一个新游戏:由爸爸在纸上画n个“点”,并用n-1条“边”把这n个“点”恰好连通(其实这就是一棵树)。并且每条“边”上都有一个数。接下来由聪聪和可可分别随即选一个点(当然他们选点时是看不到这棵树的),如果两个点之间所有边上数的和加起来恰好是3的倍数,则判聪聪赢,否则可可赢。聪聪非常爱思考问题,在每次游戏后都会仔细研究这棵树,希望知道对于这张图自己的获胜概率是多少。现请你帮忙求出这个值以验证聪聪的答案是否正确。
输入的第1行包含1个正整数n。后面n-1行,每行3个整数x、y、w,表示x号点和y号点之间有一条边,上面的数是w。
以即约分数形式输出这个概率(即“a/b”的形式,其中a和b必须互质。如果概率为1,输出“1/1”)。
#include <algorithm>
#include <cstdio>
#include <cstring>
#define forEdge(x, i) for (int i = pre[(x)]; ~i; i = nxt[i])
typedef long long LL;
const int N = 20050;
int pre[N], nxt[N * 2], to[N * 2], w[N * 2];
int cnt = 0;
int n;
inline void addEdge(int x, int y, int v) {
to[cnt] = y; w[cnt] = v;
nxt[cnt] = pre[x]; pre[x] = cnt++;
to[cnt] = x; w[cnt] = v;
nxt[cnt] = pre[y]; pre[y] = cnt++;
}
bool vis[N];
LL g[3], f[3];
int siz[N], ssiz[N], sizv;
int getRoot(int x, int fa) {
int rt = -1;
siz[x] = 1;
ssiz[x] = 0;
forEdge(x, i) {
int u = to[i];
if (u == fa || vis[u]) continue;
int rt1 = getRoot(u, x);
if (!~rt || ssiz[rt] > ssiz[rt1]) rt = rt1;
siz[x] += siz[u];
ssiz[x] = std::max(ssiz[x], siz[u]);
}
ssiz[x] = std::max(ssiz[x], sizv - siz[x]);
if (!~rt || ssiz[rt] > ssiz[x]) rt = x;
return rt;
}
void mainDfs(int x, int fa, int dis) {
++f[dis %= 3];
forEdge(x, i) {
int u = to[i];
if (u == fa || vis[u]) continue;
mainDfs(u, x, dis + w[i]);
}
}
LL calc(int x) {
LL ans = 0;
g[0] = 1;
g[1] = g[2] = 0;
forEdge(x, i) {
f[0] = f[1] = f[2] = 0;
int u = to[i];
if (vis[u]) continue;
mainDfs(u, x, w[i]);
ans += g[0] * f[0] + g[1] * f[2] + g[2] * f[1];
g[0] += f[0]; g[1] += f[1]; g[2] += f[2];
}
return ans;
}
LL solve(int x) {
vis[x] = 1;
LL ans = calc(x);
forEdge(x, i)
if (!vis[to[i]])
ans += solve(getRoot(to[i], x));
return ans;
}
LL gcd(LL a, LL b) {
return b ? gcd(b, a % b) : a;
}
int main() {
memset(pre, -1, sizeof pre);
int n, x, y, v;
scanf("%d", &n);
for (int i = 1; i < n; ++i) {
scanf("%d%d%d", &x, &y, &v);
addEdge(--x, --y, v);
}
sizv = n;
LL ans = solve(getRoot(1, -1)) * 2 + n;
LL t = (LL)n * n;
LL g = gcd(ans, t);
printf("%lld/%lld\n", ans / g, t / g);
return 0;
}
#include <algorithm>
#include <cstdio>
#include <cstring>
#define forEdge(x, i) for (int i = pre[(x)]; ~i; i = nxt[i])
typedef long long LL;
const int N = 20050;
int pre[N], nxt[N * 2], to[N * 2], w[N * 2];
int cnt = 0;
int n;
LL f[N][3];
inline void addEdge(int x, int y, int v) {
to[cnt] = y; w[cnt] = v;
nxt[cnt] = pre[x]; pre[x] = cnt++;
to[cnt] = x; w[cnt] = v;
nxt[cnt] = pre[y]; pre[y] = cnt++;
}
LL dfs(int x, int fa) {
LL ans = 0;
f[x][0] = 1;
forEdge(x, i) if (to[i] != fa) {
ans += dfs(to[i], x);
for (int k = 0; k < 3; ++k)
ans += f[x][k] * f[to[i]][(6 - k - w[i]) % 3];
for (int k = 0; k < 3; ++k)
f[x][(k + w[i]) % 3] += f[to[i]][k];
}
return ans;
}
LL gcd(LL a, LL b) {
return b ? gcd(b, a % b) : a;
}
int main() {
memset(pre, -1, sizeof pre);
int n, x, y, v;
scanf("%d", &n);
for (int i = 1; i < n; ++i) {
scanf("%d%d%d", &x, &y, &v);
addEdge(--x, --y, v %= 3);
}
LL ans = dfs(0, -1) * 2 + n;
LL t = (LL)n * n;
LL g = gcd(ans, t);
printf("%lld/%lld\n", ans / g, t / g);
return 0;