/* * @Author: black_trees <black_trees@foxmail.com> * @Date: 2022-03-08 08:30:06 * @Last Modified by: black_trees <black_trees@foxmail.com> * @Last Modified time: 2022-03-08 09:12:38 *//* * READ THIS BEFORE YOU WRITE YOUR CODE * ==================================== * @ Read the description carefully! * @ Pay attention to the data domain. * @ Keep calm. * @ Write down your thought on your draft. * @ Keep your mind clear. * @ Stabilize the coding speed. * ==================================== * READ THIS AGAIN BEFORE YOU START !!! */#include<bits/stdc++.h>usingnamespacestd;usingldb=longdouble;constexprintsi_n=1e3+10;constexprintsi_m=5e3+10;constexprldbeps=1e-4;intL,P,tot=0;intf[si_n],t[si_m];doubledis[si_n];intcnt[si_n];boolvis[si_n];std::queue<int>q;structEdge{inthead,Next,ver,w;}e[si_m];inlinevoidadd(intu,intv,intw){e[++tot].ver=v,e[tot].Next=e[u].head,e[u].head=tot,e[tot].w=w;}inlineboolspfa(ldbmid){for(registerinti=1;i<=L;++i)cnt[i]=dis[i]=0,q.push(i),vis[i]=true;while(!q.empty()){intu=q.front();q.pop();vis[u]=false;for(registerinti=e[u].head;i;i=e[i].Next){intv=e[i].ver;ldbw=e[i].w*1.0*mid-1.0*f[u];if(dis[v]>dis[u]+w){dis[v]=dis[u]+w,cnt[v]=cnt[u]+1;if(cnt[u]>=L)returntrue;if(!vis[v])q.push(v),vis[v]=true;}}}returnfalse;}intmain(){cin>>L>>P;for(registerinti=1;i<=L;++i)cin>>f[i];for(registerinti=1,u,v;i<=P;++i)cin>>u>>v>>t[i],add(u,v,t[i]);ldbl=0.0,r=1000.0;while(r-l>eps){ldbmid=(l+r)/2;if(spfa(mid))l=mid;elser=mid;}returnprintf("%.2Lf\n",r),0;}
/* * @Author: black_trees <black_trees@foxmail.com> * @Date: 2022-03-08 09:30:27 * @Last Modified by: black_trees <black_trees@foxmail.com> * @Last Modified time: 2022-03-08 10:34:42 *//* * READ THIS BEFORE YOU WRITE YOUR CODE * ==================================== * @ Read the description carefully! * @ Pay attention to the data domain. * @ Keep calm. * @ Write down your thought on your draft. * @ Keep your mind clear. * @ Stabilize the coding speed. * ==================================== * READ THIS AGAIN BEFORE YOU START !!! */#include<bits/stdc++.h>usingnamespacestd;usingldb=longdouble;constexprintsi_n=7e2+10;constexprintsi_m=1e5+10;constexprinteps=1e-4;intn,tot=0,cnt=0,num=0;std::map<pair<char,char>,int>rec;inlinevoidadd(charc,charcc){if(rec.find({c,cc})==rec.end())rec[{c,cc}]=++cnt;}structEdge{inthead,ver,Next,w;inlinevoidInit(){head=-1;}}e[si_m];inlinevoidadd(intu,intv,intw){e[++tot].ver=v,e[tot].Next=e[u].head,e[u].head=tot,e[tot].w=w;}ldbdis[si_n];boolvis[si_n];intCnt[si_n];std::queue<int>q;inlineboolspfa(ldbmid){for(registerinti=1;i<=cnt;++i)vis[i]=true,q.push(i),Cnt[i]=0,dis[i]=0;while(!q.empty()){intu=q.front();q.pop();vis[u]=false;for(registerinti=e[u].head;i;i=e[i].Next){intv=e[i].ver;ldbw=e[i].w*1.0-mid;if(dis[v]<dis[u]+w){dis[v]=dis[u]+w,Cnt[v]=Cnt[u]+1;if(++num>10000)returntrue;if(Cnt[v]>=cnt)returntrue;if(!vis[v])q.push(v),vis[v]=true;}}}returnfalse;}intmain(){while(~scanf("%d",&n)&&n){e->Init();num=0,tot=0,cnt=0;rec.clear();for(registerinti=1;i<=n;++i){strings;cin>>s;add(s[0],s[1]),add(s[s.size()-2],s[s.size()-1]);intu=rec[{s[0],s[1]}],v=rec[{s[s.size()-2],s[s.size()-1]}];add(u,v,s.size());}ldbl=0.0,r=1000.0;if(!spfa(0)){puts("No solution");continue;}while(r-l>eps){ldbmid=(l+r)/2;if(spfa(mid))l=mid;elser=mid;}printf("%.2Lf\n",r);}return0;}// 不知道是不是 WA 的,不管了
/* * @Author: black_trees <black_trees@foxmail.com> * @Date: 2022-03-17 15:06:41 * @Last Modified by: black_trees <black_trees@foxmail.com> * @Last Modified time: 2022-03-17 20:26:14 *//* * READ THIS BEFORE YOU WRITE YOUR CODE * ==================================== * @ Read the description carefully! * @ Pay attention to the data domain. * @ Keep calm. * @ Write down your thought on your draft. * @ Keep your mind clear. * @ Stabilize the coding speed. * ==================================== * READ THIS AGAIN BEFORE YOU START !!! */#include<queue>#include<cstring>#include<cstdio>#include<iostream>#include<algorithm>usingnamespacestd;constexprintsi_n=1e5+10;constexprintsi_m=2e5+10;intn,K,tot=0;inthead[si_n];structEdge{intver,Next,w;}e[si_m];inlinevoidadd(intu,intv,intw){e[tot]=(Edge){v,head[u],w},head[u]=tot++;}structNode{intnu,ans;};std::queue<Node>q;boolvis[si_n];intpos=0,qos=0,ans=-1;inlinevoidBfs(intst,int&qwq){memset(vis,false,sizeofvis),vis[st]=true,q.push((Node){st,0});while(!q.empty()){Nodeu=q.front();q.pop();if(ans<u.ans)qwq=u.nu,ans=u.ans;for(registerinti=head[u.nu];~i;i=e[i].Next){intv=e[i].ver,w=e[i].w;if(!vis[v])q.push((Node){v,ans+w}),vis[v]=true;;}}return;}intdis[si_n];inlinevoiddfs(intu,intfa){for(registerinti=head[u];~i;i=e[i].Next){intv=e[i].ver,w=e[i].w;if(v==fa)continue;dfs(v,u);ans=max(ans,dis[u]+dis[v]+w);dis[u]=max(dis[u],dis[v]+w);}return;}inlineboolRev(intu,intfa){if(u==pos||u==qos)returntrue;intok=0;for(registerinti=head[u];~i;i=e[i].Next){intv=e[i].ver,&w=e[i].w;if(v==fa)continue;if(Rev(v,u))ok++,e[i].w=e[i^1].w=-1;}returnok==1?true:false;}intmain(){memset(head,-1,sizeofhead),memset(dis,0,sizeofdis);cin>>n>>K;intres=(n<<1);for(registerinti=1;i<n;++i){intu,v;cin>>u>>v;add(u,v,1),add(v,u,1);}// for(register int i=1;i<=n;++i){// for(register int j=head[i];~j;j=e[j].Next){// cout<<i<<" "<<e[j].ver<<" "<<e[j].w<<endl;// }// }Bfs(1,pos);ans=-1;Bfs(pos,qos);res-=ans;// cout<<ans<<endl;// cout<<res+ans<<endl;// cout<<pos<<" "<<qos<<endl;if(K==1){res--;returncout<<res<<endl,0;}Rev(1,0);// for(register int i=1;i<=n;++i){// for(register int j=head[i];~j;j=e[j].Next){// cout<<i<<" "<<e[j].ver<<" "<<e[j].w<<endl;// }// }// cout<<ans<<endl;ans=-1;dfs(1,0);// for(register int i=1;i<=n;++i) cout<<dis[i]<<" "; cout<<endl;// cout<<ans<<endl;res-=ans;returncout<<res<<endl,0;}// If there exist negtive edge(s) on Tree, don't use 2-BFS to calc diameter.// should use dp.
/* * READ THIS BEFORE YOU WRITE YOUR CODE * ==================================== * @ Read the description carefully! * @ Pay attention to the data domain. * @ Keep calm. * @ Write down your thought on your draft. * @ Keep your mind clear. * @ Stabilize the coding speed. * ==================================== * Common Bugs * ==================================== * @ Unuse some written function (forget write dfs(v) in dfs(u)) ? * @ long long or precision ERROR ? * @ Output Format (%lld,%llu) ? * @ Special cases (n=1),(root is not 1) ? * @ Clear the array (head,vis) ? * @ Wrong variable name (i, but written j) ? * ==================================== * READ THIS AGAIN BEFORE YOU START !!! */#pragma GCC optimize("Ofast", "inline", "-ffast-math")#pragma GCC target("avx,sse2,sse3,sse4,mmx")#include<queue>#include<vector>#include<cstring>#include<iostream>usingnamespacestd;constexprintsi=3e5+10;intn,m;inttot=0,head[si];structEdge{intver,Next;}e[si<<1];inlinevoidadd(intu,intv){e[tot]=(Edge){v,head[u]},head[u]=tot++;}std::queue<int>q;std::vector<int>a1[si],b1[si],a2[si],b2[si];intc1[si<<1],c2[si<<1],ans[si];intw[si],v[si];intf[si][20],dep[si];inlinevoidBfs(){q.push(1),dep[1]=1;while(!q.empty()){intu=q.front();q.pop();for(registerinti=head[u];~i;i=e[i].Next){intv=e[i].ver;if(dep[v])continue;dep[v]=dep[u]+1,f[v][0]=u;for(registerintj=1;j<=19;++j)f[v][j]=f[f[v][j-1]][j-1];q.push(v);}}}inlineintLca(intu,intv){if(dep[u]<dep[v])swap(u,v);for(registerinti=19;i>=0;--i){if(dep[f[u][i]]>=dep[v])u=f[u][i];}if(u==v)returnu;for(registerinti=19;i>=0;--i){if(f[u][i]!=f[v][i])u=f[u][i],v=f[v][i];}returnf[u][0];}inlinevoiddfs(intx){intval1=c1[dep[x]+w[x]],val2=c2[w[x]-dep[x]+n];v[x]=1;for(registerinti=head[x];~i;i=e[i].Next){inty=e[i].ver;if(v[y])continue;dfs(y);}for(registerinti=0;i<a1[x].size();i++)c1[a1[x][i]]++;for(registerinti=0;i<b1[x].size();i++)c1[b1[x][i]]--;for(registerinti=0;i<a2[x].size();i++)c2[a2[x][i]+n]++;for(registerinti=0;i<b2[x].size();i++)c2[b2[x][i]+n]--;ans[x]+=c1[dep[x]+w[x]]-val1+c2[w[x]-dep[x]+n]-val2;}intmain(){cin>>n>>m,memset(head,-1,sizeofhead);for(registerinti=1;i<n;++i){intx,y;cin>>x>>y;add(x,y),add(y,x);}for(registerinti=1;i<=n;++i)cin>>w[i];Bfs();for(registerinti=1;i<=m;++i){intx,y;cin>>x>>y;intz=Lca(x,y);a1[x].push_back(dep[x]),b1[f[z][0]].push_back(dep[x]);a2[y].push_back(dep[x]-2*dep[z]),b2[z].push_back(dep[x]-2*dep[z]);}dfs(1);for(registerinti=1;i<=n;++i)cout<<ans[i]<<" ";cout<<endl;return0;}