社区讨论
超时?70pts
P1351[NOIP 2014 提高组] 联合权值参与者 2已保存回复 2
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 2 条
- 当前快照
- 1 份
- 快照标识符
- @mhjrd4pv
- 此快照首次捕获于
- 2025/11/04 07:15 4 个月前
- 此快照最后确认于
- 2025/11/04 07:15 4 个月前
CPP
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5+10,M = 2e5+10,mod = 10007;
int n;
int h[N],e[M],ne[M],idx;
int w[N];
void add(int a,int b)
{
e[idx] = b;ne[idx] = h[a];h[a] = idx++;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
memset(h,-1,sizeof(h));
cin >> n;
for(int i=1;i<n;i++)
{
int a,b;
cin >> a >> b;
add(a,b);
add(b,a);
}
for(int i=1;i<=n;i++) cin >> w[i];
int res = 0,maxx = 0;
for(int t=1;t<=n;t++)
{
int s1 = 0,s2 = 0;
int max1 = 0,max2 = 0;
for(int i=h[t];i!=-1;i=ne[i])
{
int j = e[i];
if(w[j]>max1)
{
max2 = max1;
max1 = w[j];
}
else if(w[j]>max2) max2 = w[j];
s1 = (s1+w[j])%mod;
s2 = (s2+w[j]*w[j])%mod;
}
s1 = s1*s1%mod;
res = (res+s1-s2+mod)%mod;
if(max1*max2>maxx) maxx = max1*max2;
}
cout << maxx << " " << res;
return 0;
}
回复
共 2 条回复,欢迎继续交流。
正在加载回复...