[NOIP2014]联合权值

解题报告

原题见COGSSYZOJ 联合权值这道题啊,是一道数学题(此处大雾… 我还是有点不能理解这种算法。。明天再细细研究,先把代码贴出来。 对了,%晨耀防止爆零。。

#include 
#include 
#include 
#include 
#include 
#define LL long long
#define Chenyao2333 10007
using namespace std;
const int MX = 200005;
LL w[MX];
LL maxn, sum, n;
bool vis[MX];
vector  g[MX];
queue  q;
int main() {
    freopen("linkb.in", "r", stdin);
    freopen("linkb.out", "w", stdout);
    scanf("%lld", &n);
    for (int i = 1; i < n; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        g[u].push_back(v);
        g[v].push_back(u);
    }
    for (int i = 1; i <= n; i++) {
        scanf("%lld", w + i);
    }
    q.push(1);
    vis[1] = 1;
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        LL suma = 0, sumb = 0;
        LL max1 = 0, max2 = 0;
        //cout << g[u].size();
        for (int i = 0; i  max1) {
                max2 = max1;
                max1 = w[v];
            } else if (w[v] > max2) {
                max2 = w[v];
            }
            if (!vis[v]) {
                vis[v] = 1;
                q.push(v);
            }
        }
        sum = (sum + ((suma * suma) % Chenyao2333 - sumb + Chenyao2333) % Chenyao2333) % Chenyao2333;
        maxn = max(maxn, max1 * max2);
    }
    printf("%lld %lld", maxn, sum);
    return 0;
}

 

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理