constructive algorithms dp implementation math trees *2400

Please click on ads to support us..

C++ Code:

# include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n; 
vector <int> g[N];
int y, maxl;
int siz[N],dfn[N],dfntot = 0;
int L,R;
bool Ok = 1;
vector <int> S[N];
vector <int> Link;
int p[N];
void dfs(int x,int fa,int dep,bool xg = 0)
{
	if(dep > maxl) y = x, maxl = dep; 
	if(xg) siz[x] = 1,dfn[x] = ++dfntot;
	for(int i = 0; i < (int)g[x].size(); i++)
	{
		int v = g[x][i]; 
		if(v == fa) continue;
		dfs(v,x,dep + 1,xg);
		if(xg) siz[x] += siz[v];
	}
	return;
}
void dfs2(int x,int fa)
{
	if(!Ok) return;
	Link.push_back(x);
	for(int i = 0; i < (int)g[x].size(); i++)
	{
		int v = g[x][i]; if(v == fa) continue;
		if(dfn[v] <= dfn[R] && dfn[R] <= dfn[v] + siz[v] - 1) // R in T_v
		{
			dfs2(v,x);
		}
		else if(siz[v] > 1) 
		{
			Ok = 0; return;
		}
		else S[x].push_back(v);
	}
	return;
}
void solve(void)
{
	int tot = 0;
	int L = (int)Link.size();
	if(L % 2 == 0)
	{
		for(int i = 0; i < L; i += 2)
		{
			p[++tot] = Link[i];
//			if(i == L - 1) break;
			for(int v : S[Link[i + 1]]) p[++tot] = v;
		}
//		p[++tot] = L - 1;
//		for(int v : S[Link[L - 1]]) p[++tot] = v;
		for(int i = L - 1; i >= 0; i -= 2)
		{
			p[++tot] = Link[i];
			for(int v : S[Link[i - 1]]) p[++tot] = v;
		}
	}
	else 
	{
		for(int i = 0; i < L; i += 2)
		{
			p[++tot] = Link[i];
			if(i + 1 < L) 
			{
				for(int v : S[Link[i + 1]]) p[++tot] = v;
			}
		}
		for(int i = L - 2; i >= 0; i -= 2)
		{
			p[++tot] = Link[i];
			if(i - 1 >= 0)
			{
				for(int v : S[Link[i - 1]]) p[++tot] = v;
			}
		}
	}
	return;
}
int main(void)
{
	scanf("%d",&n);
	for(int i = 1; i < n; i++)
	{
		int u,v; scanf("%d%d",&u,&v);
		g[u].push_back(v),g[v].push_back(u);
	}
	y = 0, maxl = 0; 
	dfs(1,0,0); L = y,maxl = 0;
//	printf("y = %d, maxl = %d\n",y,maxl);
	dfs(L,0,0,1); R = y;
//	printf("L = %d,R = %d\n",L,R);
	dfs2(L,0);
//	printf("Link : \n");
//	for(int x : Link) printf("%d ",x);
//	printf("\n");
	if(!Ok) 
	{
		printf("No\n");
		return 0;
	}
	solve();
	printf("Yes\n");
	for(int i = 1; i <= n; i++) printf("%d ",p[i]);
	return 0;
}

   	  			  									 		 	 	 			


Comments

Submit
0 Comments
More Questions

1302. Deepest Leaves Sum
1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST