結果

問題 No.364 門松木
ユーザー rickythetarickytheta
提出日時 2016-04-20 22:30:00
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 45 ms / 3,000 ms
コード長 2,021 bytes
コンパイル時間 1,559 ms
コンパイル使用メモリ 170,264 KB
実行使用メモリ 26,208 KB
最終ジャッジ日時 2024-04-15 03:55:37
合計ジャッジ時間 3,328 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
9,816 KB
testcase_01 AC 4 ms
9,760 KB
testcase_02 AC 3 ms
10,020 KB
testcase_03 AC 3 ms
9,812 KB
testcase_04 AC 4 ms
9,580 KB
testcase_05 AC 4 ms
9,812 KB
testcase_06 AC 4 ms
9,920 KB
testcase_07 AC 5 ms
9,808 KB
testcase_08 AC 36 ms
15,472 KB
testcase_09 AC 36 ms
15,484 KB
testcase_10 AC 32 ms
13,776 KB
testcase_11 AC 36 ms
15,704 KB
testcase_12 AC 29 ms
13,396 KB
testcase_13 AC 32 ms
15,552 KB
testcase_14 AC 30 ms
15,612 KB
testcase_15 AC 32 ms
15,368 KB
testcase_16 AC 28 ms
15,320 KB
testcase_17 AC 36 ms
15,440 KB
testcase_18 AC 38 ms
15,676 KB
testcase_19 AC 40 ms
16,248 KB
testcase_20 AC 39 ms
15,588 KB
testcase_21 AC 41 ms
15,452 KB
testcase_22 AC 26 ms
14,720 KB
testcase_23 AC 43 ms
18,080 KB
testcase_24 AC 40 ms
15,192 KB
testcase_25 AC 45 ms
18,560 KB
testcase_26 AC 43 ms
17,204 KB
testcase_27 AC 39 ms
16,008 KB
testcase_28 AC 39 ms
15,728 KB
testcase_29 AC 43 ms
17,668 KB
testcase_30 AC 41 ms
18,216 KB
testcase_31 AC 5 ms
9,684 KB
testcase_32 AC 5 ms
9,684 KB
testcase_33 AC 40 ms
26,208 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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