Submission #1038625
Source Code Expand
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#include <map>
#include <set>
#include <functional>
#include <cmath>
#include <string>
#include <cassert>
#define SIZE 200005
#define INF 1000000005
using namespace std;
typedef long long int ll;
typedef pair <ll,int> P;
struct edge
{
int to,cost;
edge(int to=0,int cost=0):to(to),cost(cost){}
};
vector <edge> vec[SIZE];
set <P> dp[SIZE];
set <P>::iterator it,its;
void dfs(int v=0,int p=-1,int d=0,ll now=0)
{
dp[v].clear();
dp[v].insert(P(now,1));
ll D=(ll) d+now*2LL;
for(int i=0;i<vec[v].size();i++)
{
edge e=vec[v][i];
if(e.to!=p)
{
dfs(e.to,v,d,now+e.cost);
if(dp[e.to].size()>dp[v].size()) swap(dp[v],dp[e.to]);
vector <P> add;
for(it=dp[e.to].begin();it!=dp[e.to].end();it++)
{
P p=*it;
if(p.first*2LL>=D)
{
its=dp[v].lower_bound(P(p.first,-1));
add.push_back(P(p.first,(its==dp[v].end()?0:its->second)+p.second));
}
else add.push_back(p);
its=dp[v].lower_bound(P(D-p.first,-1));
ll t1=p.first;
int t2=p.second;
if(its!=dp[v].end()) t1=min(t1,its->first),t2+=its->second;
//if(v==0) printf("%d %d : %lld %d\n",v,e.to,t1,t2);
add.push_back(P(t1,t2));
}
for(int j=0;j<add.size();j++)
{
P p=add[j];
it=dp[v].lower_bound(P(p.first,-1));
if(it!=dp[v].end()&&it->second>p.second) continue;
while(1)
{
it=dp[v].lower_bound(P(p.first+1,-1));
if(it==dp[v].begin()) break;
it--;
if(it->second<=p.second) dp[v].erase(it);
else break;
}
dp[v].insert(p);
}
//printf("%d\n",v);
//for(it=dp[v].begin();it!=dp[v].end();it++) printf("[%lld %d] ",it->first,it->second);puts("");
}
}
}
int main()
{
int n,K;
scanf("%d %d",&n,&K);
assert(n<=40000);
for(int i=0;i<n-1;i++)
{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);a--,b--;
vec[a].push_back(edge(b,c));
vec[b].push_back(edge(a,c));
}
//dfs(0,-1,3,0);
//it=dp[0].begin();
///printf("%d\n",it->second);
//return 0;
int l=-1,r=INF;
while(r-l>1)
{
int m=(l+r)/2;
dfs(0,-1,m,0);
it=dp[0].begin();
int nm=it->second;
if(nm>=K) l=m;
else r=m;
}
printf("%d\n",l);
return 0;
}
Submission Info
Compile Error
./Main.cpp: In function ‘int main()’:
./Main.cpp:81:22: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&n,&K);
^
./Main.cpp:86:29: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d",&a,&b,&c);a--,b--;
^
Judge Result
| Set Name |
sample |
dataset1 |
dataset2 |
| Score / Max Score |
0 / 0 |
0 / 50 |
0 / 50 |
| Status |
|
|
|
| Set Name |
Test Cases |
| sample |
sample-01.txt, sample-02.txt, sample-03.txt, sample-04.txt |
| dataset1 |
sample-01.txt, sample-02.txt, sample-03.txt, sample-04.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 01-11.txt, 01-12.txt, 01-13.txt, 01-14.txt, 01-15.txt, 01-16.txt, 01-17.txt, 01-18.txt, 01-19.txt, 01-20.txt, 01-21.txt, 01-22.txt |
| dataset2 |
sample-01.txt, sample-02.txt, sample-03.txt, sample-04.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 01-11.txt, 01-12.txt, 01-13.txt, 01-14.txt, 01-15.txt, 01-16.txt, 01-17.txt, 01-18.txt, 01-19.txt, 01-20.txt, 01-21.txt, 01-22.txt, 02-01.txt, 02-02.txt, 02-03.txt, 02-04.txt, 02-05.txt, 02-06.txt, 02-07.txt, 02-08.txt, 02-09.txt, 02-10.txt, 02-11.txt, 02-12.txt, 02-13.txt, 02-14.txt, 02-15.txt, 02-16.txt, 02-17.txt, 02-18.txt, 02-19.txt, 02-20.txt, 02-21.txt, 02-22.txt |
| Case Name |
Status |
Exec Time |
Memory |
| 01-01.txt |
AC |
17 ms |
14336 KiB |
| 01-02.txt |
AC |
447 ms |
18560 KiB |
| 01-03.txt |
AC |
466 ms |
18560 KiB |
| 01-04.txt |
WA |
473 ms |
18560 KiB |
| 01-05.txt |
WA |
467 ms |
18688 KiB |
| 01-06.txt |
AC |
698 ms |
21248 KiB |
| 01-07.txt |
WA |
455 ms |
18560 KiB |
| 01-08.txt |
WA |
489 ms |
18944 KiB |
| 01-09.txt |
WA |
637 ms |
20736 KiB |
| 01-10.txt |
AC |
466 ms |
19328 KiB |
| 01-11.txt |
WA |
463 ms |
19328 KiB |
| 01-12.txt |
WA |
473 ms |
19456 KiB |
| 01-13.txt |
WA |
412 ms |
18432 KiB |
| 01-14.txt |
WA |
390 ms |
18560 KiB |
| 01-15.txt |
WA |
368 ms |
18560 KiB |
| 01-16.txt |
WA |
418 ms |
18688 KiB |
| 01-17.txt |
AC |
920 ms |
27392 KiB |
| 01-18.txt |
AC |
946 ms |
27392 KiB |
| 01-19.txt |
AC |
363 ms |
18424 KiB |
| 01-20.txt |
WA |
359 ms |
18424 KiB |
| 01-21.txt |
WA |
532 ms |
18424 KiB |
| 01-22.txt |
WA |
461 ms |
18556 KiB |
| 02-01.txt |
RE |
124 ms |
14336 KiB |
| 02-02.txt |
RE |
123 ms |
14336 KiB |
| 02-03.txt |
RE |
124 ms |
14336 KiB |
| 02-04.txt |
RE |
124 ms |
14336 KiB |
| 02-05.txt |
RE |
123 ms |
14336 KiB |
| 02-06.txt |
RE |
124 ms |
14336 KiB |
| 02-07.txt |
RE |
124 ms |
14336 KiB |
| 02-08.txt |
RE |
124 ms |
14336 KiB |
| 02-09.txt |
RE |
123 ms |
14336 KiB |
| 02-10.txt |
RE |
124 ms |
14336 KiB |
| 02-11.txt |
RE |
123 ms |
14336 KiB |
| 02-12.txt |
RE |
123 ms |
14336 KiB |
| 02-13.txt |
RE |
124 ms |
14336 KiB |
| 02-14.txt |
RE |
123 ms |
14336 KiB |
| 02-15.txt |
RE |
123 ms |
14336 KiB |
| 02-16.txt |
RE |
123 ms |
14336 KiB |
| 02-17.txt |
RE |
123 ms |
14336 KiB |
| 02-18.txt |
RE |
124 ms |
14336 KiB |
| 02-19.txt |
RE |
124 ms |
14336 KiB |
| 02-20.txt |
RE |
123 ms |
14336 KiB |
| 02-21.txt |
RE |
124 ms |
14336 KiB |
| 02-22.txt |
RE |
123 ms |
14336 KiB |
| sample-01.txt |
AC |
17 ms |
14336 KiB |
| sample-02.txt |
AC |
17 ms |
14336 KiB |
| sample-03.txt |
AC |
17 ms |
14336 KiB |
| sample-04.txt |
AC |
17 ms |
14336 KiB |