QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#303515 | #7419. Jiry Matchings | xztmax67 | WA | 22ms | 60936kb | C++14 | 5.5kb | 2024-01-12 17:41:53 | 2024-01-12 17:41:53 |
Judging History
answer
#include<bits/stdc++.h>
#define pii pair<int,int>
#define fi first
#define se second
#define mk make_pair
#define pb push_back
#define ll long long
using namespace std;
const int N=2e5+100;
const ll inf=1e18;
int n,dfn_tot;
int top[N],en[N],dfn[N],id[N],fav[N],siz[N],son[N];
vector<pii>g[N];
void dfs1(int x,int fa,int faz)
{
if(fa)g[x].erase(find(g[x].begin(),g[x].end(),mk(fa,faz)));
siz[x]=1;for(auto t:g[x])
{
int y=t.fi,z=t.se;fav[y]=z;
dfs1(y,x,z),siz[x]+=siz[y];
if(siz[son[x]]<siz[y])son[x]=y;
}
if(son[x])g[x].erase(find(g[x].begin(),g[x].end(),mk(son[x],fav[son[x]])));
}
struct Poly
{
int L,R,tag;//顶端(dfn 序) + 左边是否一定和右边相同
vector<ll>dp[2][2];//左端匹配/未匹配 + 右端匹配/未匹配
auto&operator[](const int&x){return dp[x];}
Poly(){clear(1);dp[0][0][0]=0;tag=1;}
void clear(int siz)
{
L=R=0;for(int a:{0,1})for(int b:{0,1})
{dp[a][b].resize(siz);for(auto&x:dp[a][b])x=-inf;}
}
Poly add(int p,int v)
{
int siz=dp[0][0].size()+1;
Poly now;now.clear(siz);now.L=now.R=p;now.tag=1;
for(int i=0;i<siz-1;i++)
now[0][0][i]=max({now[0][0][i],dp[0][0][i],dp[0][1][i],dp[1][1][i],dp[1][0][i]}),
now[1][1][i+1]=max({now[1][1][i+1],dp[0][0][i]+v,dp[0][1][i]+v});
return now;
}
friend Poly merge1(Poly x,Poly y)//其中 Lx=Rx=Ly=Ry
{
int siz=x[0][0].size()+y[0][0].size()-1;
Poly now;now.clear(siz);now.L=now.R=x.L;now.tag=1;
for(int t1:{0,1})for(int t2:{0,1})
{
if(t1==1&&t2==1)continue;
int t=t1|t2;now[t][t][0]=max(now[t][t][0],x[t1][t1][0]+y[t2][t2][0]);
int px=0,py=0;for(int i=1;i<siz;i++)
{
if(px==x[t1][t1].size()-1)py++;
else if(py==y[t2][t2].size()-1)px++;
else if(x[t1][t1][px+1]+y[t2][t2][py]>
x[t1][t1][px]+y[t2][t2][py+1])px++;else py++;
now[t][t][i]=max(now[t][t][i],x[t1][t1][px]+y[t2][t2][py]);
}
// cout<<x[t1][t1].size()<<" "<<y[t2][t2].size()<<" "<<px<<" "<<py<<' '<<siz<<endl;
}
return now;
}
friend Poly merge2(Poly&x,Poly&y)//其中 [Lx Rx][Ly,Ry] Ly=Rx+1
{
int v=fav[dfn[y.L]],sx=x[0][0].size(),sy=y[0][0].size(),siz=sx+sy;
Poly now;now.clear(siz);now.L=x.L;now.R=y.R;now.tag=0;
for(int t1:{0,1})for(int t2:{0,1})
{
now[t1][t2][0]=max(now[t1][t2][0],max(x[t1][0][0],x[t1][1][0])+max(y[0][t2][0],y[1][t2][0]));
int px=0,py=0;for(int i=1;i<siz-1;i++)
{
if(px==sx-1)py++;
else if(py==sy-1)px++;
else if(max(x[t1][0][px+1],x[t1][1][px+1])+max(y[0][t2][py],y[1][t2][py])>
max(x[t1][0][px],x[t1][1][px])+max(y[0][t2][py+1],y[1][t2][py+1]))px++;else py++;
now[t1][t2][i]=max(now[t1][t2][i],max(x[t1][0][px],x[t1][1][px])+max(y[0][t2][py],y[1][t2][py]));
}
// if(x.tag&&t1)continue;if(y.tag&&t2)continue;
now[x.tag|t1][y.tag|t2][1]=max(now[x.tag|t1][y.tag|t2][1],x[t1][0][0]+y[0][t2][0]+v);
px=0,py=0;for(int i=2;i<siz;i++)
{
if(px==sx-1)py++;
else if(py==sy-1)px++;
else if(x[t1][0][px+1]+y[0][t2][py]>x[t1][0][px]+y[0][t2][py+1])px++;else py++;
now[x.tag|t1][y.tag|t2][i]=max(now[x.tag|t1][y.tag|t2][i],x[t1][0][px]+y[0][t2][py]+v);
}
}
// if(v==5)cout<<x[0][0][0]<<" "<<y[0][0][0]<<' '<<x.tag<<" "<<y.tag<<endl;
return now;
/*
now[t1][t2][a+b] = max(x[t1][0/1][a]+y[0/1][t2][b],
x[t1][0][a(-1)]+y[0][t2][b(-1)])
*/
}
};
Poly dp[N];
void dfs2(int x,int topf)
{
top[x]=topf;
dfn[id[x]=++dfn_tot]=x;en[top[x]]=id[x];
// cout<<x<<' '<<topf<<' '<<fav[x]<<endl;
// dp[x].clear(1);dp[x].L=dp[x].R=id[x];dp[x][0][0][0]=0;
if(son[x])
{
dfs2(son[x],topf);
// dp[x]=dp[son[x]].add(id[x],fav[son[x]]),dp[son[x]].clear(0);
}
for(auto t:g[x])
{
int y=t.fi;dfs2(y,y);
// dp[x]=merge1(dp[x],dp[y].add(id[x],t.se));
// dp[y].clear(0);
}
}
void get(int l,int r)
{
if(l>=r)return;
int p=l+r>>1;
//[l,p] 和 [p+1,r]
// for(int i=l+1;i<r;i++)
// if(min(siz[dfn[l]]-siz[son[dfn[p]]],siz[son[dfn[p]]]-siz[son[dfn[r]]])<
// min(siz[dfn[l]]-siz[son[dfn[i]]],siz[son[dfn[i]]]-siz[son[dfn[r]]]))p=i;
// cout<<l<<" "<<r<<' '<<p<<endl;exit(0);
// cout<<l<<" "<<r<<" "<<dfn[p]<<' '<<son[dfn[p]]<<" "<<siz[son[dfn[p]]]<<endl;
// cout<<l<<" "<<r<<' '<<dfn[p]<<' '<<son[dfn[p]]<<" "<<dfn[p+1]<<' '<<siz[son[dfn[p]]]<<endl;
get(l,p);get(p+1,r);
if(p+1<=r)dp[dfn[l]]=merge2(dp[dfn[l]],dp[dfn[p+1]]),dp[dfn[p+1]].clear(0);
// cout<<"Seg:"<<" "<<dfn[l]<<' '<<l<<" "<<r<<endl;
// for(auto y:dp[dfn[l]][0][0])cout<<y<<" ";cout<<endl;
// for(auto y:dp[dfn[l]][1][0])cout<<y<<' ';cout<<endl;
// for(auto y:dp[dfn[l]][0][1])cout<<y<<" ";cout<<endl;
// for(auto y:dp[dfn[l]][1][1])cout<<y<<' ';cout<<endl;
}
void solve(int l,int r)
{
for(int i=l;i<=r;i++)
{
int x=dfn[i];for(auto t:g[x])
{
int y=t.fi;solve(id[y],en[y]);
dp[x]=merge1(dp[x],dp[y].add(id[x],t.se));
dp[y].clear(0);
}
dp[x].L=dp[x].R=id[x];dp[x].tag=1;
}
get(l,r);
}
signed main()
{
freopen("r.in","r",stdin);
// freopen("my.out","w",stdout);
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n;
for(int i=1,x,y,z;i<n;i++)cin>>x>>y>>z,g[x].push_back(mk(y,z)),g[y].push_back(mk(x,z)) ;
dfs1(1,0,0);dfs2(1,1);
solve(1,en[1]);
// exit(0);
for(int i=1;i<n;i++)
{
// cout<<dp[1][0][0].size()<<endl;
if(i>=dp[1][0][0].size()){cout<<"? ";continue;}
ll v=max({dp[1][0][0][i],dp[1][0][1][i],dp[1][1][0][i],dp[1][1][1][i]});
if(v<-1e15)cout<<"? ";else cout<<v<<' ';
}
// cout<<dp[1][0][0][4]<<" "<<dp[1][0][1][4]<<' '<<dp[1][1][0][4]<<" "<<dp[1][1][1][4]<<endl;
cout<<endl;
return 0;
}
/*
5
1 2 3
2 3 5
3 4 6
4 5 1
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 22ms
memory: 60936kb
input:
5 1 2 3 2 3 5 2 4 4 3 5 2
output:
result:
wrong answer 1st lines differ - expected: '5 6 ? ?', found: ''