这是我的代码和我的搭档的代码的结合体,当然自己的代码根本不对(理解问题,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;
}
忽忽悠悠的明白了,当时还想把每条边直接乘,然后再找出两个不等的素数,可是复杂度太高,而且实现麻烦,哎!!多理解把!!!!!!!!!