Submission #39890084
Source Code Expand
#include<bits/stdc++.h>
using namespace std;
const int max_N=2e5+5;
int End[max_N<<1],Last[max_N],Next[max_N<<1],E=1;
inline void add_edge(int x,int y)
{
End[++E]=y,Next[E]=Last[x],Last[x]=E;
End[++E]=x,Next[E]=Last[y],Last[y]=E;
}
int dp[max_N][2],lim;
bool ok;
inline bool cmp(int x,int y)
{
return dp[x][1]>dp[y][1];
}
inline void upd(int &f,vector<int> v)
{
sort(v.begin(),v.end(),greater<int>());
if(v[0]+v[1]<=lim)
f=min(f,v[0]);
}
inline void upd(int &f,int a,int b,int c)
{
vector<int> v(3);
v[0]=a,v[1]=b,v[2]=c;
upd(f,v);
}
inline void upd(int &f,int a,int b,int c,int d)
{
vector<int> v(4);
v[0]=a,v[1]=b,v[2]=c,v[3]=d;
upd(f,v);
}
void dfs(int x,int fa)
{
vector<int> son;
for(int i=Last[x];i;i=Next[i])
{
int y=End[i];
if(y!=fa)
{
dfs(y,x);
if(!ok)
return;
son.push_back(y);
}
}
sort(son.begin(),son.end(),cmp);
son.resize(4);
int a=son[0],b=son[1],c=son[2],d=son[3];
dp[x][0]=dp[x][1]=lim+1;
upd(dp[x][0],min(dp[a][0],dp[a][1]+1),dp[b][1]+1,dp[c][1]+1);
upd(dp[x][0],dp[a][1]+1,min(dp[b][0],dp[b][1]+1),dp[c][1]+1);
upd(dp[x][1],min(dp[a][0],dp[a][1]+1),min(dp[b][0],dp[b][1]+1),dp[c][1]+1,dp[d][1]+1);
upd(dp[x][1],min(dp[a][0],dp[a][1]+1),dp[b][1]+1,min(dp[c][0],dp[c][1]+1),dp[d][1]+1);
upd(dp[x][1],dp[a][1]+1,min(dp[b][0],dp[b][1]+1),min(dp[c][0],dp[c][1]+1),dp[d][1]+1);
if(dp[x][0]>lim&&dp[x][1]>lim)
ok=false;
}
inline bool check(int val)
{
lim=val-1,ok=true;
dfs(1,0);
return ok;
}
int main()
{
int N;
scanf("%d",&N);
for(int i=1;i<=N-1;++i)
{
int A,B;
scanf("%d%d",&A,&B);
add_edge(A,B);
}
dp[0][0]=0,dp[0][1]=-1;
int l=1,r=N-1,ans=N;
while(l<=r)
{
int mid=(l+r)>>1;
if(check(mid))
ans=mid,r=mid-1;
else
l=mid+1;
}
printf("%d\n",ans);
return 0;
}
Submission Info
Submission Time |
|
Task |
Ex - Optimal Path Decomposition |
User |
wsyhb |
Language |
C++ (GCC 9.2.1) |
Score |
600 |
Code Size |
1842 Byte |
Status |
AC |
Exec Time |
1210 ms |
Memory |
39372 KiB |
Compile Error
./Main.cpp: In function ‘int main()’:
./Main.cpp:69:7: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
69 | scanf("%d",&N);
| ~~~~~^~~~~~~~~
./Main.cpp:73:8: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
73 | scanf("%d%d",&A,&B);
| ~~~~~^~~~~~~~~~~~~~
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
600 / 600 |
Status |
|
|
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 |
9 ms |
3628 KiB |
sample01.txt |
AC |
2 ms |
3736 KiB |
sample02.txt |
AC |
2 ms |
3728 KiB |
testcase00.txt |
AC |
2 ms |
3608 KiB |
testcase01.txt |
AC |
2 ms |
3732 KiB |
testcase02.txt |
AC |
1146 ms |
34728 KiB |
testcase03.txt |
AC |
1210 ms |
39372 KiB |
testcase04.txt |
AC |
1044 ms |
32204 KiB |
testcase05.txt |
AC |
1006 ms |
34708 KiB |
testcase06.txt |
AC |
1018 ms |
34964 KiB |
testcase07.txt |
AC |
995 ms |
34568 KiB |
testcase08.txt |
AC |
838 ms |
9996 KiB |
testcase09.txt |
AC |
842 ms |
10016 KiB |
testcase10.txt |
AC |
953 ms |
9020 KiB |
testcase11.txt |
AC |
885 ms |
8684 KiB |
testcase12.txt |
AC |
992 ms |
9036 KiB |
testcase13.txt |
AC |
963 ms |
8812 KiB |
testcase14.txt |
AC |
913 ms |
8732 KiB |
testcase15.txt |
AC |
996 ms |
9040 KiB |
testcase16.txt |
AC |
976 ms |
8724 KiB |
testcase17.txt |
AC |
1002 ms |
9068 KiB |
testcase18.txt |
AC |
999 ms |
9108 KiB |
testcase19.txt |
AC |
939 ms |
8896 KiB |
testcase20.txt |
AC |
845 ms |
8708 KiB |
testcase21.txt |
AC |
821 ms |
8516 KiB |
testcase22.txt |
AC |
913 ms |
9004 KiB |
testcase23.txt |
AC |
859 ms |
8760 KiB |
testcase24.txt |
AC |
905 ms |
8656 KiB |
testcase25.txt |
AC |
997 ms |
8988 KiB |
testcase26.txt |
AC |
1058 ms |
8964 KiB |
testcase27.txt |
AC |
919 ms |
8564 KiB |
testcase28.txt |
AC |
957 ms |
8860 KiB |
testcase29.txt |
AC |
997 ms |
8788 KiB |
testcase30.txt |
AC |
998 ms |
8928 KiB |
testcase31.txt |
AC |
989 ms |
8992 KiB |
testcase32.txt |
AC |
987 ms |
8824 KiB |
testcase33.txt |
AC |
999 ms |
8984 KiB |
testcase34.txt |
AC |
1090 ms |
9104 KiB |
testcase35.txt |
AC |
914 ms |
8564 KiB |
testcase36.txt |
AC |
1007 ms |
8896 KiB |
testcase37.txt |
AC |
1002 ms |
8992 KiB |
testcase38.txt |
AC |
939 ms |
8656 KiB |
testcase39.txt |
AC |
905 ms |
8552 KiB |
testcase40.txt |
AC |
995 ms |
8808 KiB |
testcase41.txt |
AC |
1010 ms |
8864 KiB |
testcase42.txt |
AC |
1069 ms |
9012 KiB |
testcase43.txt |
AC |
933 ms |
8704 KiB |