Submission #40708151


Source Code Expand

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define op(x) ((x&1)?x+1:x-1)
#define odd(x) (x&1)
#define even(x) (!odd(x))
#define lc(x) (x<<1)
#define rc(x) (lc(x)|1)
#define lowbit(x) (x&-x)
#define mp(x,y) make_pair(x,y)
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
using namespace std;
const int MAXN=2e5+10,INF=1e9;
void tomax(int& x,int y){x=max(x,y);}
void tomin(int& x,int y){x=min(x,y);}

int n,f[MAXN][2],ss[MAXN],son;
vector<int>e[MAXN];

typedef array<int,2> info;
info pre[MAXN],suf[MAXN];

void upd(info& x,int v){
	if(v>x[0])x[1]=x[0],x[0]=v;
	else if(v>x[1])x[1]=v;
}

void do_solve(int u,int i){
	rep(j,2,son){
		int len=f[ss[1]][1] + f[ss[j]][1]-1;

		tomax(len,pre[j-1][0] + 1 + pre[j-1][1]);
		tomax(len,suf[j+1][0] + 1 + suf[j+1][1]);
		tomax(len,pre[j-1][0] + 1 + suf[j+1][0]);
		tomax(len,max(f[ss[1]][1],f[ss[j]][1]) + max(pre[j-1][0],suf[j+1][0]));

		if(len<=i){
			int mx=max(f[ss[1]][1],f[ss[j]][1]);
			tomax(mx,pre[j-1][0]+1);tomax(mx,suf[j+1][0]+1);

			tomin(f[u][0],mx);
		}
	}
}

void dfs(int u,int fa,int i){
	son=0;
	for(auto v:e[u])if(v!=fa){
		dfs(v,u,i);
		son++;
	}
	if(!son){
		f[u][0]=f[u][1]=1;
		return;
	}

	son=0;
	for(auto v:e[u])if(v!=fa)ss[++son]=v;

	
	//做两轮冒泡排序
	rep(j,1,min(2,son)){
		per(k,son,j+1){
			if(f[ss[k]][0] > f[ss[k-1]][0])swap(ss[k],ss[k-1]);
		}
	}

	pre[0]=suf[son+1]={0,0};
	rep(j,1,son)pre[j]=pre[j-1],upd(pre[j],f[ss[j]][0]);
	per(j,son,1)suf[j]=suf[j+1],upd(suf[j],f[ss[j]][0]);

	//新颜色
	if(pre[son][0] + pre[son][1] + 1 <= i){
		tomin(f[u][1],pre[son][0]+1);
	}
	//只和一个儿子合并
	rep(j,1,son){
		int len=f[ss[j]][1]+pre[j-1][0];
		tomax(len,f[ss[j]][1]+suf[j+1][0]);
		tomax(len,pre[j-1][0]+pre[j-1][1]+1);
		tomax(len,suf[j+1][0]+suf[j+1][1]+1);
		tomax(len,pre[j-1][0]+suf[j+1][0]+1);

		if(len<=i){
			int mx=f[ss[j]][1];
			tomax(mx,pre[j-1][0]+1);tomax(mx,suf[j+1][0]+1);

			tomin(f[u][1],mx);
		}
	}

	if(son>1){
		//和两个儿子合并,则至少有一个是ss[1]或者ss[2]
		pre[1]={0,0};
		rep(j,2,son)pre[j]=pre[j-1],upd(pre[j],f[ss[j]][0]);
		do_solve(u,i);

		swap(ss[1],ss[2]);
		pre[1]={0,0};
		rep(j,2,son)pre[j]=pre[j-1],upd(pre[j],f[ss[j]][0]);
		do_solve(u,i);
	}

	tomin(f[u][0],f[u][1]);
}

int check(int mid){
	rep(i,1,n){
		f[i][0]=f[i][1]=INF;
	}
	dfs(1,0,mid);
	return f[1][0]!=INF || f[1][1]!=INF;
}

int main(){
	ios::sync_with_stdio(false);

	cin>>n;
	rep(i,1,n-1){
		int u,v;cin>>u>>v;
		e[u].push_back(v);
		e[v].push_back(u);
	}

	int L=1,R=40,res=0;
	while(L<=R){
		int mid=(L+R)>>1;
		if(check(mid)){
			res=mid;R=mid-1;
		}else{
			L=mid+1;
		}
	}
	cout<<res<<endl;
    return 0;
}

Submission Info

Submission Time
Task Ex - Optimal Path Decomposition
User Crying
Language C++ (GCC 9.2.1)
Score 600
Code Size 2855 Byte
Status AC
Exec Time 293 ms
Memory 31052 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 3
AC × 47
Set Name Test Cases
Sample sample00.txt, sample01.txt, sample02.txt
All sample00.txt, sample01.txt, sample02.txt, testcase00.txt, testcase01.txt, testcase02.txt, testcase03.txt, testcase04.txt, testcase05.txt, testcase06.txt, testcase07.txt, testcase08.txt, testcase09.txt, testcase10.txt, testcase11.txt, testcase12.txt, testcase13.txt, testcase14.txt, testcase15.txt, testcase16.txt, testcase17.txt, testcase18.txt, testcase19.txt, testcase20.txt, testcase21.txt, testcase22.txt, testcase23.txt, testcase24.txt, testcase25.txt, testcase26.txt, testcase27.txt, testcase28.txt, testcase29.txt, testcase30.txt, testcase31.txt, testcase32.txt, testcase33.txt, testcase34.txt, testcase35.txt, testcase36.txt, testcase37.txt, testcase38.txt, testcase39.txt, testcase40.txt, testcase41.txt, testcase42.txt, testcase43.txt
Case Name Status Exec Time Memory
sample00.txt AC 14 ms 8280 KB
sample01.txt AC 10 ms 8232 KB
sample02.txt AC 7 ms 8192 KB
testcase00.txt AC 8 ms 8156 KB
testcase01.txt AC 7 ms 8188 KB
testcase02.txt AC 194 ms 28676 KB
testcase03.txt AC 192 ms 31052 KB
testcase04.txt AC 208 ms 27176 KB
testcase05.txt AC 217 ms 28328 KB
testcase06.txt AC 206 ms 28368 KB
testcase07.txt AC 186 ms 28124 KB
testcase08.txt AC 105 ms 19120 KB
testcase09.txt AC 108 ms 19460 KB
testcase10.txt AC 158 ms 17004 KB
testcase11.txt AC 147 ms 16452 KB
testcase12.txt AC 155 ms 15940 KB
testcase13.txt AC 148 ms 15692 KB
testcase14.txt AC 143 ms 15280 KB
testcase15.txt AC 153 ms 15952 KB
testcase16.txt AC 139 ms 15356 KB
testcase17.txt AC 157 ms 15880 KB
testcase18.txt AC 150 ms 15980 KB
testcase19.txt AC 144 ms 15672 KB
testcase20.txt AC 107 ms 16304 KB
testcase21.txt AC 105 ms 15852 KB
testcase22.txt AC 127 ms 16632 KB
testcase23.txt AC 139 ms 16004 KB
testcase24.txt AC 149 ms 15572 KB
testcase25.txt AC 293 ms 15964 KB
testcase26.txt AC 188 ms 16052 KB
testcase27.txt AC 198 ms 15528 KB
testcase28.txt AC 182 ms 15960 KB
testcase29.txt AC 171 ms 15856 KB
testcase30.txt AC 181 ms 15920 KB
testcase31.txt AC 171 ms 16040 KB
testcase32.txt AC 171 ms 15784 KB
testcase33.txt AC 177 ms 16024 KB
testcase34.txt AC 183 ms 16132 KB
testcase35.txt AC 161 ms 15460 KB
testcase36.txt AC 173 ms 15852 KB
testcase37.txt AC 181 ms 16012 KB
testcase38.txt AC 159 ms 15452 KB
testcase39.txt AC 152 ms 15424 KB
testcase40.txt AC 207 ms 15796 KB
testcase41.txt AC 167 ms 15684 KB
testcase42.txt AC 177 ms 16024 KB
testcase43.txt AC 163 ms 15528 KB