結果
| 問題 |
No.364 門松木
|
| コンテスト | |
| ユーザー |
rickytheta
|
| 提出日時 | 2016-04-20 22:21:13 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,914 bytes |
| コンパイル時間 | 1,292 ms |
| コンパイル使用メモリ | 170,944 KB |
| 実行使用メモリ | 25,528 KB |
| 最終ジャッジ日時 | 2024-10-04 14:29:49 |
| 合計ジャッジ時間 | 3,269 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 17 WA * 14 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:58:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
58 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
main.cpp:59:16: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
59 | REP(i,n)scanf("%d",a+i);
| ~~~~~^~~~~~~~~~
main.cpp:62:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
62 | scanf("%d%d",&x,&y);
| ~~~~~^~~~~~~~~~~~~~
ソースコード
#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] がいるなら消す
cnt[to][opp] -= dp[to][opp][a[pos]];
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;
}
rickytheta