Codeforces 109 C. Lucky Tree


反面考虑,找到所有的边都不是Lucky digits的联通块,若块中的点有x个,则不是 triples 的数量为 :

X*(X-1)*(X-2)-2LL*X*(X-1)*(n-X)

C. Lucky Tree
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 477444 are lucky and 517467 are not.

One day Petya encountered a tree with n vertexes. Besides, the tree was weighted, i. e. each edge of the tree has weight (a positive integer). An edge is lucky if its weight is a lucky number. Note that a tree with n vertexes is an undirected connected graph that has exactlyn - 1 edges.

Petya wondered how many vertex triples (i, j, k) exists that on the way from i to j, as well as on the way from i to k there must be at least one lucky edge (all three vertexes are pairwise distinct). The order of numbers in the triple matters, that is, the triple (1, 2, 3) is not equal to the triple (2, 1, 3) and is not equal to the triple (1, 3, 2).

Find how many such triples of vertexes exist.

Input

The first line contains the single integer n (1 ≤ n ≤ 105) — the number of tree vertexes. Next n - 1 lines contain three integers each: ui viwi (1 ≤ ui, vi ≤ n, 1 ≤ wi ≤ 109) — the pair of vertexes connected by the edge and the edge's weight.

Output

On the single line print the single number — the answer.

Please do not use the %lld specificator to read or write 64-bit numbers in С++. It is recommended to use the cin, cout streams or the %I64d specificator.

Sample test(s)
input
4
1 2 4
3 1 2
1 4 7
output
16
input
4
1 2 4
1 3 47
1 4 7447
output
24
Note

The 16 triples of vertexes from the first sample are: (1, 2, 4), (1, 4, 2), (2, 1, 3), (2, 1, 4), (2, 3, 1), (2, 3, 4), (2, 4, 1), (2, 4, 3), (3, 2, 4), (3, 4, 2), (4, 1, 2), (4, 1, 3), (4, 2, 1), (4, 2, 3), (4, 3, 1), (4, 3, 2).

In the second sample all the triples should be counted: 4·3·2 = 24.


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

using namespace std;

typedef long long int LL;

int n;

struct Edge
{
	int u,v,c,is;
}edge[100100];

bool cmp(Edge a,Edge b)
{
	return a.is<b.is;
}

bool check(int x)
{
	while(x)
	{
		int t=x%10;
		x=x/10;
		if(t==4||t==7)
			continue;
		else return false;
	}
	return true;
}

struct TE
{
	int from,to,next;
}te[100100*2];

int Adj[100100],Size;
bool vis[100100];

void init()
{
	Size=0;
	memset(Adj,-1,sizeof(Adj));
}

void Add_Edge(int u,int v)
{
	te[Size].to=v;
	te[Size].from=u;
	te[Size].next=Adj[u];
	Adj[u]=Size++;
}	

LL X=0;

void dfs(int u)
{
	for(int i=Adj[u];~i;i=te[i].next)
	{
		int v=te[i].to;
		if(vis[v]==false)
		{
			X++;
			vis[v]=true;
			dfs(v);
		}
	}
}

int main()
{
	scanf("%d",&n);
	for(int i=0;i<n-1;i++)
	{
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		edge[i].u=a;edge[i].v=b;edge[i].c=c;
		if(check(c)) edge[i].is=1;
	}
	sort(edge,edge+n-1,cmp);
	init();
	for(int i=0;i<n-1;i++)
	{
		if(edge[i].is==0)
		{
			Add_Edge(edge[i].u,edge[i].v);
			Add_Edge(edge[i].v,edge[i].u);
		}
	}
	LL ans=(LL)n*(LL)(n-1)*(LL)(n-2);
	for(int i=1;i<=n;i++)
	{
		if(vis[i]==false)
		{
			X=0;
			dfs(i);
			ans=ans-X*(X-1)*(X-2)-2LL*X*(X-1)*(n-X);
		}
	}
	printf("%I64d\n",ans);
	return 0;
}




你可能感兴趣的:(Codeforces 109 C. Lucky Tree)