結果

問題 No.364 門松木
ユーザー yaoshimax
提出日時 2016-04-22 02:01:13
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 52 ms / 3,000 ms
コード長 2,458 bytes
コンパイル時間 850 ms
コンパイル使用メモリ 97,828 KB
実行使用メモリ 20,424 KB
最終ジャッジ日時 2024-10-04 14:37:38
合計ジャッジ時間 3,228 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

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;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0