"BZOJ3732" network,noip2013 lorry transport, YGYLCA

Source: Internet
Author: User

It was exactly the same as Noip's, and I rewrote the code a bit more clearly.

Ideas See http://blog.csdn.net/vmurder/article/details/38734663

But I still have to say it again.

First of all, we build the minimum tree, this is not proof, because in accordance with the Kruskal map, each traversed an edge, it is equivalent to some of the inquiry between the road, and must be the edge.

And then it's YGYLCA. Thought: The thing to be disposed of to the node, in a certain order in the node processing, and after processing to throw into the LCA, and then because to the LCA, some need to process the information has been processed, so you can deal with information like processing.

For example, the subject is two to the longest side of the LCA path in larger values.

The time complexity of this tree-like problem is excellent! And the idea is far more clear than the tree, relatively good writing, but only offline, even if done online with some time stamp-related methods will be converted online to do offline.


The middle of the tle is another thought that is not thought of, and may be optimized for readers to look at.

Look directly at the code, and then hand simulation is good.

#include <cstdio> #include <cstring> #include <algorithm> #define N 101000using namespace Std;struct Ksd{int U,v,next,note,len;bool operator < (const KSD &a) Const{return Len<a.len;}} E[n],eq[n],elca[n],road[n];int head[n],headq[n],headlca[n];int cnt,cntq,cntlca;void Add (int u,int v,int len) {++cnt;e [Cnt].u=u;e[cnt].v=v;e[cnt].len=len;e[cnt].next=head[u];head[u]=cnt;} void addq (int u,int v) {++CNTQ;EQ[CNTQ].U=U;EQ[CNTQ].V=V;EQ[CNTQ].NEXT=HEADQ[U];HEADQ[U]=CNTQ;} void Addlca (int u,int v,int lca,int Note) {++cntlca;elca[cntlca].u=u;elca[cntlca].v=v;elca[cntlca].next=headlca[lca] ; Elca[cntlca].note=note;headlca[lca]=cntlca;} int N,m,p;int f[n],l[n],visit[n],vis[n],ans[n];int Find (int x) {int t=f[x];if (f[x]==x) return X;f[x]=find (F[x]); l[x]= Max (L[x],l[t]); return f[x];} void Ygylca (int x,int p,int len) {int i,u,v,fa,fb;for (i=head[x];i;i=e[i].next) {v=e[i].v;if (v==p) Continue;ygylca (V,x, E[i].len);} for (I=headq[x];i;i=eq[i].next) {v=eq[i].v;if (Visit[v]) Addlca (X,v,find (v), i+1>>1);} for (I=headlca[x];i;i=elca[i].next) {u=elca[i].u;v=elca[i].v;find (U); find (v); Ans[elca[i].note]=max (L[u],l[v]);} Visit[x]=vis[x]=1;l[x]=len;f[x]=p;} void Kruskal () {int i,j,k;int fa,fb;for (i=1;i<=n;i++) f[i]=i;for (i=1;i<=m;i++) scanf ("%d%d%d", &AMP;ROAD[I].U, &road[i].v,&road[i].len); sort (road+1,road+m+1); for (i=1;i<=m;i++) {fa=find (road[i].u); Fb=find (Road[i] . v); if (FA!=FB) {f[fa]=fb;add (Road[i].u,road[i].v,road[i].len); add (Road[i].v,road[i].u,road[i].len);}}} int main () {//freopen ("test.in", "R", stdin), int i,j,k;int a,b,c;scanf ("%d%d%d", &n,&m,&p); Kruskal (); Memset (ans,-1,sizeof (ans)); for (i=1;i<=n;i++) f[i]=i;for (i=1;i<=p;i++) {scanf ("%d%d",&a,& b); Addq (A, b); ADDQ (b,a);} for (i=1;i<=n;i++) {if (!vis[i]) {YGYLCA (i,0,0); memset (visit,0,sizeof (visit));}} for (i=1;i<=p;i++) printf ("%d\n", Ans[i]); return 0;}

Tle approach (or can be optimized)

#include <cstdio> #include <cstring> #include <algorithm> #define N 20000#define M 40000#define Q    20000using namespace std;struct ksd{int u,v,len; BOOL operator < (const KSD &a) const {return len<a.len;}} e[m];struct query{int u,v,ans,note;}    Query[q],stk[2][q];int n,m,q,top[2];int f[n];int Find (int x) {if (x==f[x]) return x; Return F[x]=find (F[x]);}    int main () {//Freopen ("Test.in", "R", stdin);    int i,j,k;    int Flag,len,now;    int u,v;    scanf ("%d%d%d", &n,&m,&q);    for (i=1;i<=n;i++) f[i]=i;    for (i=1;i<=m;i++) scanf ("%d%d%d", &e[i].u,&e[i].v,&e[i].len);    for (i=1;i<=q;i++) scanf ("%d%d", &query[i].u,&query[i].v), query[i].note=i;    for (i=1;i<=q;i++) stk[0][i]=query[i];    Top[0]=q;sort (e+1,e+m+1);        for (Now=0,flag=1;top[now]&&flag<=m;top[now]=0,now^=1) {for (len=e[flag].len;e[flag].len==len;flag++)        {F[find (E[FLAG].V)]=find (E[FLAG].U); } for (i=1;i< =top[now];i++) {u=stk[now][i].u;            V=STK[NOW][I].V;            if (Find (U) ==find (v)) Query[stk[now][i].note].ans=len;        else Stk[now^1][++top[now^1]]=stk[now][i];    }} for (i=1;i<=q;i++) printf ("%d\n", Query[i].ans); return 0;}


Copy to Google TranslateTranslation Results

"BZOJ3732" network,noip2013 lorry transport, YGYLCA

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.