Example #1
0
int main(int argc, char* argv[])
{
	int t;
	scanf("%d",&t);
	while(t--){
		init();
		lca(ro);
		print();
	}
	return 0;
}
Example #2
0
int Hash(int u, int v)
{
	int pp = lca(u, v), distu = dep[u] - dep[pp], distv = dep[v] - dep[pp];
	int ret1 = h1[u] - (LL)pw1[distu + 1] * h1[fa[pp][0]] % mod;
	int ret2 = h2[v] - (LL)pw2[distv] * h2[pp] % mod;
	if(ret1 < 0)
		ret1 += mod;
	if(ret2 < 0)
		ret2 += mod;
	return (ret1 + (LL)pw1[distu] * pw1[distv] % mod * ret2) % mod;
}
Example #3
0
int main()
{
    int cas=0,u,v;
    int S,T;

    freopen("data.in","r",stdin);
    freopen("data.out","w",stdout);
    Bei=18;
    while(scanf("%d%d",&n,&m)==2)
    {
        //scanf("%d%d",&S,&T);
        cnt=0;
        memset(box,-1,sizeof(box));
        while(m--)
        {
            scanf("%d%d",&u,&v);
            add(u,v);
            add(v,u);
        }
        precut();
        pretree();
        prebridge();
        prelca();
        //debug();

        printf("Case #%d:\n",++cas);
        int q;
        u=S;v=T;
        scanf("%d",&q);
        while(q--)
        {
            scanf("%d%d",&u,&v);
            int ans;
            if (u==v) ans=n-1;
            else
            {
                u=st[u];v=st[v];
                int t=lca(u,v);
               // printf("u=%d v=%d t=%d\n",u,v,t);
                if (t==0) ans=n;
                else
                {
                    ans=sum[u]+sum[v]-2*sum[t]+we[t];
                    if (we[u]==-1) ans++;
                    if (we[v]==-1) ans++;
                    ans=n-ans;
                }
            }
            printf("%d\n",ans);
        }
        puts("");
    }
    return 0;
}
Example #4
0
/**
 * Suffix link for internal nodes
 */
ulong SSTree::sl(ulong v)
{
    if (v == 0 || v == root() || isleaf(v))
        return 0;

    ulong x = brLeaf->rank(v - 1) + 1;
    ulong y = brLeaf->rank(Pr->findclose(v));
    x = sa->Psi(x - 1);
    y = sa->Psi(y - 1);
    return lca(brLeaf->select(x + 1), brLeaf->select(y + 1));
}
Example #5
0
File: bst_lca.c Project: pharic/c
int main() {
	struct bst *root = NULL;
	struct bst *node;
	root = insert (root, 6);
	insert (root, 4);
	insert (root, 8);
	insert (root, 3);
	insert (root, 5);
	node = lca (root,3,5);
	printf ("LCA of 3 and 5 is: %d\n", node->data);
	return 0;
}
Example #6
0
/**
 * Suffix link for internal nodes
 */
ulong SSTree::sl(ulong v) {

    if (v == 0 || v == root() || isleaf(v)) {
        return 0;
    }

    ulong x = _brLeaf->rank(v - 1) + 1;
    ulong y = _brLeaf->rank(_Pr->findclose(v));
    x = _sa->Psi(x - 1);
    y = _sa->Psi(y - 1);
    return lca(_brLeaf->select(x + 1), _brLeaf->select(y + 1));
}
Example #7
0
static int uwsgi_api_log(lua_State *L) {
	
	const char *logline ;

	lca(L, 1);

	if (lua_isstring(L, 1)) {
		logline = lua_tolstring(L, 1, NULL);
                uwsgi_log( "%s\n", logline);
	}

	return 0;
}
Example #8
0
void lca(int v){
	int p;
	root[v]=v;
	for(p=head[v];p>=0;p=next[p]){
		lca(data[p]);
		root[data[p]]=v;
	}
	flag[v]=true;
	for(p=q[v];p>=0;p=next[p])
		if(flag[data[p]]){
			ans[find(data[p])]++;
			int j=p;
		}
}
Example #9
0
vector < pair <int,int> > get_path(int v, int u)
{
	int w=lca(v, u);
	auto ret=path_up(v, w);
	auto pom=path_up(u, w);
	for (auto &i : ret)
		swap(i.first, i.second);
	while(!pom.empty())
	{
		ret.push_back(pom.back());
		pom.pop_back();
	}
	return ret;
}
int main(){
	// Let us construct the BST shown in the above figure
    node *root        = newNode(20);
    root->left               = newNode(8);
    root->right              = newNode(22);
    root->left->left         = newNode(4);
    root->left->right        = newNode(12);
    root->left->right->left  = newNode(10);
    root->left->right->right = newNode(14);
 
    int n1 = 10, n2 = 14;
    struct node *t = lca(root, n1, n2);
    printf("LCA of %d and %d is %d \n", n1, n2, t->data);
 
    n1 = 14, n2 = 8;
    t = lca(root, n1, n2);
    printf("LCA of %d and %d is %d \n", n1, n2, t->data);
 
    n1 = 10, n2 = 22;
    t = lca(root, n1, n2);
    printf("LCA of %d and %d is %d \n", n1, n2, t->data);
	return 0;
}
int main(){
    while(scanf("%d", &n) > 0){
        for(int i = 1; i <= n; i++)
            scanf("%d", &parent[i]);
        for(int i = 1; i <= n; i++)
            if(parent[i] == -1)
                height[i] = 0, dfs(i);
        printf("Case %d:", ++ct);
        while(scanf("%d%d", &a, &b), a+b)
            printf(" %d", height[a] + height[b] - 2*height[lca(a, b)]);
        putchar(10);
    }
    return 0;
}
Example #12
0
struct node* lca(struct node *root, int n1, int n2){
	if(root == NULL)
		return NULL;

	/*
	If the node's data is greater than n1 and n2, then it means
	the LCA for n1 and n2 must be in node's left subtree.
	*/
	if((root->data > n1) && (root->data > n2))
		return lca(root->left, n1, n2);
	
	/*
	If the node's data is less than n1 and n2, then it means the
	LCA for n1 and n2 must be in the right subtree of node.
	*/
	if((root->data < n1) && (root->data < n2))
		return lca(root->right, n1, n2);

	/*
	The point at which one of the node is less than the current node and the other
	node is greater than the current node is the LCA of the two nodes.
	*/
	return root;
}
Example #13
0
int main()
{
    tree_t *t=newNode(1);
    t->left=newNode(2);
    t->left->left=newNode(4);
    t->left->left->left=newNode(8);
    t->left->left->right=newNode(10);
    t->left->right=newNode(5);
    t->left->right->right=newNode(9);
    t->right=newNode(3);
    t->right->left=newNode(6);
    t->right->right=newNode(7);
    int arr[30],index=0;
    lca(t,arr,8,6,&index);
}
int main()
{
    int i,u,v,len,m,k;
    while(scanf("%d%d%d",&n,&m,&k)!=EOF)
    {
        chu();
        for(i=0;i<m;i++)
        {
            scanf("%d%d%d",&u,&v,&len);
            run.dis=len;
            run.v=v;
            q[u].push_back(run);
            run.v=u;
            q[v].push_back(run);            
        }
        for(i=0;i<k;i++)
        {
            scanf("%d%d",&u,&v);
            ruq.id=i;
            ruq.v=v;
            que[u].push_back(ruq);
            ruq.v=u;
            que[v].push_back(ruq);    
        }
        ji=0;
        for(i=1;i<=n;i++)
        {
            if(0 == hash[i])
            {
                ji++;
                lca(i,0);    
            }
        }
        for(i=0;i<k;i++)
        {
            if(-1 == ans[i])
            {
                printf("Not connected\n");    
            }
            else
            {
                printf("%d\n",ans[i]);            
            }
        }
    }
    return 0;
}
Example #15
0
int reroot(struct rooted_tree *tree, struct llist *outgroup_nodes)
{
	struct rnode *outgroup_root;

	outgroup_root = lca(tree, outgroup_nodes);
	if (NULL == outgroup_root) { perror(NULL); exit(EXIT_FAILURE); }

	if (tree->root == outgroup_root) {
		return LCA_IS_TREE_ROOT;
	}
	if (! reroot_tree (tree, outgroup_root)) {
		perror(NULL);
		exit(EXIT_FAILURE);
	}

	return REROOT_OK;
}
Example #16
0
    void blossom(int u, int v) {
        for(int x=1; x<=n; ++x) inblossom[x] = false;

        newRoot = lca(u, v);
        trace(u); trace(v);

        if (root[u] != newRoot) dad[u] = v;
        if (root[v] != newRoot) dad[v] = u;

        for(int x=1; x<=n; ++x) if (inblossom[root[x]]) {
            root[x] = newRoot;
            if (!inque[x]) {
                inque[x] = true;
                que[qsize++] = x;
            }
        }
    }
Example #17
0
int main(){
    // do you mind telling us in the problem statement
    // that there is more than one test case per input
    while(~scanf("%d%d",&N,&M)){
        V.clear();
        for(int i=1;i<=N;++i){
            scanf("%d",W+i);
            V.emplace_back(W[i]);
        }
        std::sort(all(V));
        V.erase(std::unique(all(V)),V.end());
        for(int i=1;i<=N;++i)
            W[i]=std::lower_bound(all(V),W[i])-V.begin();
        for(int i=1;i<=N;++i)
            adj[i].clear();
        for(int i=1,a,b;i<N;++i){
            scanf("%d%d",&a,&b);
            adj[a].emplace_back(b);
            adj[b].emplace_back(a);
        }
        dfs(1,-1);
        build();
        for(int i=0,a,b,l;i<M;++i){
            scanf("%d%d",&a,&b);
            l=lca(a,b);
            if(ein[b]<ein[a]) std::swap(a,b);
            if(a==l||b==l) qrys[i]={ein[a],ein[b],-1,i};
            else qrys[i]={eout[a],ein[b],l,i};
        }
        std::sort(qrys,qrys+M);
        vis.reset();
        memset(cnt,0,sizeof cnt);
        res=0;
        chk(tour[1]);
        for(int i=0,l=1,r=1;i<M;++i){
            while(r<qrys[i].r) chk(tour[++r]);
            while(l>qrys[i].l) chk(tour[--l]);
            while(r>qrys[i].r) chk(tour[r--]);   
            while(l<qrys[i].l) chk(tour[l++]);
            if(~qrys[i].lca) chk(qrys[i].lca);
            ans[qrys[i].i]=res;
            if(~qrys[i].lca) chk(qrys[i].lca);
        }
        for(int i=0;i<M;printf("%d\n",ans[i++]));
    }
}
Example #18
0
void lca(int k)
{
	if(found) return;
	ancestor[k]=k;
	for(int i=1;i<=degree[k];++i)
	{
		lca(tree[k][i]);
		getunion(k,tree[k][i]);
		ancestor[find(k)]=k;
	}
	vis[k]=true;
	if((k==q1&&vis[q2])||(k==q2&&vis[q1]))
	{
		printf("%d\n",ancestor[find(k==q1?q2:q1)]);
		found=true;
	}
}
// Searches for an augmenting path rooted at r via a breadth first search.
bool BFS(int r) { 
  if (graph[r].size() == 0) return false;

  // Set up union-find (initially, all nodes are their own representatives)
  // Reset lca and d memory, we cut this time from the total recorded.
  time_t t = clock();
  for (int i = 0; i<n; ++i) pp[i] = i;
  memset(v, -1, sizeof(v));
  memset(d, -1, sizeof(d));
  mem_time += clock() - t;

  // Initialize values and push root onto the queue.
  q.resize(0);
  d[r] = 0;
  q.push_front(r);
  while (!q.empty()) {
    // Pop front of queue, take to be the current node, x.
    // Iterate over all neighbours of the current node, y.
    int this_is_dumb = q.front(); q.pop_front();
    for (int x = this_is_dumb, i = 0, y = graph[x][0]; i < (int)graph[x].size(); ++i, y = graph[x][i]) {

      if (m[y] != y && f(x) != f(y)) {  // if neighbour not unmatchable and not in blossom with x:
        if (d[y] == -1) {               // if neighbour not in tree yet:
          if (m[y] == -1) {             // if neighbour not matched:
            path(r, x);                
            m[x] = y;                   
            m[y] = x; 
            return true;                // AUGMENTING PATH FOUND, update m and return

          } else {                      // if neighbour matched:
            p[y] = x;                   // update tree growing structure (m and d)
            p[m[y]] = y;                // and push y's match onto queue.
            d[y] = 1; 
            d[m[y]] = 0; 
            q.push_front(m[y]); 
          }
        } else if (d[f(y)] == 0) {      // if root-distance to neighbour known and even
          int b = lca(x, y, r);         // then we have found a blossom.
          shrink_one_side(x, y, b); shrink_one_side(y, x, b); 
        } 
      }
    }
  }
  return false; 
}
Example #20
0
void solve ()
{
    scanf ("%ld", &n);

    for (logn = 1; (1 << logn) <= n; logn++);
    logn--;
	for (int i = 0; i < n; i++)
		for (int j = 0; j <= logn; j++)
			d[i][j] = -1;

    for (long i = 1, f, t; i < n; i++)
    {
        scanf ("%ld%ld", &f, &t);
        --f;
        --t;
        scanf ("%ld", &g[f][t]);
        g[t][f] = g[f][t];
    }

    dfs(0);
    while (true)
    {
        scanf ("%s", s);
        k = 0;
        if (!strcmp("DONE", s))
            break;
        if (!strcmp("KTH", s))
        {
            scanf ("%ld %ld %ld ", &A, &B, &k);
            printf("%ld\n", kth(A - 1, B - 1, k) + 1);
        }
        else
        {
            scanf ("%ld%ld", &A, &B);
            lca(A - 1,B - 1);
            printf ("%ld\n", res);
        }
    }
    puts("");
    for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			g[i][j] = 0;
	for (int i = 0; i < n; i++)
		dist[i] = u[i] = h[i] = 0;
}
int main(){
    scanf("%d%d",&N,&M);
    for(int i=1;i<=N;++i) lct[i]=new nd(i);
    for(int i=0,a,b;i<M;++i){
        scanf("%s",S);
        if(S[0]=='c'){
            scanf("%d",&a);
            cut(lct[a]);
        }else{
            scanf("%d%d",&a,&b);
            if(S[1]=='c'){
                printf("%d\n",lca(lct[a],lct[b])->val);
            }else{
                lnk(lct[a],lct[b]);
            }
        }
    }
}
Example #22
0
void lca(int u)
{
	int i, j;
	make_set(u);
	ancestor[find_set(u)] = u;
	for(i = 1;i <= n; i++)
	{
		if(tree[u][i] == 1)
		{
			lca(i);
			union_set(u, i);
			ancestor[find_set(u)] = u;
		}
	}
	color[u] = 1;
	for(i = 1;i <= n; i++)
		if(q[u][i] && color[i] == 1) num[ancestor[find_set(i)]]++;
}
Example #23
0
void main()
{
	struct bst *b,*r;
	b = NULL ;
	printf("no f nodes??");
	int n,num,n1,n2;
	scanf("%d",&num);
	while(num--)
		{   scanf("%d",&n);
    	    insert(&b,n);
		}
	inorder(b);
	printf("\nenter nodes for LCA computation\n");
	scanf("%d%d",&n1,&n2);
	r=lca(b,n1,n2);
	printf("\nresult is %d",r->data);
	getch();
}
Example #24
0
void reduce(list<int>* A,list<int>* B,list<int> & Label,int size){
     for (int i=0;i<size;i++){
         B[i].push_back(A[i].front());
         Label.push_back(A[i].front());
         for (int j=0;j<size;j++){
             if (i!=j){
                int d = lca(A[i],A[j]);       
                B[i].push_back(d);
                Label.push_back(d);
             }
         }
     }
     Label.sort();
     Label.unique();
     for (int i=0;i<size;i++){
         B[i].sort();         
         B[i].unique();
     }
}
Example #25
0
int main(void) {
	int _data[] = {1,2,3,4,5,6,7,8,9};
	int * data = _data;

	tree_t * tree = tree_init();
	node_t * node1 = tree_init_node(data+0);
	node_t * node2 = tree_init_node(data+1);
	node_t * node3 = tree_init_node(data+2);
	node_t * node4 = tree_init_node(data+3);
	node_t * node5 = tree_init_node(data+4);
	node_t * node6 = tree_init_node(data+5);
	node_t * node7 = tree_init_node(data+6);
	node_t * node8 = tree_init_node(data+7);
	node_t * node9 = tree_init_node(data+8);

	tree->root = node5;
	node5->left = node2;
	node2->left = node1;
	node2->right = node3;
	node3->right = node4;
	node5->right = node8;
	node8->left = node7;
	node7->left = node6;
	node8->right = node9;

	/* uh what if ... one of the nodes is an ancestor*/
	int tests[6][3] = { {6,9,8}, {1,4,2}, {5,6,5}, {5,5,5}, {1,1,1}, {1,9,5} }; 
	int c;	
	int ret;
	for (int i = 0; i < 6; ++i) { 	
		ret = lca(tree, tests[i][0], tests[i][1], &c) ;
		if ( ret != 0 ) {
			printf("lca(%d,%d) returned %d\n", tests[i][0], tests[i][1], ret);
			return -1;
		} else if ( c != tests[i][2] ) {
			printf("lca(%d,%d) != %d\n",tests[i][0], tests[i][1], tests[i][2]);
			return -1;
		}
	}

	return 0;
}
Example #26
0
int main(){
    TreeNode t0(15);
    TreeNode t1(10);
    TreeNode t2(20);
    TreeNode t3(8);
    TreeNode t4(12);
    TreeNode t5(16);
    TreeNode t6(25);
    TreeNode t7(13);
    TreeNode t8(17);
    t0.left = &t1;
    t0.right = &t2;
    t1.left = &t3;
    t1.right = &t4;
    t2.left = &t5;
    t2.right = &t6;
    t4.right = &t7;
    t5.right = &t8;
    cout<<lca(&t0,&t3,&t7)->val<<endl;
    return 0;
}
Example #27
0
void lca(ll u){
    fa[u]=ma[u]=u;
    for(ll i=g1.head[u];i!=-1;i=g1.e[i].next){
        ll v=g1.e[i].v;
        lca(v);
        yy_union(u,v);
        ma[findfa(v)]=u;
    }
    flag[u]=1;
    for(ll i=g2.head[u];i!=-1;i=g2.e[i].next){
        ll v=g2.e[i].v;
        if(flag[v]){
            ll fa1=findfa(v);
            if(ma[fa1]==u){
              //  printf("%d   %d \n",u,v);
                tnum++;
                tsum+=dis[v]-dis[u];
            }
        }
    }
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
#endif
    scanf("%d",&n);
    {
        memset(fe,-1,sizeof(fe));
        memset(fq,-1,sizeof(fq));
        memset(fnq,-1,sizeof(fnq));
        memset(vis,0,sizeof(vis));
        cnte=cntq=cntnq=0;
        for(int i=1;i<=n;i++)
        {
            int tmp;scanf("%d",&tmp);
            mx[i]=mn[i]=tmp;
            up[i]=dw[i]=0;
        }
        for(int i=1;i<n;i++)
        {
            int x,y;
            scanf("%d %d",&x,&y);
            addedge(x,y);
        }
        scanf("%d",&q);
        for(int i=0;i<q;i++)
        {
            int x,y;
            scanf("%d %d",&x,&y);
            addq(x,y,i);
        }
        lca(1,-1);
        for(int i=0;i<q;i++)
            printf("%d\n",ans[i]);
    }
    return 0;
}
Example #29
0
int main()
{
	char ch;
	int i, j, k, m;
	while(scanf("%d", &n) != EOF)
	{
		init();
		for(k = 0;k < n; k++)
		{
			scanf("%d:(%d)", &i, &m);
			while(m--)
			{
				scanf("%d", &j);
				tree[i][j] = 1;
			}
		}
		scanf("%d", &m);
		for(k = 0;k < m; k++)
		{
			while(getchar() != '(');
			scanf("%d", &i);
			scanf("%d", &j);
			q[i][j] = 1;
			q[j][i] = 1;
			while(getchar() != ')');
		}
		for(i = 1;i <= n; i++)
		{
			k = 0;
			for(j = 1;j <= n; j++) k += tree[j][i];
			if(k == 0) break;
		}
		lca(i);
		for(i = 1;i <= n; i++) if(num[i]) printf("%d:%d\n", i, num[i]);
	}
	return 0;
}
bool flow(int s){
	memset(mtp, -1, sizeof(mtp));
	while(qu.size()) qu.pop();
	qu.push(s);
	mtp[s] = 0; bk[s] = pr[s] = -1;

	while(qu.size() && pr[s] == -1){
		int u = qu.front(); qu.pop();
		for(int v=0; v<V; v++){
			
			if (el[u][v] == 0) continue;
			if (ffa(v) == ffa(u)) continue;

			if(pr[v] == -1){
				do{
					int t = pr[u];
					pr[v] = u; pr[u] = v;
					v = t; u = t==-1?-1:bk[t];
				}while( v != -1 );
				break;
			}else if(mtp[v] == 0){
				int w = lca(u, v);	
				if(ffa(w) != ffa(u)) bk[u] = v;
				if(ffa(w) != ffa(v)) bk[v] = u;
				flower(u, w);
				flower(v, w);
			}else if(mtp[v] != 1){
				bk[v] = u;
				mtp[v] = 1;
				mtp[pr[v]] = 0;
				qu.push(pr[v]);
			}
		}
	}
	return pr[s] != -1;
}