結果

問題 No.364 門松木
ユーザー yaoshimaxyaoshimax
提出日時 2016-04-22 02:01:13
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 58 ms / 3,000 ms
コード長 2,458 bytes
コンパイル時間 968 ms
コンパイル使用メモリ 98,608 KB
実行使用メモリ 20,164 KB
最終ジャッジ日時 2024-04-15 03:59:15
合計ジャッジ時間 3,218 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,744 KB
testcase_01 AC 4 ms
7,748 KB
testcase_02 AC 4 ms
7,744 KB
testcase_03 AC 3 ms
7,496 KB
testcase_04 AC 4 ms
7,744 KB
testcase_05 AC 3 ms
7,748 KB
testcase_06 AC 4 ms
7,748 KB
testcase_07 AC 3 ms
7,624 KB
testcase_08 AC 51 ms
9,672 KB
testcase_09 AC 54 ms
9,792 KB
testcase_10 AC 49 ms
9,792 KB
testcase_11 AC 54 ms
9,924 KB
testcase_12 AC 48 ms
9,796 KB
testcase_13 AC 48 ms
9,796 KB
testcase_14 AC 46 ms
9,800 KB
testcase_15 AC 48 ms
9,664 KB
testcase_16 AC 44 ms
9,668 KB
testcase_17 AC 52 ms
9,664 KB
testcase_18 AC 51 ms
9,792 KB
testcase_19 AC 51 ms
10,304 KB
testcase_20 AC 49 ms
10,048 KB
testcase_21 AC 50 ms
9,924 KB
testcase_22 AC 37 ms
9,924 KB
testcase_23 AC 54 ms
11,848 KB
testcase_24 AC 55 ms
9,616 KB
testcase_25 AC 55 ms
12,104 KB
testcase_26 AC 57 ms
11,200 KB
testcase_27 AC 54 ms
10,056 KB
testcase_28 AC 58 ms
9,924 KB
testcase_29 AC 55 ms
11,716 KB
testcase_30 AC 55 ms
12,480 KB
testcase_31 AC 3 ms
7,496 KB
testcase_32 AC 4 ms
7,620 KB
testcase_33 AC 57 ms
20,164 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>

using namespace std;

int A[100000];
vector<int> edge[100000];
int parent[100000];
int dp[100000][2][2]; //dp[i][j]... node i as a parent, j=1 must use i's parent
void dfs(int p){
//   //cout <<p<<endl;
   for( int i = 0 ;i<(int)edge[p].size();i++){
      int nxt = edge[p][i];
      if( nxt!= parent[p] ){
         parent[nxt]=p;
         dfs(nxt);
      }
   }
}
int ans = 0;
int calc( int p ,int o, int f){
   map<int,int> m;
   //cout << p <<", "<< o <<", "<< f<<endl;
   if(dp[p][o][f]!=-1 ) return dp[p][o][f];
   for( int ei=0; ei < (int)edge[p].size(); ei++ ){
      int n = edge[p][ei];
      //cout << p <<"- "<< n << endl;
      if( n==parent[p]) continue;
      calc(n,0,0);
      calc(n,1,0);
      if( A[n]>A[p] &&o==0){
         m[A[n]]=max(m[A[n]],calc(n,1-o,1));
      }
      if( A[n]<A[p]&&o==1){
         m[A[n]]=max(m[A[n]],calc(n,1-o,1));
      }
   }

   map<int,int>::iterator it=m.begin();
   int cnt=0;
   int cnt_wo_parent=0;
   dp[p][o][0]=0;
   dp[p][o][1]=0;
   while(it!= m.end()){
      cnt++;
      dp[p][o][0]+=it->second;
      if( it->first!= A[parent[p]] ){
         cnt_wo_parent++;
         dp[p][o][1]+=it->second;
      }
      it++;
   }
   dp[p][o][0]+=cnt*(cnt-1)/2;
   dp[p][o][1]+=cnt_wo_parent*(cnt_wo_parent-1)/2;
   if( o==0 && A[parent[p]]>A[p]) dp[p][o][1]+=cnt_wo_parent;
   if( o==1 && A[parent[p]]<A[p]) dp[p][o][1]+=cnt_wo_parent;
   ans=max(dp[p][o][0],ans);
   if( parent[p]!=p ) ans=max(dp[p][o][1],ans);
   //cout << p << ", "<< o <<", "<< 0 <<": "<< dp[p][o][0]<<endl;
   //cout << p << ", "<< o <<", "<< 1 <<": "<< dp[p][o][1]<<endl;
   return dp[p][o][f];
}

int main(){
  int N;
  cin >> N;
  for( int i = 0 ; i <N; i++ ){
      cin >> A[i];
  }
  for( int i = 0;  i < N-1 ; i++ ){
      int x,y;
      cin >> x >> y;
      x--;y--;
      edge[x].push_back(y);
      edge[y].push_back(x);
  }
  memset(parent,-1,sizeof(parent));
  if( N <= 2 ){
   cout<<0<<endl;
   return 0;
  }
  int p=0;
  while( edge[p].size() < 2 ) p++;
  parent[p]=p;
  dfs(p);
  memset(dp,-1,sizeof(dp)); 
  calc(p,0,0);
  calc(p,1,0);
  cout << ans <<endl;
  return 0;
}
0