QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#431455 | #7419. Jiry Matchings | IIIIIlIIIl | WA | 7ms | 53304kb | C++14 | 5.0kb | 2024-06-05 16:44:36 | 2024-06-05 16:44:38 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
const int maxn=2e5+5;
const int inf=1e16;
int n,siz[maxn],ff[maxn],dep[maxn],son[maxn],w[maxn],w1[maxn],cnt;
std::vector<std::pair<int,int>>v[maxn];
std::vector<int>f[2][maxn],g[2][maxn],h[2][2][maxn],ans;
std::vector<int>merge(std::vector<int>&a,std::vector<int>&b){
if(a.empty()||b.empty())return {};
int siza=a.size(),sizb=b.size(),pa=1,pb=1,pos=1;
std::vector<int>res(siza+sizb-1);
res[0]=a[0]+b[0];
while(pa<siza&&pb<sizb){
if(a[pa]-a[pa-1]>b[pb]-b[pb-1])res[pos]=res[pos-1]+a[pa]-a[pa-1],pa++;
else res[pos]=res[pos-1]+b[pb]-b[pb-1],pb++;
pos++;
}
while(pa<siza)res[pos]=res[pos-1]+a[pa]-a[pa-1],pa++,pos++;
while(pb<sizb)res[pos]=res[pos-1]+b[pb]-b[pb-1],pb++,pos++;
// for(auto x:a)printf("a=%lld ",x);printf("\n");
// for(auto x:b)printf("b=%lld ",x);printf("\n");
// for(auto x:res)printf("%lld ",x);printf("\n");
return res;
}
std::vector<int>getmax(std::vector<int>&a,std::vector<int>&b){
int siza=a.size(),sizb=b.size();std::vector<int>res(std::max(siza,sizb),-inf);
// for(auto x:a)printf("a=%lld ",x);printf("\n");
// for(auto x:b)printf("b=%lld ",x);printf("\n");
for(int i=0;i<siza;i++)res[i]=std::max(res[i],a[i]);
for(int i=0;i<sizb;i++)res[i]=std::max(res[i],b[i]);
// for(auto x:res)printf("res=%lld ",x);printf("\n");
return res;
}
std::vector<int>move(std::vector<int>&a,int val){
int siza=a.size();std::vector<int>res(siza+1,-inf);
// for(auto x:a)printf("a=%lld ",x);printf("\n");
// printf("fuck=%lld\n",val);
for(int i=0;i<siza;i++)res[i+1]=std::max(res[i+1],a[i]+val);
// for(auto x:res)printf("res=%lld ",x);printf("\n");
return res;
}
void solve(int l,int r){
if(l==r)return;
int mid=l+r>>1;
solve(l,mid);solve(mid+1,r);
std::vector<int>a=merge(g[0][l],g[0][mid+1]);
std::vector<int>b=merge(g[1][l],g[0][mid+1]);
std::vector<int>c=merge(g[0][l],g[1][mid+1]);
g[0][l]=a,g[1][l]=getmax(b,c);g[0][mid+1].clear();g[1][mid+1].clear();
}
void Solve(int l,int r){
if(l==r)return;
int mid=l+r>>1;
Solve(l,mid);Solve(mid+1,r);
std::vector<int>v1[2][2];
for(int c1=0;c1<=1;c1++){
for(int c2=0;c2<=1;c2++){
for(int c3=0;c3<=1;c3++){
for(int c4=0;c4<=1;c4++){
std::vector<int>tmp=merge(h[c1][c2][l],h[c3][c4][mid+1]);
if(tmp.size()){
v1[c1][c4]=getmax(v1[c1][c4],tmp);
if(c2==0&&c3==0){
tmp=move(tmp,w1[mid]);
if(l==mid)c1=1;if(mid+1==r)c4=1;
v1[c1][c4]=getmax(v1[c1][c4],tmp);
}
}
}
}
}
}
for(int i=0;i<=1;i++)for(int j=0;j<=1;j++)h[i][j][l]=v1[i][j];
}
void dfs1(int now,int fa){
dep[now]=dep[fa]+1,ff[now]=fa,siz[now]=1;
for(auto i:v[now]){
int to=i.first,val=i.second;
if(to!=fa){
dfs1(to,now);siz[now]+=siz[to];
if(siz[to]>siz[son[now]])son[now]=to,w[now]=val;
}
}
}
void dfs2(int now,int topf){
if(!son[now]){
f[0][now]=std::vector<int>(1,0);
f[1][now]=std::vector<int>(1,0);
return;
}
dfs2(son[now],topf);
for(auto i:v[now]){
int to=i.first;
if(to==ff[now]||to==son[now])continue;
dfs2(to,to);
}
cnt=0;
for(auto i:v[now]){
int to=i.first,val=i.second;
if(to==ff[now]||to==son[now])continue;
cnt++;
g[0][cnt]=getmax(f[0][to],f[1][to]);g[1][cnt]=move(f[0][to],val);
f[0][to].clear();f[1][to].clear();
}
if(!cnt)f[0][now]=std::vector<int>(1,0),f[1][now]=std::vector<int>(1,0);
else{
solve(1,cnt);
f[0][now]=g[0][1],f[1][now]=g[1][1];g[0][1].clear();g[1][1].clear();
}
if(now==topf){
cnt=0;int cur=now;
while(cur){
cnt++;
h[0][0][cnt]=f[0][cur];h[1][1][cnt]=f[1][cur];w1[cnt]=w[cur];
cur=son[cur];
}
Solve(1,cnt);
f[0][now]=getmax(h[0][1][1],h[0][0][1]);
f[1][now]=getmax(h[1][1][1],h[1][0][1]);
for(int i=1;i<=cnt;i++)h[0][0][i].clear(),h[0][1][i].clear(),h[1][0][i].clear(),h[1][1][i].clear();
}
}
signed main(){
scanf("%lld",&n);
for(int i=1,x,y,z;i<n;i++){
scanf("%lld%lld%lld",&x,&y,&z);
v[x].push_back({y,z});v[y].push_back({x,z});
}
// h[0][0][1]=std::vector<int>(1,0);h[1][1][2]=std::vector<int>(1,0);
// h[0][0][2]=std::vector<int>(1,0);h[1][1][2]=std::vector<int>(1,0);w1[1]=3;
// Solve(1,2);
// for(auto i:h[1][1][1])printf("val=%lld\n",i);
dfs1(1,0);dfs2(1,1);
ans=getmax(f[0][1],f[1][1]);
for(int i=1;i<ans.size();i++)printf("%lld ",ans[i]);
for(int i=ans.size();i<n;i++)printf("? ");
return 0;
}
/*
10
2 8 -5
5 10 5
3 4 -5
1 6 5
3 9 5
1 7 -3
4 8 -5
10 8 -5
1 8 -3
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 4ms
memory: 52996kb
input:
5 1 2 3 2 3 5 2 4 4 3 5 2
output:
5 6 ? ?
result:
ok single line: '5 6 ? ? '
Test #2:
score: 0
Accepted
time: 6ms
memory: 50000kb
input:
10 2 8 -5 5 10 5 3 4 -5 1 6 5 3 9 5 1 7 -3 4 8 -5 10 8 -5 1 8 -3
output:
5 10 15 10 ? ? ? ? ?
result:
ok single line: '5 10 15 10 ? ? ? ? ? '
Test #3:
score: 0
Accepted
time: 7ms
memory: 53304kb
input:
2 1 2 35
output:
35
result:
ok single line: '35 '
Test #4:
score: -100
Wrong Answer
time: 7ms
memory: 50028kb
input:
100 75 98 770328247 87 98 -219729992 98 35 578758971 98 93 -348642106 63 98 -638036803 83 25 -744163505 21 98 810313834 97 25 131957988 19 98 -711567435 8 25 68874404 43 98 184473508 28 94 171940607 92 28 971759198 51 98 -674532123 28 6 797727320 98 95 1154600 98 58 683765502 28 12 358426364 4 42 65...
output:
990461444 1951945471 2906346403 3565958017 4484694703 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
result:
wrong answer 1st lines differ - expected: '990461444 1951945471 290634640...? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?', found: '990461444 1951945471 290634640... ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? '