QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#431674 | #7419. Jiry Matchings | xyloph0nex17 | WA | 0ms | 54464kb | C++14 | 4.7kb | 2024-06-05 21:38:19 | 2024-06-05 21:38:19 |
Judging History
answer
//El Psy Kongroo
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define ri int
#define TIME (1e3 * clock() / CLOCKS_PER_SEC)
using namespace std ;
template <typename T>
inline void read(T&x){
x = 0 ; char c = getchar() ; bool flg = 1 ; while(c > '9' || c < '0'){if(c == '-')flg = 0 ; c = getchar() ;}
while(c >= '0' && c <= '9'){x = (x << 1) + (x << 3) + (c ^ 48) ; c = getchar() ;} x = flg ? x : -x ;
}
template <typename T,typename ...Args>
inline void read(T&x,Args&...args){read(x),read(args...);}
bool m_bg ;
const ll N = 2e5 + 10 , inf = 1e18 ;
int pp ;
#define poly vector<ll>
#define mat array<array<poly,2>,2>
poly max(poly f,poly g){
if(f.size()<g.size())swap(f,g) ;
for(ri i = 0 ; i < (int)g.size() ; i++)f[i]=max(f[i],g[i]) ;
return f ;
}
poly operator * (poly f,poly g){
if(!f.size()||!g.size())return {} ;
poly h(f.size()+g.size()-1,0) ; h[0]=f[0]+g[0] ;
int i=1,j=1,k=1 ; while(i<(int)f.size()&&j<(int)g.size()){
if(f[i]-f[i-1]>g[j]-g[j-1])h[k]=h[k-1]+f[i]-f[i-1],k++,i++ ;
else h[k]=h[k-1]+g[j]-g[j-1],k++,j++ ;
}
while(i<(int)f.size())h[k]=h[k-1]+f[i]-f[i-1],k++,i++ ;
while(j<(int)g.size())h[k]=h[k-1]+g[j]-g[j-1],k++,j++ ;
//assert(k==f.size()+g.size()-1) ;
return h ;
}
poly operator + (poly f,int k){
for(auto &x : f)x += k ;
f.insert(f.begin(),-inf) ; return f ;
}
mat mul(mat f,mat g,ll k){
mat h ;
for(ri i : {0,1})for(ri j:{0,1}){
h[i][j]=max(f[i][0]*g[0][j],f[i][1]*g[1][j]+k) ;
}return h ;
}
void clr(mat &f){for(ri i:{0,1})for(ri j:{0,1})poly().swap(f[i][j]) ;}
struct EDGE{int v,w ;} ; vector<EDGE>G[N] ; poly dp[N][2] ;
int fa[N],fw[N],top[N],siz[N],son[N] ;
void dfs0(int u,int _fa){
siz[u] = 1 ; fa[u] = _fa ;
for(auto e : G[u])if(e.v!=_fa)fw[e.v]=e.w,dfs0(e.v,u),siz[u]+=siz[e.v],son[u]=(siz[e.v]>siz[son[u]])?e.v:son[u] ;
}
vector<int>ps,tps[N] ; mat f[N] ; poly g[N][2] ;
void solve0(int l,int r,vector<int>&id){
if(l==r)return g[l][0]=max(dp[id[l]][0],dp[id[l]][1]+fw[id[l]]),g[l][1]=dp[id[l]][0],void() ;
int s = 0 ; for(ri i = l ; i <= r ; i++)s+=dp[id[i]][0].size() ;
for(ri i = l + 1 , t = dp[id[l]][0].size() ; i <= r ; i++){
t+=dp[id[i]][0].size() ;
if(t*2<=s)continue ;
solve0(l,i-1,id),solve0(i,r,id) ;
g[l][0]=max(g[l][0]*g[i][1],g[l][1]*g[i][0]) ;
g[l][1]=g[l][1]*g[i][1] ; break ;
}
}
/*
void dfs1(int u,int top) {
dp[u][0]=dp[u][1]={0};
for(auto e:G[u]) if(e.v!=fa[u]&&e.v!=son[u]) dfs1(e.v,e.v);
if(u==1) return ;
tps[top].push_back(u);
if(son[u]) dfs1(son[u],top);
if(u!=top) return ;
solve1(0,tps[u].size()-1,tps[u]);
int p=fa[u];
//auto qqq=dp[p][1]*f[0][1][0]+fw[u];
dp[p][0]=max(dp[p][0]*f[0][0][0],dp[p][1]*f[0][1][0]+fw[u]);
dp[p][1]=dp[p][1]*f[0][0][0];
for(int i=0;i<(int)tps[u].size();++i) clr(f[i]);
}
*/
void solve1(int l,int r,vector<int>&id){
if(l==r){
f[l][0][0]=dp[id[l]][0] ;
f[l][0][1]=f[l][1][0]=dp[id[l]][1] ;
return ;
}
int s = 0 ; for(ri i = l ; i <= r ; i++)s+=dp[id[i]][0].size() ;
for(ri i = l + 1 , t = dp[id[l]][0].size() ; i <= r ; i++){
t+=dp[id[i]][0].size() ;
if(t*2<=s)continue ;
solve1(l,i-1,id),solve1(i,r,id) ;
f[l]=mul(f[l],f[i],fw[id[i]]) ; break ;
}
}
void out(int u){
cout<<u<<"!!!"<<endl ;
for(ri i = 0 ; i < dp[u][0].size() ; i++)cout<<dp[u][0][i]<<" " ; cout<<endl ;
for(ri i = 0 ; i < dp[u][1].size() ; i++)cout<<dp[u][1][i]<<" " ; cout<<endl ;
cout<<"@@@"<<endl ;
}
void dfs1(int u,int _top){
dp[u][0]=dp[u][1]={0} ; tps[_top].pb(u) ;
if(!son[u]&&u!=1)return ;
dfs1(son[u],_top) ; for(auto e : G[u])if(e.v!=fa[u]&&e.v!=son[u])dfs1(e.v,e.v) ;
for(auto e : G[u])if(e.v!=fa[u]&&e.v!=son[u])ps.pb(e.v) ;
if(ps.size()){
solve0(0,ps.size()-1,ps) ;
dp[u][0]=g[0][0],dp[u][1]=g[0][1] ;
ps.clear() ; for(ri i = 0 ; i < (int)ps.size() ; i++)poly().swap(g[i][0]),poly().swap(g[i][1]) ;
}
if(u==_top){
solve1(0,tps[u].size()-1,tps[u]) ;
dp[u][0]=f[0][0][0],dp[u][1]=f[0][1][0] ;
for(ri i = 0 ; i < (int)tps[u].size() ; i++)clr(f[i]) ;
}
}
int n ;
bool m_ed ;
signed main(){
//freopen("a.in","r",stdin) ;
read(n) ; for(ri i = 1 , u , v , w ; i < n ; i++)read(u,v,w),G[u].pb({v,w}),G[v].pb({u,w}) ;
dfs0(1,0),son[1]=0,dfs1(1,1) ;
for(ri i = 1 ; i < (int)dp[1][0].size() ; i++)printf("%lld ",dp[1][0][i]) ;
for(ri i = dp[1][0].size() ; i < n ; i++)printf("? ") ;
return 0 ;
}
//g++ -std=c++14 a.cpp -o a -Wall -Wextra -O2 -fsanitize=address,undefined -Wno-misleading-indentation
詳細信息
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 54464kb
input:
5 1 2 3 2 3 5 2 4 4 3 5 2
output:
5 6 6 ?
result:
wrong answer 1st lines differ - expected: '5 6 ? ?', found: '5 6 6 ? '