日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

星座图(2020特长生 T4)

發布時間:2023/12/3 64 豆豆
生活随笔 收集整理的這篇文章主要介紹了 星座图(2020特长生 T4) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目大意

給你一棵樹,距離為2的兩個點代價為wi?wjw_i*w_jwi??wj?,問你最小代價和代價之和


解題思路

搜索這棵樹,每次拿父親和子節點一起計算即可


代碼

#include<cstdio> #include<cstring> #include<algorithm> #define ll long long #define wyc 10007 #define N 200100 using namespace std; int n, x, y, tot, anssum, ansmax, s[N], head[N]; struct rec {int to, next; }a[N<<1]; void add(int x, int y) {a[++tot].to = y;a[tot].next = head[x];head[x] = tot; } void dfs(int x, int fa) {int sum = s[fa] % wyc, maxx = s[fa];for (int i = head[x]; i; i = a[i].next)if (a[i].to != fa){anssum = (anssum + sum * s[a[i].to] % wyc) % wyc;//求代價之和ansmax = max(ansmax, maxx * s[a[i].to]);//求最大值dfs(a[i].to, x);sum = (sum + s[a[i].to]) % wyc;maxx = max(maxx, s[a[i].to]);}return; } int main() {scanf("%d", &n);for (int i = 1; i < n; ++i){scanf("%d%d", &x, &y);add(x, y);add(y, x);}for (int i = 1; i <= n; ++i)scanf("%d", &s[i]);dfs(1, 0);printf("%d %d", ansmax, anssum * 2 % wyc);return 0; }

總結

以上是生活随笔為你收集整理的星座图(2020特长生 T4)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。