結果

問題 No.364 門松木
ユーザー chocoruskchocorusk
提出日時 2020-02-13 06:45:48
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 76 ms / 3,000 ms
コード長 1,012 bytes
コンパイル時間 2,516 ms
コンパイル使用メモリ 174,264 KB
実行使用メモリ 15,872 KB
最終ジャッジ日時 2024-04-15 09:44:57
合計ジャッジ時間 5,368 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,812 KB
testcase_01 AC 3 ms
6,940 KB
testcase_02 AC 3 ms
6,944 KB
testcase_03 AC 3 ms
6,940 KB
testcase_04 AC 3 ms
6,940 KB
testcase_05 AC 4 ms
6,940 KB
testcase_06 AC 3 ms
6,940 KB
testcase_07 AC 4 ms
6,944 KB
testcase_08 AC 65 ms
7,296 KB
testcase_09 AC 66 ms
7,168 KB
testcase_10 AC 59 ms
7,296 KB
testcase_11 AC 66 ms
7,424 KB
testcase_12 AC 62 ms
7,296 KB
testcase_13 AC 55 ms
7,296 KB
testcase_14 AC 55 ms
7,296 KB
testcase_15 AC 56 ms
7,424 KB
testcase_16 AC 53 ms
7,296 KB
testcase_17 AC 65 ms
7,040 KB
testcase_18 AC 63 ms
7,424 KB
testcase_19 AC 72 ms
7,424 KB
testcase_20 AC 65 ms
7,296 KB
testcase_21 AC 74 ms
6,940 KB
testcase_22 AC 46 ms
6,940 KB
testcase_23 AC 72 ms
7,552 KB
testcase_24 AC 75 ms
7,168 KB
testcase_25 AC 73 ms
7,552 KB
testcase_26 AC 70 ms
7,552 KB
testcase_27 AC 66 ms
7,168 KB
testcase_28 AC 76 ms
7,680 KB
testcase_29 AC 69 ms
7,552 KB
testcase_30 AC 68 ms
7,680 KB
testcase_31 AC 3 ms
6,940 KB
testcase_32 AC 3 ms
6,944 KB
testcase_33 AC 63 ms
15,872 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
int n;
vector<int> g[50050];
int a[50050], c[50050];
void dfs0(int x, int p){
  for(auto y:g[x]){
    if(y==p) continue;
    c[y]=-c[x];
    dfs0(y, x);
  }
}
ll dp[50050], ans;
bool used[50050];
void dfs(int x, int p){
  used[x]=1;
  map<int, ll> mp;
  for(auto y:g[x]){
    if(y==p) continue;
    if(a[y]==a[x] || (c[y]<c[x])!=(a[y]<a[x])) continue;
    dfs(y, x);
    mp[a[y]]=max(mp[a[y]], dp[y]);
  }
  ll s=0, c=mp.size();
  for(auto q:mp) s+=q.second;
  ans=max(ans, s+c*(c-1)/2);
  if(p==-1) return;
  s-=mp[a[p]], c=mp.size();
  dp[x]=s+c*(c-1)/2;
}
int main()
{
    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--;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  c[0]=1; dfs0(0, -1);
  for(int i=0; i<n; i++) if(!used[i]) dfs(i, -1);
  for(int i=0; i<n; i++) c[i]*=(-1), used[i]=0, dp[i]=0;
  for(int i=0; i<n; i++) if(!used[i]) dfs(i, -1);
  cout<<ans<<endl;
    return 0;
}
0