Topological sorting, tree's diameter template (cf14d enum-delete edge)

Source: Internet
Author: User

The diameter of the HDU4607 tree

#include <stdio.h>#include <string.h>#include <iostream>#include <queue>#include <vector>using namespace STD;#define N 100005#define INF 1<<30intN,dis[n],e;BOOLVis[n]; vector<int>G[n];//Note that the marking is starting at 0 or 1intBFS (intx) {intI for(i=0; i<=n;i++) Dis[i]=inf;memset(Vis,0,sizeof(VIS)); Queue<int>Q while(!q.empty ()) Q.pop ();    Q.push (x); dis[x]=0; while(!q.empty ()) {intU=q.front (); Q.pop (); vis[u]=1; for(i=0; I<g[u].size (); i++) {intV=g[u][i];if(dis[v]>dis[u]+1) {dis[v]=dis[u]+1; E=v;if(!vis[v]) {vis[v]=1; Q.push (v); }            }        }    }returnE;}intMain () {intT,i,que;scanf("%d", &t); while(t--) {scanf("%d%d", &n,&que); for(i=0; i<=n;i++) g[i].clear (); for(i=1; i<n;i++) {intb;scanf("%d%d", &a,&b);            G[a].push_back (b);        G[b].push_back (a); } E=BFS (1); E=BFS (E); while(que--) {intUscanf("%d", &u);if(u<=dis[e]+1)printf("%d\n", U-1);Else printf("%d\n", dis[e]+ (U-dis[e]+1))*2);//If the crossing node is greater than the point on the tree diameter, go to the loop (obviously the loop is the part that walks the branch, RT) diameter + loop}    }return 0;}

Codeforces 14D diameter of Paths tree

Test instructions: Given a tree
Find 2-point non-repeating path, so that the length of the two paths is the largest product
Ideas:
1, in order to ensure that the point does not repeat, in the picture to delete an edge, enumerate the deletion of the edge
2, so get 2 trees, in their own tree to find the longest chain, that is, the diameter of the tree, and then multiply it

#include <stdio.h>#include <iostream>#include <string.h>#include <set>#include <vector>#include <map>#include <math.h>#include <queue>#include <string>#include <stdlib.h>#include <algorithm>using namespace STD;#define NstructEdge {intFrom, to, NEX;BOOLhehe;} edge[n<<1];intHead[n], Edgenum;voidAddintUintV) {Edge e={u,v,head[u],true};    Edge[edgenum] = E; Head[u] = Edgenum + +;}intNintDis[n];voidInit () {memset(Head,-1,sizeofHead); Edgenum =0; }intBFsintx) {memset(Dis,0,sizeofDIS); DIS[X] =1; Queue<int>Q Q.push (x);inthehe = x; while(!q.empty ()) {intU = Q.front (); Q.pop (); for(inti = Head[u]; ~i; i = Edge[i].nex) {if(!edge[i].hehe)Continue;intv = edge[i].to;if(Dis[v])Continue; DIS[V] = dis[u]+1, Q.push (v);if(Dis[hehe]<dis[v]) hehe = v; }    }returnhehe;}intMain () {intI, u, v; while(~scanf("%d", &n)) {init (); for(i=1; i<n;i++) {scanf("%d%d", &u,&v); Add (U,V);        Add (V,u); }intAns =0; for(i =0; i < Edgenum; i+=2) {Edge[i].hehe = edge[i^1].hehe =false; u = edge[i].from; v = edge[i].to;intL1 = BFS (u);intR1 = BFS (L1);intMUL1 = Dis[r1]-1;intL2 = BFS (v);intR2 = BFS (L2);intMUL2 = Dis[r2]-1;            ans = max (ans, mul1 * mul2); Edge[i].hehe = edge[i^1].hehe =true; }printf("%d\n", ans); }return 0;}

Topological sorting

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <queue>using namespace STD;Const intmaxn= -;intEXGCD (intAintBint&x,int&y) {if(b==0) {x =1; y =0;returnA }intR = EXGCD (b,a%b,x,y);intt = x;    x = y; y = t-a/b*y;returnR;}intHEAD[MAXN],IP,INDEGREE[MAXN];intN,M,SEQ[MAXN];structnote{intV,next;} EDGE[MAXN*MAXN];voidInit () {memset(head,-1,sizeof(head)); ip=0;}voidAddedge (intUintV) {edge[ip].v=v,edge[ip].next=head[u],head[u]=ip++;}intTopo ()/// topology, template can be made{ Queue<int>QintINDEG[MAXN]; for(intI=0; i<n; i++) {Indeg[i]=indegree[i];if(indeg[i]==0) Q.push (i); }intk=0;BOOLres=false; while(!q.empty ()) {if(Q.size ()! =1) res=true;intU=q.front ();        Q.pop (); Seq[k++]=u; for(intI=head[u]; i!=-1; I=edge[i].next) {intV=EDGE[I].V; indeg[v]--;if(indeg[v]==0) Q.push (v); }    }if(k<n)return-1;///There is a forward loop, in short, topological ordering is not possible    if(RES)return 0;//// can be topological ordered, and only one way, seq array is a well-ordered sequence    return 1;/// can be topological ordered, there are many cases, SEQ array is one of the sequence}intMain () {intx, y;intR = EXGCD ( at,5, x, y); R = EXGCD ( at,5, x, y);printf(" %d%d%d", r,x,y);return 0;}

Topological sorting, tree's diameter template (cf14d enum-delete edge)

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.