磨魔樹

題目來源:http://oj.flyioi.cn/contest/9/problem/39

題目描述

陶陶的院子裏有一棵由n個點,n-1條樹枝構成的魔樹,陶陶最大的業餘愛好就是磨一磨這棵魔樹,他每一次磨樹都會選擇三個點,不妨設這三點爲a,b,c,從a磨到b,從b磨到c,從c磨到a。(a,b,c的任意一種排列被視爲同一方案)然而這棵樹被磨了許多次,某些樹枝再磨一次就被磨爛了。陶陶認爲若a->b,b->c,c->a上都有樹枝被他磨爛,則這次磨樹是失敗的。現在他想問你,他下一次磨樹,失敗的概率是多少?

輸入格式

 第一行輸入n,表示點的數目
 第二到n行,每行輸入三個數u,v,p。表示u->v有一條邊,若p=1,則這條邊是要爛的邊,否則是不會爛的邊。

輸出格式

輸出的概率爲一個在對1e9+7取模意義下的分數,
即一個整數。

樣例輸入

5
1 2 0
2 3 1
3 4 1
4 5 0

樣例輸出

800000006

樣例解釋

樣例一有4種方案磨爛,一共10種方案,概率爲2/5,取模意義下爲800000006


思路:組成幾個聯通塊,把所有不磨爛的方案算出來就可以了,最後求一個逆元。

/*************************************************************************
    > Author: wzw-cnyali
    > Created Time: 2017/3/12 14:09:15
 ************************************************************************/

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>

using namespace std;

typedef unsigned long long LL;

#define REP(i, a, b) for(register int i = (a), i##_end_ = (b); i <= i##_end_; ++ i)
#define DREP(i, a, b) for(register int i = (a), i##_end_ = (b); i >= i##_end_; -- i)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define mem(a, b) memset((a), b, sizeof(a))

template<typename T> inline bool chkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

int read()
{
	int sum = 0, fg = 1; char c = getchar();
	while(c < '0' || c > '9') { if (c == '-') fg = -1; c = getchar(); }
	while(c >= '0' && c <= '9') { sum = sum * 10 + c - '0'; c = getchar(); }
	return sum * fg;
}

const int Size = 200010;
const int inf = 0x3f3f3f3f;
const LL mod = 1e9 + 7;

LL C[Size][5];

void get_C(int n)
{
	REP(i, 0, n) { C[i][0] = 1; C[i][i <= 3 ? i : 3] = 1; }
	REP(i, 1, n) REP(j, 1, i <= 3 ? i : 3) C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod;
}

int be[Size], to[Size], nxt[Size], w[Size], e;

void add(int x, int y, int z) { to[e] = y; nxt[e] = be[x]; be[x] = e; w[e] = z; e++; }

int size[Size], id[Size], cnt;
bool vis[Size];

void dfs(int x)
{
	vis[x] = 1;
	for(int i = be[x]; i != -1; i = nxt[i])
	{
		int v = to[i];
		if(vis[v]) continue;
		if(!w[i]) { id[v] = id[x]; ++size[id[v]]; }
		else { id[v] = ++cnt; ++size[cnt]; }
		dfs(v);
	}
}

LL extend_gcd(LL a, LL b, LL &x, LL &y)
{
	if(!b) { x = 1; y = 0; return a; }
	LL gcd = extend_gcd(b, a % b, y, x);
	y -= a / b * x;
	return gcd;
}

LL inv(LL p)
{
	LL x, y, gcd = extend_gcd(p, mod, x, y);
	return gcd == 1 ? ((x + mod) % mod) : -1;
}

void init(int n)
{
	get_C(n);
	mem(be, -1);
	e = 0;
}

int main()
{
	int n = read();
	init(n);
	REP(i, 1, n - 1)
	{
		int x = read(), y = read(), z = read();
		add(x, y, z); add(y, x, z);
	}
	
	size[1] = id[1] = cnt = 1;
	dfs(1);
	
	LL sum = C[n][3], ans1 = 0;
	REP(i, 1, cnt)
	{
		if(size[i] == 1) continue;
		ans1 = ((ans1 + C[size[i]][3]) % mod + (C[size[i]][2] * (n - size[i])) % mod) % mod;
	}
	LL ans = ((sum - ans1 + mod) % mod * inv(sum) + mod) % mod;
	printf("%lld\n", ans);
    return 0;
}



發佈了83 篇原創文章 · 獲贊 27 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章