結果
問題 | No.364 門松木 |
ユーザー |
![]() |
提出日時 | 2016-04-17 23:54:28 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 38 ms / 3,000 ms |
コード長 | 2,198 bytes |
コンパイル時間 | 1,449 ms |
コンパイル使用メモリ | 170,700 KB |
実行使用メモリ | 21,464 KB |
最終ジャッジ日時 | 2024-10-04 11:07:42 |
合計ジャッジ時間 | 3,502 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 31 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:72:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 72 | scanf("%lld",&N); | ~~~~~^~~~~~~~~~~ main.cpp:73:18: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 73 | rep(i,N)scanf("%lld",&A[i]); | ~~~~~^~~~~~~~~~~~~~ main.cpp:76:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 76 | scanf("%lld%lld",&a,&b); | ~~~~~^~~~~~~~~~~~~~~~~~
ソースコード
#include<bits/stdc++.h>using namespace std;#define int long longtypedef pair<int,int>pint;typedef vector<int>vint;typedef vector<pint>vpint;#define pb push_back#define mp make_pair#define fi first#define se second#define all(v) (v).begin(),(v).end()#define rep(i,n) for(int i=0;i<(n);i++)#define reps(i,f,n) for(int i=(f);i<(n);i++)#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)template<class T,class U>inline void chmin(T &t,U f){if(t>f)t=f;}template<class T,class U>inline void chmax(T &t,U f){if(t<f)t=f;}const int SIZE=50000;int N;int A[SIZE];vint G[SIZE];int ans;map<int,int>li_mi[SIZE],li_ma[SIZE];int dp_mi[SIZE],dp_ma[SIZE];void dfs(int v,int p,int t){if(t==0){for(auto to:G[v]){if(to==p)continue;dfs(to,v,1);if(A[v]>=A[to])continue;int tmp;if(li_ma[to].find(A[v])!=li_ma[to].end()){tmp=dp_ma[to]-li_ma[to][A[v]];}else{int hoge=li_ma[to].size();tmp=dp_ma[to]-hoge*(hoge-1)/2+(hoge+1)*hoge/2;}chmax(li_mi[v][A[to]],tmp);}each(it,li_mi[v])dp_mi[v]+=it->se;int hoge=li_mi[v].size();dp_mi[v]+=hoge*(hoge-1)/2;chmax(ans,dp_mi[v]);}else{for(auto to:G[v]){if(to==p)continue;dfs(to,v,0);if(A[v]<=A[to])continue;int tmp;if(li_mi[to].find(A[v])!=li_mi[to].end()){tmp=dp_mi[to]-li_mi[to][A[v]];}else{int hoge=li_mi[to].size();tmp=dp_mi[to]-hoge*(hoge-1)/2+(hoge+1)*hoge/2;}chmax(li_ma[v][A[to]],tmp);}each(it,li_ma[v])dp_ma[v]+=it->se;int hoge=li_ma[v].size();dp_ma[v]+=hoge*(hoge-1)/2;chmax(ans,dp_ma[v]);}}signed main(){scanf("%lld",&N);rep(i,N)scanf("%lld",&A[i]);rep(i,N-1){int a,b;scanf("%lld%lld",&a,&b);a--;b--;G[a].pb(b);G[b].pb(a);}dfs(0,-1,0);dfs(0,-1,1);cout<<ans<<endl;return 0;}