結果

問題 No.364 門松木
ユーザー chocoruskchocorusk
提出日時 2020-02-13 06:37:05
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 65 ms / 3,000 ms
コード長 1,533 bytes
コンパイル時間 2,133 ms
コンパイル使用メモリ 120,256 KB
実行使用メモリ 15,744 KB
最終ジャッジ日時 2024-04-15 09:43:38
合計ジャッジ時間 5,214 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
int n;
vector<int> g[50050];
int a[50050];
int 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];
ll 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