Luogu 1351 - Joint Weights

topic address

https://www.luogu.org/problemnew/show/P1351

Topic description

An undirected connected graph G has n vertices and n - 1 edges. Points are numbered sequentially from 1 to n, the weight of the point numbered i is W i , and the length of each edge is 1 . The distance between two points ( u , v ) on the graph is defined as the shortest distance from point u to point v. For the point pair (u, v) on the graph G, if their distance is 2, a joint weight of Wu×Wv will be generated between them.

May I ask, among all the ordered point pairs on graph G that can generate joint weights, what is the largest joint weight? What is the sum of all joint weights?

Input and output format

Input format:

The input file name is link .in.

The first line contains 1 integer n.

Next n - 1 lines, each line contains 2 positive integers u and v separated by spaces, indicating that the points numbered u and numbered v are connected by an edge.

The last line contains n positive integers, separated by a space between each two positive integers, where the ith integer represents the weight of the point numbered i on the graph G as Wi.

Output format:

The output file is named link .out.

The output has a total of 1 line, including 2 integers, separated by a space, the order is the maximum value of the joint weight on the graph G

and the sum of all joint weights. Since the sum of all joint weights can be large, output it with a remainder of 10007.

Input and output example

Input example #1: 
5  
1 2  
2 3
3 4  
4 5  
1 5 2 3 10
Sample output #1: 
20 74
The idea of ​​this question is actually not difficult to think about, and the topic is easy to understand. 
  The idea of ​​​​(cuo) opening (wu) is that you can enumerate each point first, find the point to form a team with the point that can be reached through this point, use maxx to maintain the maximum value, and ans to record the sum, of
course, This idea itself is not wrong, but if you look at the data range, you will know that it will definitely be tle, because it is an exam, and it is going for the 60 points. . . .
 1 #include<cstdio>
 2 #include<iostream>
 3 using namespace std;
 4 int w[200010],k,n,head[200010],ans,maxx=-0x6ffffff;
 5 struct node{
 6     int u,v,nxt;
 7 }a[400010];
 8 void add(int x,int y)
 9 {
10     a[++k].u=x,a[k].v=y;
11     a[k].nxt=head[x];
12     head[x]=k;
13 }
14 int main()
15 {
16     scanf("%d",&n);
17     for(int i=1,x,y;i<n;++i)
18     {
19         scanf("%d%d",&x,&y);
20         add(x,y);
21         add(y,x);
22     }
23     for(int i=1;i<=n;++i)
24     {
25         scanf("%d",&w[i]);
26     }
27     for(int i=1;i<=n;++i)
28     {
29         int sum=0,max2=0,max1=0;
30         for(int j=head[i];j;j=a[j].nxt)
31         {
32             
33             if(w[a[j].v]>max1)
34             {
35                 max2=max1;
36                 max1=w[a[j].v];
37             }
38             else if(w[a[j].v]>max2)
39                 max2=w[a[j].v];
40             ans=(ans+w[a[j].v]*sum)%10007;
41             sum=(sum+w[a[j].v])%10007;
42         }
43         maxx=max(maxx,max1*max2);
44     }
45     printf("%d %d",maxx,(ans*2)%10007);
46     return 0;
47  } 
 
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324600910&siteId=291194637
Recommended