Problem E. Evaluations Gym - 101840E 很留皮的题

这是我的代码和我的搭档的代码的结合体,当然自己的代码根本不对(理解问题,product是乘积),但是搭档改对了

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <math.h>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <utility>
#define Fast_io ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define filein freopen("evaluations.in", "r", stdin);
#define fileout freopen("output.txt", "w", stdout);
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;

struct node
{
    int w;
    int data, next;
} e[100005];
int head[100005], cnt;
int numm[N], vis[N];
void creat()
{
    for (int i = 2; i < N; i++)
    {
        if (!vis[i])
        {
            vis[i] = 1;
            numm[i]++;
            for (int j = 2 * i; j < N; j += i)
            {
                vis[j] = 1;
                numm[j]++;
            }
        }
    }
}numm记录的是素数的个数,并且这些素数是不想同的,当时埃氏筛的时候应该想到这一点。
void add(int u, int v, int w)
{
    e[cnt].w = w;
    e[cnt].data = v;
    e[cnt].next = head[u];
    head[u] = cnt++;
}
bool book[100005];
ll t;
ll dfs(int u, int pre, int sum)
{
    int ans = 0;
    int i;
    for (i = head[u]; i != -1; i = e[i].next)
    {
        if (e[i].data == pre)
            continue;
        book[e[i].data] = 1;
        if (sum + e[i].w <= 2) 还必须在找素数直到正好有两个。
        {
            ans += dfs(e[i].data, u, sum + e[i].w);
        }
    }
    if (sum == 2)///正好有两个
        ans++;
    return ans;
}

int main()
{
    Fast_io;
    // filein;
    int tt, m = 1;
    cin >> tt;
    creat();
    int i;
    for (i = 0; i < 100; i++)
        printf("%d ", numm[i]);
    while (tt--)
    {
        int n;
        cin >> n;
        memset(head, -1, sizeof(head));
        int i;
        t = 0;
        for (i = 0; i < n - 1; i++)
        {
            int x, y, w;
            cin >> x >> y >> w;
            add(x, y, numm[w]);
            add(y, x, numm[w]);
        }
        for (i = 1; i <= n; i++)
        {
            memset(book, 0, sizeof book);
            book[i] = 1;
            t += dfs(i, 0, 0);
            // printf("%d\n", t);
        }
        printf("Case %d: %lld\n", m++, t / 2);//无向图,多找了一倍
    }
    return 0;
}

忽忽悠悠的明白了,当时还想把每条边直接乘,然后再找出两个不等的素数,可是复杂度太高,而且实现麻烦,哎!!多理解把!!!!!!!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值