結果

問題 No.364 門松木
ユーザー rickytheta
提出日時 2016-04-20 22:30:00
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 41 ms / 3,000 ms
コード長 2,021 bytes
コンパイル時間 1,440 ms
コンパイル使用メモリ 171,108 KB
実行使用メモリ 26,360 KB
最終ジャッジ日時 2024-10-04 14:29:57
合計ジャッジ時間 3,273 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 31
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:61:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   61 |   scanf("%d",&n);
      |   ~~~~~^~~~~~~~~
main.cpp:62:16: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   62 |   REP(i,n)scanf("%d",a+i);
      |           ~~~~~^~~~~~~~~~
main.cpp:65:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   65 |     scanf("%d%d",&x,&y);
      |     ~~~~~^~~~~~~~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef complex<double> P;
typedef pair<int,int> pii;
#define REP(i,n) for(ll i=0;i<n;++i)
#define REPR(i,n) for(ll i=1;i<n;++i)
#define FOR(i,a,b) for(ll i=a;i<b;++i)

#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()

#define MOD (ll)(1e9+7)
#define ADD(a,b) a=((a)+(b))%MOD
#define FIX(a) ((a)%MOD+MOD)%MOD

int n;
int a[52525];
vi g[52525];
map<int,ll> dp[52525][2]; // その点から下の者達
ll cnt[52525][2];         // ???

ll dfs(int pos, int par, int type){
  int opp = 1-type;
  ll ans = 0;
  REP(i,g[pos].size()){
    int to = g[pos][i];
    if(to==par)continue;
    ll tmp = dfs(to,pos,opp);
    ans = max(ans,tmp);
    // じぐざぐ
    if(a[pos]==a[to] || (a[pos]<a[to])!=type)continue;
    // pos - to - to_child
    // to_child に a[pos] がいるなら消す
    if(dp[to][opp].find(a[pos])!=dp[to][opp].end()){
      cnt[to][opp] -= dp[to][opp][a[pos]];
      cnt[to][opp] -= dp[to][opp].size()-1;
      dp[to][opp].erase(a[pos]);
    }
    // そして子リストに重複無く追加する ( size 足すのは pos - to - to_child ラインの門松列 )
    dp[pos][type][a[to]] = max<ll>(dp[pos][type][a[to]], cnt[to][opp] + dp[to][opp].size());
  }
  map<int,ll>::iterator iter = dp[pos][type].begin();
  while(iter != dp[pos][type].end()){
    cnt[pos][type] += iter->second;
    iter++;
  }
  // to - pos - to ラインの門松列の加算
  ll sz = dp[pos][type].size();
  cnt[pos][type] += sz*(sz-1ll)/2ll;
  ans = max(ans,cnt[pos][type]);
  return ans;
}

int main(){
  scanf("%d",&n);
  REP(i,n)scanf("%d",a+i);
  REP(i,n-1){
    int x,y;
    scanf("%d%d",&x,&y);
    --x;--y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  ll ans = 0;
  ans = max(ans, dfs(0,830252521,0));
  ans = max(ans, dfs(0,830253150,1));
  printf("%lld\n",ans);
  return 0;
}
0