#include <bits/stdc++.h>
using namespace std;
int mat[200001][2],ll[200001],p;
const int inf=1e9;
struct ura
{
int t,s;
bool ok;
}v[200001];
vector < pair<int,int> > g[200001];
int sol;
void dfs(int nod, int n)
{
bool ok=true;
v[nod].s=1;
for(auto x : g[nod])
{
if(!v[x.first].ok&&v[nod].t!=x.first)
{
v[x.first].t=nod;
dfs(x.first,n);
v[nod].s+=v[x.first].s;
if(v[x.first].s>n/2)
ok=false;
}
}
if(n-v[nod].s>n/2)
ok=false;
if(ok)
sol=nod;
}
unordered_map <long long,int> maph;
std :: unordered_map <long long,int> :: iterator it;
int rez=inf;
void dfs1(int nod, long long d, int nrm)
{
it=maph.find(p-d);
if(it!=maph.end())
rez=min(rez,(*it).second+nrm);
for(auto x : g[nod])
{
if(!v[x.first].ok&&x.first!=v[nod].t)
{
v[x.first].t=nod;
dfs1(x.first,d+x.second,nrm+1);
}
}
}
void dfs2(int nod, long long d, int nrm)
{
it=maph.find(d);
if(it==maph.end())
maph.insert(make_pair(d,nrm));
else
maph.at(d)=min(maph.at(d),nrm);
for(auto x : g[nod])
{
if(!v[x.first].ok&&x.first!=v[nod].t)
{
v[x.first].t=nod;
dfs2(x.first,d+x.second,nrm+1);
}
}
}
void divide(int nod, int nr)
{
if(nr==1)
return;
maph.clear();
maph.insert(make_pair(0,0));
dfs(nod,nr);
int ctd=sol;
for(auto x : g[ctd])
{
if(!v[x.first].ok)
{
v[x.first].t=ctd;
dfs1(x.first,x.second,1);
dfs2(x.first,x.second,1);
}
}
v[ctd].ok=true;
for(auto x : g[ctd])
{
if(!v[x.first].ok)
{
v[x.first].t=ctd;
divide(x.first,v[x.first].s);
}
}
}
int best_path(int n,int k,int h[][2],int l[])
{
int i;
for(i=0;i<n-1;i++)
{
g[h[i][0]].push_back(make_pair(h[i][1],l[i]));
g[h[i][1]].push_back(make_pair(h[i][0],l[i]));
}
p=k;
v[1].t=n;
divide(1,n);
if(rez==inf)
rez=-1;
return rez;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12624 KB |
Output is correct |
2 |
Correct |
3 ms |
12624 KB |
Output is correct |
3 |
Correct |
2 ms |
12624 KB |
Output is correct |
4 |
Correct |
2 ms |
12624 KB |
Output is correct |
5 |
Correct |
3 ms |
12624 KB |
Output is correct |
6 |
Correct |
3 ms |
12624 KB |
Output is correct |
7 |
Correct |
3 ms |
12624 KB |
Output is correct |
8 |
Correct |
3 ms |
12624 KB |
Output is correct |
9 |
Correct |
3 ms |
12624 KB |
Output is correct |
10 |
Correct |
2 ms |
12624 KB |
Output is correct |
11 |
Correct |
2 ms |
12624 KB |
Output is correct |
12 |
Correct |
2 ms |
12624 KB |
Output is correct |
13 |
Correct |
2 ms |
12772 KB |
Output is correct |
14 |
Correct |
2 ms |
12624 KB |
Output is correct |
15 |
Correct |
2 ms |
12624 KB |
Output is correct |
16 |
Correct |
2 ms |
12624 KB |
Output is correct |
17 |
Correct |
2 ms |
12624 KB |
Output is correct |
18 |
Correct |
2 ms |
12748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12624 KB |
Output is correct |
2 |
Correct |
3 ms |
12624 KB |
Output is correct |
3 |
Correct |
2 ms |
12624 KB |
Output is correct |
4 |
Correct |
2 ms |
12624 KB |
Output is correct |
5 |
Correct |
3 ms |
12624 KB |
Output is correct |
6 |
Correct |
3 ms |
12624 KB |
Output is correct |
7 |
Correct |
3 ms |
12624 KB |
Output is correct |
8 |
Correct |
3 ms |
12624 KB |
Output is correct |
9 |
Correct |
3 ms |
12624 KB |
Output is correct |
10 |
Correct |
2 ms |
12624 KB |
Output is correct |
11 |
Correct |
2 ms |
12624 KB |
Output is correct |
12 |
Correct |
2 ms |
12624 KB |
Output is correct |
13 |
Correct |
2 ms |
12772 KB |
Output is correct |
14 |
Correct |
2 ms |
12624 KB |
Output is correct |
15 |
Correct |
2 ms |
12624 KB |
Output is correct |
16 |
Correct |
2 ms |
12624 KB |
Output is correct |
17 |
Correct |
2 ms |
12624 KB |
Output is correct |
18 |
Correct |
2 ms |
12748 KB |
Output is correct |
19 |
Incorrect |
2 ms |
12624 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12624 KB |
Output is correct |
2 |
Correct |
3 ms |
12624 KB |
Output is correct |
3 |
Correct |
2 ms |
12624 KB |
Output is correct |
4 |
Correct |
2 ms |
12624 KB |
Output is correct |
5 |
Correct |
3 ms |
12624 KB |
Output is correct |
6 |
Correct |
3 ms |
12624 KB |
Output is correct |
7 |
Correct |
3 ms |
12624 KB |
Output is correct |
8 |
Correct |
3 ms |
12624 KB |
Output is correct |
9 |
Correct |
3 ms |
12624 KB |
Output is correct |
10 |
Correct |
2 ms |
12624 KB |
Output is correct |
11 |
Correct |
2 ms |
12624 KB |
Output is correct |
12 |
Correct |
2 ms |
12624 KB |
Output is correct |
13 |
Correct |
2 ms |
12772 KB |
Output is correct |
14 |
Correct |
2 ms |
12624 KB |
Output is correct |
15 |
Correct |
2 ms |
12624 KB |
Output is correct |
16 |
Correct |
2 ms |
12624 KB |
Output is correct |
17 |
Correct |
2 ms |
12624 KB |
Output is correct |
18 |
Correct |
2 ms |
12748 KB |
Output is correct |
19 |
Correct |
156 ms |
19792 KB |
Output is correct |
20 |
Correct |
154 ms |
19784 KB |
Output is correct |
21 |
Correct |
125 ms |
19836 KB |
Output is correct |
22 |
Correct |
129 ms |
19900 KB |
Output is correct |
23 |
Correct |
82 ms |
20304 KB |
Output is correct |
24 |
Correct |
59 ms |
20204 KB |
Output is correct |
25 |
Correct |
194 ms |
27988 KB |
Output is correct |
26 |
Correct |
75 ms |
27296 KB |
Output is correct |
27 |
Correct |
185 ms |
22712 KB |
Output is correct |
28 |
Execution timed out |
3076 ms |
50880 KB |
Time limit exceeded |
29 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12624 KB |
Output is correct |
2 |
Correct |
3 ms |
12624 KB |
Output is correct |
3 |
Correct |
2 ms |
12624 KB |
Output is correct |
4 |
Correct |
2 ms |
12624 KB |
Output is correct |
5 |
Correct |
3 ms |
12624 KB |
Output is correct |
6 |
Correct |
3 ms |
12624 KB |
Output is correct |
7 |
Correct |
3 ms |
12624 KB |
Output is correct |
8 |
Correct |
3 ms |
12624 KB |
Output is correct |
9 |
Correct |
3 ms |
12624 KB |
Output is correct |
10 |
Correct |
2 ms |
12624 KB |
Output is correct |
11 |
Correct |
2 ms |
12624 KB |
Output is correct |
12 |
Correct |
2 ms |
12624 KB |
Output is correct |
13 |
Correct |
2 ms |
12772 KB |
Output is correct |
14 |
Correct |
2 ms |
12624 KB |
Output is correct |
15 |
Correct |
2 ms |
12624 KB |
Output is correct |
16 |
Correct |
2 ms |
12624 KB |
Output is correct |
17 |
Correct |
2 ms |
12624 KB |
Output is correct |
18 |
Correct |
2 ms |
12748 KB |
Output is correct |
19 |
Incorrect |
2 ms |
12624 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |