[NOIP2013]货车运输
Posted QYP_2002
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[NOIP2013]货车运输相关的知识,希望对你有一定的参考价值。
【题目描述】
思路{直接最大生成树+树链剖分即可。}
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<cmath> 6 #include<queue> 7 #include<vector> 8 #include<set> 9 #include<map> 10 #define LL long long 11 #define dd double 12 #define Inf 0x3f3f3f3f 13 #define maxx 10001 14 #define maxm 50001 15 #define rs ((o<<1)|1) 16 #define ls (o<<1) 17 #define mid ((l+r)>>1) 18 #define I(i) que[i] 19 #define RG register 20 using namespace std; 21 struct ee{int nxt,to,c;}e[maxm*2],E[maxm*2];int siz,W[maxx],w[maxx],hson[maxx]; 22 int head[maxx],n,m,fa[maxx],col[maxx],HEAD[maxx],tot,TOT,colorr,f[maxx],top[maxx],sz[maxx],deep[maxx],id[maxx],cn,tree[maxx*4]; 23 int find(int x){if(fa[x]!=x)fa[x]=find(fa[x]);return fa[x];} 24 int que[maxm*2];struct bi{int u,v,c;}b[maxm];vector<int>ha[maxx]; 25 void ADD(int u,int v,int c){E[TOT].nxt=HEAD[u];E[TOT].to=v;E[TOT].c=c;HEAD[u]=TOT++;} 26 void add(int u,int v,int c){e[tot].nxt=head[u];e[tot].to=v;e[tot].c=c;head[u]=tot++;} 27 inline int read(){ 28 int res=0,fh=1;char ch=getchar(); 29 while((ch<‘0‘||ch>‘9‘)&&ch!=‘-‘)ch=getchar(); 30 if(ch==‘-‘)fh=-1,ch=getchar(); 31 while(ch<=‘9‘&&ch>=‘0‘)res=res*10+ch-‘0‘,ch=getchar(); 32 return res*fh; 33 } 34 void DFS(int u,int faa){ 35 for(int i=0;i<ha[u].size();++i)que[++que[0]]=ha[u][i]; 36 for(int i=HEAD[u];i!=-1;i=E[i].nxt)if(!col[E[i].to]){ 37 siz++;int v=E[i].to;col[v]=col[u];DFS(v,u); 38 } 39 } 40 bool comp(const int & aa,const int & bb){return b[aa].c>b[bb].c;} 41 void dfs(int u,int faa){ 42 f[u]=faa;sz[u]=1;deep[u]=deep[faa]+1; 43 for(int i=head[u];i!=-1;i=e[i].nxt)if(!sz[e[i].to]){ 44 int v=e[i].to;W[v]=e[i].c;dfs(v,u); 45 sz[u]+=sz[v];if(sz[hson[u]]<sz[v])hson[u]=v; 46 } 47 } 48 void dfs2(int u,int toop){top[u]=toop; 49 id[u]=++cn;w[cn]=W[u];if(hson[u])dfs2(hson[u],toop); 50 for(int i=head[u];i!=-1;i=e[i].nxt)if(e[i].to!=f[u]&&e[i].to!=hson[u])dfs2(e[i].to,e[i].to); 51 } 52 void kurusal(int u){siz=1;memset(que,0,sizeof(que)); 53 col[u]=++colorr;DFS(u,u);sort(que+1,que+que[0]+1,comp);int cnt=0; 54 for(int i=1;i<=que[0]&&cnt+1!=siz;++i) 55 if(fa[find(b[I(i)].u)]!=fa[find(b[que[i]].v)]) 56 fa[find(b[I(i)].u)]=fa[find(b[I(i)].v)], 57 add(b[I(i)].u,b[I(i)].v,b[I(i)].c),add(b[I(i)].v,b[I(i)].u,b[I(i)].c); 58 dfs(u,u);dfs2(u,u); 59 } 60 void build(int o,int l,int r){ 61 if(l==r){tree[o]=w[l];return;} 62 build(ls,l,mid),build(rs,mid+1,r); 63 tree[o]=min(tree[rs],tree[ls]); 64 } 65 int query(int o,int l,int r,int L,int R){ 66 if(L<=l&&r<=R)return tree[o]; 67 if(mid<L)return query(rs,mid+1,r,L,R); 68 else if(mid>=R)return query(ls,l,mid,L,R); 69 else return min(query(rs,mid+1,r,L,R),query(ls,l,mid,L,R)); 70 } 71 void lca(int x,int y){ 72 int ans=Inf; 73 while(top[x]!=top[y]){ 74 if(deep[top[x]]<deep[top[y]])swap(x,y); 75 ans=min(ans,query(1,1,n,id[top[x]],id[x])); 76 x=f[top[x]]; 77 } 78 if(deep[x]<deep[y])swap(x,y); 79 if(id[y]<id[x])ans=min(ans,query(1,1,n,id[y]+1,id[x])); 80 printf("%d\n",ans); 81 } 82 int main(){ 83 memset(HEAD,-1,sizeof(HEAD)); 84 memset(head,-1,sizeof(head));scanf("%d%d",&n,&m); 85 for(RG int i=1;i<=n;++i)fa[i]=i; 86 for(int i=1;i<=m;++i)b[i].u=read(),b[i].v=read(),b[i].c=read(),ha[b[i].u].push_back(i),ADD(b[i].u,b[i].v,b[i].c),ADD(b[i].v,b[i].u,b[i].c); 87 for(int i=1;i<=n;++i)if(!col[i])kurusal(i);int kk;scanf("%d",&kk);build(1,1,n); 88 for(int i=1;i<=kk;++i){ 89 int x,y;scanf("%d%d",&x,&y); 90 if(col[x]!=col[y]){cout<<"-1\n";continue;} 91 else lca(x,y); 92 } 93 return 0; 94 }
以上是关于[NOIP2013]货车运输的主要内容,如果未能解决你的问题,请参考以下文章