結果
| 問題 |
No.364 門松木
|
| コンテスト | |
| ユーザー |
lam6er
|
| 提出日時 | 2025-03-26 16:02:04 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 33 ms / 3,000 ms |
| コード長 | 1,898 bytes |
| コンパイル時間 | 2,278 ms |
| コンパイル使用メモリ | 203,672 KB |
| 実行使用メモリ | 16,768 KB |
| 最終ジャッジ日時 | 2025-03-26 16:02:40 |
| 合計ジャッジ時間 | 5,067 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 31 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:84:13: warning: ISO C++17 does not allow ‘register’ storage class specifier [-Wregister]
84 | For(i,1,n)
| ^
main.cpp:13:37: note: in definition of macro ‘For’
13 | #define For(i,l,r) for(register int i=l;i<=r;i++)
| ^
main.cpp:86:13: warning: ISO C++17 does not allow ‘register’ storage class specifier [-Wregister]
86 | For(i,1,n-1){
| ^
main.cpp:13:37: note: in definition of macro ‘For’
13 | #define For(i,l,r) for(register int i=l;i<=r;i++)
| ^
main.cpp:92:13: warning: ISO C++17 does not allow ‘register’ storage class specifier [-Wregister]
92 | For(i,1,n)
| ^
main.cpp:13:37: note: in definition of macro ‘For’
13 | #define For(i,l,r) for(register int i=l;i<=r;i++)
| ^
main.cpp:95:13: warning: ISO C++17 does not allow ‘register’ storage class specifier [-Wregister]
95 | For(i,1,n){
| ^
main.cpp:13:37: note: in definition of macro ‘For’
13 | #define For(i,l,r) for(register int i=l;i<=r;i++)
| ^
main.cpp:99:13: warning: ISO C++17 does not allow ‘register’ storage class specifier [-Wregister]
99 | For(i,1,n)
| ^
main.cpp:13:37: note: in definition of macro ‘For’
13 | #define For(i,l,r) for(register int i=l;i<=r;i++)
| ^
ソースコード
#include<bits/stdc++.h>
#define Add(x,y) (x+y>=mod)?(x+y-mod):(x+y)
#define lowbit(x) x&(-x)
#define pi pair<ll,ll>
#define pii pair<ll,pair<ll,ll>>
#define iip pair<pair<ll,ll>,ll>
#define ppii pair<pair<ll,ll>,pair<ll,ll>>
#define fi first
#define se second
#define full(l,r,x) for(auto it=l;it!=r;it++) (*it)=x
#define Full(a) memset(a,0,sizeof(a))
#define open(s1,s2) freopen(s1,"r",stdin),freopen(s2,"w",stdout);
#define For(i,l,r) for(register int i=l;i<=r;i++)
#define _For(i,l,r) for(register int i=r;i>=l;i--)
using namespace std;
typedef double db;
typedef unsigned long long ull;
typedef long long ll;
bool Begin;
const ll N=50050;
inline ll read(){
ll x=0,f=1;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-')
f=-1;
c=getchar();
}
while(c>='0'&&c<='9'){
x=(x<<1)+(x<<3)+(c^48);
c=getchar();
}
return x*f;
}
inline void write(ll x){
if(x<0){
putchar('-');
x=-x;
}
if(x>9)
write(x/10);
putchar(x%10+'0');
}
ll n,u,v,ans;
ll a[N],s[N],dp[N];
bool f[N];
vector<ll> E[N];
void add(ll u,ll v){
E[u].push_back(v);
E[v].push_back(u);
}
void dfs1(ll u,ll fa){
for(auto v:E[u]){
if(v==fa)
continue;
s[v]-=s[u];
dfs1(v,u);
}
}
void dfs2(ll u,ll fa){
f[u]=1;
map<ll,ll> h;
for(auto v:E[u]){
if(v==fa)
continue;
if(a[v]==a[u]||(s[v]<s[u])!=(a[v]<a[u]))
continue;
dfs2(v,u);
h[a[v]]=max(h[a[v]],dp[v]);
}
ll s=0,c=h.size();
for(auto t:h)
s+=t.se;
ans=max(ans,s+c*(c-1)/2);
if(!fa)
return;
s-=h[a[fa]];
c=h.size();
dp[u]=s+c*(c-1)/2;
}
bool End;
int main(){
n=read();
For(i,1,n)
a[i]=read();
For(i,1,n-1){
u=read(),v=read();
add(u,v);
}
s[1]=1;
dfs1(1,0);
For(i,1,n)
if(!f[i])
dfs2(i,0);
For(i,1,n){
s[i]*=-1;
dp[i]=f[i]=0;
}
For(i,1,n)
if(!f[i])
dfs2(i,0);
write(ans);
cerr<<'\n'<<abs(&Begin-&End)/1048576<<"MB";
return 0;
}
lam6er