本文共 1961 字,大约阅读时间需要 6 分钟。
#include #include #include #include #include #include #include #include #include #include #include #include #define MEM(x,y) memset ( x , y , sizeof ( x ) )#define rep(i,a,b) for (int i = (a) ; i <= (b) ; ++ i)#define per(i,a,b) for (int i = (a) ; i >= (b) ; -- i)#define pii pair < int , int >#define X first#define Y second#define rint read #define int long long#define pb push_backusing std::queue ;using std::set ;using std::pair ;using std::max ;using std::min ;using std::priority_queue ;using std::vector ;using std::swap ;using std::sort ;using std::unique ;using std::greater ;template < class T > inline T read () { T x = 0 , f = 1 ; char ch = getchar () ; while ( ch < '0' || ch > '9' ) { if ( ch == '-' ) f = - 1 ; ch = getchar () ; } while ( ch >= '0' && ch <= '9' ) { x = ( x << 3 ) + ( x << 1 ) + ( ch - 48 ) ; ch = getchar () ; } return f * x ;}const int mod = 1e4 + 7 ;const int N = 2e5 + 100 ;vector < int > G[N] ;int n , v[N] , sum[N] ;int maxn , ans ;inline bool cmp (int a , int b) { return v[a] > v[b] ; }signed main (int argc , char * argv[]) { n = rint () ; rep ( i , 2 , n ) { int u = rint () , v = rint () ; G[u].pb ( v ) ; G[v].pb ( u ) ; } rep ( i , 1 , n ) v[i] = rint () ; rep ( i , 1 , n ) sort ( G[i].begin () , G[i].end () , cmp ) ; rep ( i , 1 , n ) { if ( (int)G[i].size () < 2 ) continue ; int sum = 0 ; for (int j = 0 ; j < (int)G[i].size () ; ++ j) sum += v[G[i][j]] ; maxn = max ( maxn , v[G[i][0]] * v[G[i][1]] ) ; for (int j = 0 ; j < (int)G[i].size () ; ++ j) { int k = G[i][j] , m = (int)G[i].size () ; ans = ( ans + v[k] * ( sum - v[k] ) % mod ) % mod ; } } printf ("%lld %lld\n" , maxn , ans ) ; system ("pause") ; return 0 ;}
转载于:https://www.cnblogs.com/Equinox-Flower/p/11507976.html