版本比较
心连心题解byTwelveHexar
正文
增加 10 行删除 454 行
123452 行未改动89101112131415161718192021222324252627282930313233343536373839405 行未改动46474849505152535455565758594 行未改动6465666768697071723 行未改动7677787980818283848586874 行未改动92939495969798991001011021031041051061071081091101111121134 行未改动118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554## [Part 00 读题](/problem/T741752)## Part 01 十分显然的前置结论1. 图是基环树。2 行未改动3. 操作的先后顺序没有任何影响。\更进一步,一直重复降低边权和速度最快的操作总是最优的。\更更近一步,如果没有办法降低边权和,那么总边权和最小值一定是初始的总边权和;否则,最少的操作次数是边权总和除以最快的降低边权和速度(下取整)+1(好了可以全部输出 yes 骗分了)## Part 02 朴素到不能再朴素的算法暴力枚举操作哪条边,并暴力计算指令传递,$\Theta(n^2)$。$20$ 分到手。代码不放了,这要是不会写可以退役了。## Part 03 第一个核心优化人帅心善的出题人贴心赠送了 subtask 2~5。我们来浅浅的端详一下 subtask 2 的限制。> $l\le 6$,其中 $l$ 是基环树环长。~~何意味?~~环长极小的情况下,几乎所有边都不在基环树的“基环”上而在“树”上。为了方便,我们定义“子树”:将基环树的“基环”的边去掉,全部节点和剩下的边构成一个森林。可以证明这个森林里每棵树都有且仅有 $1$ 个环上节点。令每棵树的根都是其唯一的环上的节点。鉴于图的大部分在子树上,而子树极其少,我们考虑把子树整体来看。我们发现,当操作边不在一棵子树上的时候,一棵子树受到的影响无碍乎 $2$ 种情况:1. 从环上的边收到了指令 $1$。5 行未改动1. 向环上的边发送指令 $1$。2. 向环上的边发送指令 $-1$。而且每种情况可以贪心的取最优值,而计算就要用到换根 dp 了。(大胆的预言一下这里会有很多人写出 bug)经过了这么一番折腾,我们成功且高度地“封装”了每棵子树,使得我们无需再去考虑每个子树内部的传递。或者说,基环树只剩基环了。但是操作相应的要分为 $3$ 种:1. 操作环上的边。2. 操作子树内的边,使得子树向环上的边发送指令 $1$。4 行未改动又有 $15$ 分到手。## Part 04 第二个核心优化的铺垫浅浅的端详一下 subtask 4。> $n-l\le5$也就是说,这回基环树的大头在基环上而不是子树上了。3 行未改动顺带一提,几乎每次子树旋转都会使子树贡献取相反数。由此我们便得出了 $\Theta((n-l)n)$ 的做法。又到手了…… $5$ 分?!看来这个做法还得改改。## Part 05 第二个核心优化摆在我们面前的有 $3$ 条路:4 行未改动但其实只有一条路,因为第一条路是死路,而另两条路可以一起走。考虑一个挂满了子树的基环,对一条环上边进行操作,然后将操作边右移一步。如果我们认为是只有环上的指令数转了,边和子树都没有转,我们惊讶地发现……所有边和子树的贡献都取相反数了!……不对,怎么有几个不和谐的地方?!是的,操作边的附近和对面会有一些贡献不取相反数的位置。并且随着环上的指令的旋转,这些位置会一起转,而修正系数是由环的指令决定的。另两种情况也类似。这样,环上的指令旋转的时候,我们可以先大刀阔斧地给所有贡献取相反数,再修正特定位置贡献。也就是说,我们封装了基环树的基环。它啥都不剩了。## Part 06 汇总把基环树拆成基环和子树,$\Theta(n)$。4 行未改动把环的指令转 3 圈,$\Theta(n)$。总计,$\Theta(n)$。注意不要见祖宗哦## Part 07 代码以防我自己看不懂,很详细的解释清楚了每个参数。```cpp#include<iostream>#include<vector>#include<queue>//#define debugdp0//#define debugdp1//#define debugres//#define debug2//#define debug3//#define debugloop//#define debugREusing namespace std;const int N=100005;int n,loophead,looplen;long long sumw,ans=200000ll/*2e5*/;vector<int> g[N];int f[N];//If u on loop,f[u] marks the next node.If not,f[u] marks the father node.int in[N];//TEMPbool onloop[N];long long dp0[N]/*impact when outedge which sends in signal is 1*/,dp1[N][2];//relative increasement minimized when 0:send -1 out,1:send 1 outvoid detectloop()//O(n){queue<int> q;for(int i=1;i<=n;i++) if(in[i]==1) q.push(i);while(q.size()){int u=q.front();q.pop();for(int i=0;i<g[u].size();i++)if(in[g[u][i]]){in[g[u][i]]--;in[u]--;if(in[g[u][i]]==1) q.push(g[u][i]);}}for(int i=1;i<=n;i++)if(in[i]==2){loophead=i;looplen++;onloop[i]=true;#ifdef debugloopcout<<i<<" onloop\n";#endiffor(int j=0;j<g[i].size();j++)if(in[g[i][j]]){f[i]=g[i][j];int u=g[i][j];while(u!=i){onloop[u]=true;#ifdef debugloopcout<<u<<" onloop\n";#endiflooplen++;for(int k=0;k<g[u].size();k++)if(in[g[u][k]]&&f[g[u][k]]!=u){f[u]=g[u][k];u=g[u][k];break;}}break;}break;}}void dfs1(int,int,int,int);void dfs0(int u){#ifdef debugREcout<<"dfs0"<<u<<endl;#endiffor(int i=0;i<g[u].size();i++)if(g[u][i]!=f[u]&&!onloop[g[u][i]]){f[g[u][i]]=u;dfs0(g[u][i]);dp0[u]-=dp0[g[u][i]];dp0[u]-=1;//send -1}#ifdef debugdp0cout<<"dp0["<<u<<"]="<<dp0[u]<<'\n';#endifif(onloop[u]){dp1[u][0]=dp1[u][1]=200000ll;//2e5dfs1(u,u,1,0);}}void dfs1(int u,int rt,int mfout,int mfimpact/*considering the father edge*/){if(u!=rt){#ifdef debugdp1cout<<"dp1["<<rt<<"]["<<mfout<<"] records "<<dp0[u]<<"+"<<mfimpact<<"="<<dp0[u]+mfimpact<<'\n';#endifdp1[rt][mfout]=min(dp1[rt][mfout],dp0[u]+mfimpact);}mfimpact=-mfimpact;mfout=1-mfout;for(int i=0;i<g[u].size();i++)if(g[u][i]!=f[u]&&!onloop[g[u][i]]){mfimpact-=dp0[g[u][i]];mfimpact-=1;//send -1}for(int i=0;i<g[u].size();i++)if(g[u][i]!=f[u]&&!onloop[g[u][i]]){//send -1 --> send 1//dp0* -1 --> dp0* 0//edge -1 --> edge 1dfs1(g[u][i],rt,mfout,mfimpact+2+dp0[g[u][i]]);}}int main()//O(n) in total{ios::sync_with_stdio(false);cin>>n;for(int i=1,u,v,w;i<=n;i++){cin>>u>>v>>w;g[u].push_back(v);g[v].push_back(u);in[u]++;in[v]++;sumw+=w;}detectloop();for(int i=1;i<=n;i++)if(onloop[i]) dfs0(i);long long res,mark/*edge mark*/,u,opu,opm;//caculation of all node_out1 type//start with loopheadres=0;res+=dp1[loophead][1];mark=1,u=loophead;for(int i=1;i<=looplen/2;i++){res+=mark;//edgeu=f[u];res+=mark*dp0[u];//nodemark=-mark;}if(looplen%2==1)//opposite an edge{//in this view,'+' means mark and '-' means -mark.// u>>>>>// >-> - >0< - <-< + will be turn into// >+> + >-> - >0< -//this edge bears no contribution.opu=f[u],opm=-2*mark;//marking this node is supposed to be caculated with more -2*mark times of contribution\upon recalculating//the edges bears more -2*mark=opm contribution upon recalculating.u=f[u],mark=-mark;//pass the edge}else{// u//+ >-> 0 <-< + <+< - will be turn into//- >+> + >-> 0 <-< +res+=2*mark*dp0[u];//to compensate the contribution of the opposite nodeopu=u,opm=mark;//marking this node is supposed to be caculated with more mark times of contribution upon\recalculating,and SO DOES THE NEXT ONE!!!!!!!!//the edge bears more -2*mark=-2opm contribution upon recalculating.mark=-mark;//passing the node}for(int i=1;i<=looplen/2;i++){res+=mark*dp0[u];//noderes+=mark;//edgeu=f[u];mark=-mark;}ans=min(ans,res);#ifdef debugrescout<<"precalc node_out1 with beginnode="<<loophead<<":"<<res<<'\n';#endif//OPM SHOULDN'T BE CHANGED DURING RECALCULATING!!!!!!!//start recalculatingfor(u=loophead;f[u]!=loophead;){//in this view,+ means k=1 and - means k=-1// u>>>>>//+ <+< # >+> + >-> - will be turn into//- <-< + <+< # >+> +res-=dp1[u][1];res-=dp0[u];res-=2;res=-res;//reverse almost every contributionu=f[u];res+=dp0[u];res+=dp1[u][1];if(looplen%2==1)//recalculating operations mentioned above{res+=opm;res+=dp0[opu]*opm;opu=f[opu];}else{res+=-2*opm;res+=dp0[opu]*opm;opu=f[opu];res+=dp0[opu]*opm;}ans=min(ans,res);#ifdef debugrescout<<"@Recalc node_out1 with beginnode="<<u<<":"<<res<<'\n';#endif}#ifdef debugrescout<<'\n';#endif//caculation of all node_out-1 type//start with loopheadres=0;res+=dp1[loophead][0];#ifdef debug2cout<<"$node "<<loophead<<" self-contributes "<<dp1[loophead][0]<<'\n';#endifmark=-1,u=loophead;for(int i=1;i<=looplen/2;i++){res+=mark;//edge#ifdef debug2cout<<"$edge from "<<u<<" to "<<f[u]<<" contributes "<<mark<<'\n';#endifu=f[u];res+=mark*dp0[u];//node#ifdef debug2cout<<"$node "<<u<<" contributes "<<mark<<"*"<<dp0[u]<<'\n';#endifmark=-mark;}if(looplen%2==1)//opposite an edge{// u>>>>>// >-> - >0< - <-< + will be turn into// >+> + >-> - >0< -//this edge bears no contribution.opu=f[u],opm=-2*mark;//marking this node is supposed to be caculated with more -2*mark times of contribution\upon recalculating//the edges bears more -2*mark=opm contribution upon recalculating.u=f[u],mark=-mark;//pass the edge}else{// u//+ >-> 0 <-< + <+< - will be turn into//- >+> + >-> 0 <-< +res+=2*mark*dp0[u];//to compensate the contribution of the opposite nodeopu=u,opm=mark;//marking this node is supposed to be caculated with more mark times of contribution upon\recalculating,and SO DOES THE NEXT ONE!!!!!!!!//the edge bears more -2*mark=-2opm contribution upon recalculating.mark=-mark;//passing the node}for(int i=1;i<=looplen/2;i++){res+=mark*dp0[u];//node#ifdef debug2cout<<"$node "<<u<<" contributes "<<mark<<"*"<<dp0[u]<<'\n';#endifres+=mark;//edge#ifdef debug2cout<<"$edge from "<<u<<" to "<<f[u]<<" contributes "<<mark<<'\n';#endifu=f[u];mark=-mark;}ans=min(ans,res);#ifdef debugrescout<<"precalc node_out-1 with beginnode="<<loophead<<":"<<res<<'\n';#endif//OPM SHOULDN'T BE CHANGED DURING RECALCULATING!!!!!!!//start recalculatingfor(u=loophead;f[u]!=loophead;){// u>>>>>//- <-< # >-> - >+> + will be turn into//+ <+< - <-< # >-> -res-=dp1[u][0];res+=dp0[u];res+=2;res=-res;//reverse almost every contributionu=f[u];res-=dp0[u];res+=dp1[u][0];if(looplen%2==1)//recalculating operations mentioned above{res+=opm;res+=dp0[opu]*opm;opu=f[opu];}else{res+=-2*opm;res+=dp0[opu]*opm;opu=f[opu];res+=dp0[opu]*opm;}ans=min(ans,res);#ifdef debugrescout<<"@Recalc node_out-1 with beginnode="<<u<<":"<<res<<'\n';#endif}#ifdef debugrescout<<'\n';#endif//caculation of all loopedge type//start with loophead---f[loophead]res=1;mark=-1,u=f[loophead];res+=dp0[loophead]+dp0[f[loophead]];#ifdef debug3cout<<"$edge from "<<loophead<<" to "<<f[loophead]<<" contributes 1\n";cout<<"$node "<<loophead<<" contributes 1*"<<dp0[loophead]<<'\n';cout<<"$node "<<f[loophead]<<" contributes 1*"<<f[dp0[loophead]]<<'\n';#endiffor(int i=1;i<=(looplen-1)/2;i++){res+=mark;//edge#ifdef debug3cout<<"$edge from "<<u<<" to "<<f[u]<<" contributes "<<mark<<'\n';#endifu=f[u];res+=mark*dp0[u];//node#ifdef debug3cout<<"$node "<<u<<" contributes "<<mark<<"*"<<dp0[u]<<'\n';#endifmark=-mark;}if(looplen%2==0)//opposite an edge{// u>>>>>// >-> - >0< - <-< + will be turn into// >+> + >-> - >0< -//this edge bears no contribution.opu=f[u],opm=-2*mark;//marking this node is supposed to be caculated with more -2*mark times of contribution\upon recalculating#ifdef debug3cout<<"$opu="<<opu<<" opm="<<opm<<'\n';#endif//the edges bears more -2*mark=opm contribution upon recalculating.u=f[u],mark=-mark;//pass the edge}else{// u//+ >-> 0 <-< + <+< - will be turn into//- >+> + >-> 0 <-< +res+=2*mark*dp0[u];//to compensate the contribution of the opposite nodeopu=u,opm=mark;//marking this node is supposed to be caculated with more mark times of contribution upon\recalculating,and SO DOES THE NEXT ONE!!!!!!!!#ifdef debug3cout<<"$opu="<<opu<<" opm="<<opm<<" (double operation)\n";#endif//the edge bears more -2*mark=-2opm contribution upon recalculating.mark=-mark;//passing the node}for(int i=1;i<=(looplen-1)/2;i++){res+=mark*dp0[u];//node#ifdef debug3cout<<"$node "<<u<<" contributes "<<mark<<"*"<<dp0[u]<<'\n';#endifres+=mark;//edge#ifdef debug3cout<<"$edge from "<<u<<" to "<<f[u]<<" contributes "<<mark<<'\n';#endifu=f[u];mark=-mark;}ans=min(ans,res);#ifdef debugrescout<<"$precalc edge with beginedge="<<loophead<<"->"<<f[loophead]<<":"<<res<<'\n';#endif//OPM SHOULDN'T BE CHANGED DURING RECALCULATING!!!!!!!//start recalculatingfor(u=loophead;f[u]!=loophead;){// u>>>>>//- <-< + <#> + >-> - >+> + will be turn into//+ <+< - <-< + <#> + >-> -res-=2*dp0[f[u]];res=-res;//reverse almost every contributionu=f[u];if(looplen%2==0)//recalculating operations mentioned above{res+=opm;res+=dp0[opu]*opm;opu=f[opu];}else{res+=-2*opm;res+=dp0[opu]*opm;opu=f[opu];res+=dp0[opu]*opm;}ans=min(ans,res);#ifdef debugrescout<<"@Recalc edge with beginedge="<<u<<"->"<<f[u]<<":"<<res<<'\n';#endif}//IT TAKES HEXAR V. 2.5 HOURS TO FINISH THIS STD//CURRENTLY 6 ERRORS ARE FIXED ALREADY:mark error,latter-half precalculate error,side-beginning-edge recalculate-contribution error,subtree f[] missing,debugment remained,opposite node contribution error.//I JUST CAN'T IMAGINE HOW REDSTONE OTHERS WILL BE WHEN SOLVING THISif(ans<0) cout<<"No "<<(sumw+1-ans-1)/(-ans);else cout<<"Yes "<<sumw;return 0;}```> 心之所向,一马平川。123452 行未改动891011121314151617181920212223242526272829303132333435365 行未改动4243444546474849505152534 行未改动5859606162636465663 行未改动707172737475767778794 行未改动848586878889909192939495969798994 行未改动104105106107108109110## [Part 00 读题](/problem/P15976)## Part 01 十分显然的前置结论1. 图是基环树。2 行未改动3. 操作的先后顺序没有任何影响。\更进一步,一直重复降低边权和速度最快的操作总是最优的。\更更近一步,如果没有办法降低边权和,那么总边权和最小值一定是初始的总边权和;否则,最少的操作次数是边权总和除以最快的降低边权和速度(下取整)+1## Part 02 朴素到不能再朴素的算法暴力枚举操作哪条边,并暴力计算指令传递,$\Theta(n^2)$。$20$ 分到手。(为什么赛时就没啥人做这个啊!!!)## Part 03 第一个核心优化我们来浅浅的端详一下 subtask 1 的限制。> $l\le 6$,其中 $l$ 是基环树环长。~~何意味?~~环长极小的情况下,几乎所有边都不在基环树的“基环”上而在“树”上。为了方便,我们定义“子树”:将基环树的“基环”的边去掉,全部节点和剩下的边构成一个森林。可以证明这个森林里每棵树都有且仅有 $1$ 个环上节点。令每棵树的根都是其唯一的环上的节点。鉴于图的大部分在子树上,而子树数量极其少,我们考虑把子树整体来看。我们发现,当操作边不在一棵子树上的时候,一棵子树受到的影响无碍乎 $2$ 种情况:1. 从环上的边收到了指令 $1$。5 行未改动1. 向环上的边发送指令 $1$。2. 向环上的边发送指令 $-1$。而且每种情况可以贪心的取最优值,而计算就要用到换根 dp 了。经过了这么一番折腾,我们成功且高度地“封装”了每棵子树,使得我们无需再去考虑每个子树内部的传递。但是操作相应的要分为 $3$ 种:1. 操作环上的边。2. 操作子树内的边,使得子树向环上的边发送指令 $1$。4 行未改动又有 $15$ 分到手。## Part 04 第二个核心优化的铺垫浅浅的端详一下 subtask 3。> $n-l\le5$也就是说,这回基环树的大头在基环上而不是子树上了。3 行未改动顺带一提,几乎每次子树旋转都会使子树贡献取相反数。由此我们便得出了 $\Theta((n-l)n)$ 的做法。又到手了…… $5$ 分。## Part 05 第二个核心优化摆在我们面前的有 $3$ 条路:4 行未改动但其实只有一条路,因为第一条路是死路,而另两条路可以一起走。考虑一个挂满了子树的基环,对一条环上边进行操作,然后将操作边右移一步。如果我们认为是只有环上的指令数转了,边和子树都没有转,我们惊讶地发现,所有边和子树的贡献都取相反数了!……不对,操作边的附近和对面会有一些贡献不取相反数的位置。并且随着环上的指令的旋转,这些位置会一起转,而修正系数是由环的指令决定的。另两种情况也类似。这样,环上的指令旋转的时候,我们可以先大刀阔斧地给所有贡献取相反数,再修正特定位置贡献。也就是说,我们封装了基环树的基环。## Part 06 汇总把基环树拆成基环和子树,$\Theta(n)$。4 行未改动把环的指令转 3 圈,$\Theta(n)$。总计,$\Theta(n)$。## Part 07 [代码](/article/oo5e6fls)&[删除注释版代码](/article/2tik3s5a)以防我自己看不懂,注释很详细的解释清楚了每个参数是怎么得出的。