Evaluations Gym - 101840E —— dfs

For World Cup marketing purposes, FIFA wants to evaluate the compatibility of different players. Some
players have connections that affect their compatibility scores, like being from the same country or playing
in the same team. These connections are well studied, and for some pairs of players, an integer value is
assigned based on these connections. After such studies, FIFA found out that the set of players with
connections form an undirected tree. In more details, players are the nodes of the tree and edges connect
players with a connection such that the edge’s weight is equal to the value of this connection. It is
guaranteed that these edges will form a tree. The compatibility score of players u, v is:
• If there is an edge (u, v) in the tree, then it is the weight of that edge.
• Otherwise, it is the weight of the path between u and v, such that the weight of a path is the product
of the weights of its edges.
Given the tree of players along with edges’ weights, FIFA is interested in counting the number of distinct
unordered pairs of players u, v such that their compatibility score is a product of exactly two distinct
primes.
Input
The first line of the input contains a single integer 1 ≤ T ≤ 100 the number of test cases. Each test case
consists of n lines, the first line containing a single integer n. Each of the remaining n − 1 lines consists of
3 space separated integers x y w; each of those denotes an edge between the nodes x and y with weight
w; where 1 ≤ x, y ≤ n ≤ 105 and 1 ≤ w ≤ 105
.
Output
For each test case output a single line displaying the case number and the count.
Example
evaluations.in
1
5
1 2 2
2 3 1
1 4 3
1 5 6
standard output
Case 1: 3
Note
An unordered pair is a set of two distinct elements. {a, b} = {b, a}.

题意:

给你n-1条边,每条边上有一个权值,问你有多少对点,使得这对点的路径上所有边的权值相乘正好是两个(不同的)??质数相乘

题解:

他虽然说是不同的质数,但是4还是过了?不懂为什么,可能是数据特别水或者我题目读错了。如果是两个2算相同质数的话,我觉得只需要再加上一个vector和一个判断,vector存放的是构成它的质数,然后搜索的时候判断一下,应该就好了吧,但是直接做就过了我也懒得去搞这些有的没的了。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pa pair<int,int>
#define mp(a,b) make_pair(a,b)
const int N=1e5+5;
struct node
{
    int to,next,val;
}e[N<<1];
int head[N],cnt,p[N],np[N],ctp,num[N];
void init()
{
    for(int i=2;i<N;i++)
    {
        if(!np[i])
        {
            p[++ctp]=i;
            num[i]++;
            for(int j=2*i;j<N;j+=i)
                np[j]=1,num[j]++;
        }
    }
}
void add(int x,int y,int w)
{
    e[cnt].to=y;
    e[cnt].next=head[x];
    e[cnt].val=w;
    head[x]=cnt++;
}
ll dfs(int pos,int fa,int num)
{
    int ans=0;
    for(int i=head[pos];~i;i=e[i].next)
    {
        int ne=e[i].to;
        if(ne==fa)
            continue;
        if(e[i].val+num<=2)
            ans+=dfs(ne,pos,num+e[i].val);
    }
    if(num==2)
        ans++;
    return ans;
}
int main()
{
    init();
    int t;
    freopen("evaluations.in","r",stdin);
    scanf("%d",&t);
    int cas=0;
    while(t--)
    {
        cnt=0;
        memset(head,-1,sizeof(head));
        int n;
        scanf("%d",&n);
        int u,v,w;
        for(int i=1;i<n;i++)
            scanf("%d%d%d",&u,&v,&w),add(u,v,num[w]),add(v,u,num[w]);
        ll ans=0;
        for(int i=1;i<=n;i++)
        {
            ans+=dfs(i,0,0);
        }
        printf("Case %d: %lld\n",++cas,ans/2);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/tianyizhicheng/article/details/83995150