星座图(2020特长生 T4)
生活随笔
收集整理的這篇文章主要介紹了
星座图(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)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 论文浅尝 | TANDA: Transf
- 下一篇: 裁缝师(2011特长生 T2)