#include <bits/stdc++.h>
#include "race.h"
using namespace std;
#define F first
#define S second
#define FOR(i,a,b) for (auto i = (a); i <= (b); ++i)
#define NFOR(i,a,b) for(auto i = (a); i >= (b); --i)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define mp(i,a) make_pair(i,a)
#define pb(a) push_back(a)
#define bit(x,b) (x&(1LL<<b))
typedef long long int lli;
typedef pair <lli,lli> ii;
typedef pair <lli,ii> iii;
typedef vector <lli> vi;
int vis[200001]={},vis1[200001]={},sz[200001],vis2[200001];
map<int,int> m[200001];
vector<ii> al[200001];
int n,k,ans=200001,co=0;
void dfs(int node)
{
vis1[node]=1;
//cout<<node<<endl;
sz[node]=1;
for(int i=0;i<al[node].size();++i)
{
if(vis[al[node][i].F]+vis1[al[node][i].F]==0)
{
dfs(al[node][i].F);
sz[node]+=sz[al[node][i].F];
}
}
}
int tell(int node,int p,int to)
{
for(int i=0;i<al[node].size();++i)
if(vis[al[node][i].F]==0&&al[node][i].F!=p&&sz[al[node][i].F]>to/2)
return(tell(al[node][i].F,node,to));
return(node);
}
void dfs1(int node,int root,int upa,int dis,int e)
{
vis2[node]=1;
if(upa!=-1)
{
if(m[upa].find(dis)==m[upa].end())
m[upa][dis]=e;
else
m[upa][dis]=min(m[upa][dis],e);
}
int f=-1;
for(int i=0;i<al[node].size();++i)
{
if(vis[al[node][i].F]+vis2[al[node][i].F]==0)
{
if(root==node)
{
f=al[node][i].F;
dfs1(al[node][i].F,root,al[node][i].F,dis+al[node][i].S,e+1);
m[al[node][i].F][0]=0;
}
else
dfs1(al[node][i].F,root,upa,dis+al[node][i].S,e+1);
}
}
if(node==root)
{
for(int i=0;i<al[node].size();++i)
{
// cout<<f<<' '<<al[node][i].F<<endl;
if(f==al[node][i].F||vis[al[node][i].F]==1)
continue;
for(auto j=(m[al[node][i].F].begin());j!=m[al[node][i].F].end();++j)
{
if((*j).F>k)
break;
if(m[f].find(k-(*j).F)!=m[f].end())
ans=min(ans,m[f][k-(*j).F]+(*j).S);
}
for(auto j=(m[al[node][i].F].begin());j!=m[al[node][i].F].end();++j)
{
if(m[f].find((*j).F)==m[f].end())
m[f][(*j).F]=(*j).S;
else
m[f][(*j).F]=min(m[f][(*j).F],(*j).S);
}
m[al[node][i].F].clear();
}
if(f!=-1)
m[f].clear();
}
}
int best_path(int N, int K, int H[][2], int L[])
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
n=N;
k=K;
int c=0;
for(int i=0;i<n-1;++i)
{
al[H[i][0]].pb(mp(H[i][1],L[i]));
al[H[i][1]].pb(mp(H[i][0],L[i]));
}
while(c!=n)
{
for(int i=0;i<n;++i)
{
if(vis[i]+vis1[i]==0)
{
// cout<<"hi "<<i<<' '<<vis1[i]<<endl;
dfs(i);
int cen=tell(i,i,sz[i]);
vis[cen]=1;
//cout<<i<<cen<<endl;
dfs1(cen,cen,-1,0,0);
c++;
}
}
// cout<<ans<<"Masti"<<endl;
memset(vis1,0,sizeof(vis1));
memset(vis2,0,sizeof(vis2));
}
if(ans==200001)
return(-1);
return(ans);
}
Compilation message
race.cpp: In function 'void dfs(int)':
race.cpp:27:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<al[node].size();++i)
~^~~~~~~~~~~~~~~~
race.cpp: In function 'int tell(int, int, int)':
race.cpp:38:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<al[node].size();++i)
~^~~~~~~~~~~~~~~~
race.cpp: In function 'void dfs1(int, int, int, int, int)':
race.cpp:54:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<al[node].size();++i)
~^~~~~~~~~~~~~~~~
race.cpp:70:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<al[node].size();++i)
~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
16000 KB |
Output is correct |
2 |
Correct |
11 ms |
16000 KB |
Output is correct |
3 |
Correct |
10 ms |
16000 KB |
Output is correct |
4 |
Correct |
11 ms |
16000 KB |
Output is correct |
5 |
Correct |
13 ms |
16000 KB |
Output is correct |
6 |
Correct |
11 ms |
16000 KB |
Output is correct |
7 |
Correct |
20 ms |
16000 KB |
Output is correct |
8 |
Correct |
11 ms |
16000 KB |
Output is correct |
9 |
Correct |
11 ms |
16000 KB |
Output is correct |
10 |
Correct |
15 ms |
16000 KB |
Output is correct |
11 |
Correct |
11 ms |
16000 KB |
Output is correct |
12 |
Correct |
11 ms |
16000 KB |
Output is correct |
13 |
Correct |
11 ms |
16000 KB |
Output is correct |
14 |
Correct |
11 ms |
16000 KB |
Output is correct |
15 |
Correct |
15 ms |
16128 KB |
Output is correct |
16 |
Correct |
11 ms |
16000 KB |
Output is correct |
17 |
Correct |
11 ms |
16000 KB |
Output is correct |
18 |
Correct |
21 ms |
16000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
16000 KB |
Output is correct |
2 |
Correct |
11 ms |
16000 KB |
Output is correct |
3 |
Correct |
10 ms |
16000 KB |
Output is correct |
4 |
Correct |
11 ms |
16000 KB |
Output is correct |
5 |
Correct |
13 ms |
16000 KB |
Output is correct |
6 |
Correct |
11 ms |
16000 KB |
Output is correct |
7 |
Correct |
20 ms |
16000 KB |
Output is correct |
8 |
Correct |
11 ms |
16000 KB |
Output is correct |
9 |
Correct |
11 ms |
16000 KB |
Output is correct |
10 |
Correct |
15 ms |
16000 KB |
Output is correct |
11 |
Correct |
11 ms |
16000 KB |
Output is correct |
12 |
Correct |
11 ms |
16000 KB |
Output is correct |
13 |
Correct |
11 ms |
16000 KB |
Output is correct |
14 |
Correct |
11 ms |
16000 KB |
Output is correct |
15 |
Correct |
15 ms |
16128 KB |
Output is correct |
16 |
Correct |
11 ms |
16000 KB |
Output is correct |
17 |
Correct |
11 ms |
16000 KB |
Output is correct |
18 |
Correct |
21 ms |
16000 KB |
Output is correct |
19 |
Correct |
10 ms |
16000 KB |
Output is correct |
20 |
Correct |
10 ms |
16000 KB |
Output is correct |
21 |
Correct |
13 ms |
16128 KB |
Output is correct |
22 |
Correct |
14 ms |
16128 KB |
Output is correct |
23 |
Correct |
18 ms |
16128 KB |
Output is correct |
24 |
Correct |
14 ms |
16128 KB |
Output is correct |
25 |
Correct |
13 ms |
16128 KB |
Output is correct |
26 |
Correct |
15 ms |
16128 KB |
Output is correct |
27 |
Correct |
13 ms |
16128 KB |
Output is correct |
28 |
Correct |
26 ms |
16128 KB |
Output is correct |
29 |
Correct |
27 ms |
16128 KB |
Output is correct |
30 |
Correct |
19 ms |
16128 KB |
Output is correct |
31 |
Correct |
14 ms |
16128 KB |
Output is correct |
32 |
Correct |
14 ms |
16128 KB |
Output is correct |
33 |
Correct |
16 ms |
16128 KB |
Output is correct |
34 |
Correct |
13 ms |
16128 KB |
Output is correct |
35 |
Correct |
19 ms |
16128 KB |
Output is correct |
36 |
Correct |
18 ms |
16128 KB |
Output is correct |
37 |
Correct |
15 ms |
16128 KB |
Output is correct |
38 |
Correct |
24 ms |
16128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
16000 KB |
Output is correct |
2 |
Correct |
11 ms |
16000 KB |
Output is correct |
3 |
Correct |
10 ms |
16000 KB |
Output is correct |
4 |
Correct |
11 ms |
16000 KB |
Output is correct |
5 |
Correct |
13 ms |
16000 KB |
Output is correct |
6 |
Correct |
11 ms |
16000 KB |
Output is correct |
7 |
Correct |
20 ms |
16000 KB |
Output is correct |
8 |
Correct |
11 ms |
16000 KB |
Output is correct |
9 |
Correct |
11 ms |
16000 KB |
Output is correct |
10 |
Correct |
15 ms |
16000 KB |
Output is correct |
11 |
Correct |
11 ms |
16000 KB |
Output is correct |
12 |
Correct |
11 ms |
16000 KB |
Output is correct |
13 |
Correct |
11 ms |
16000 KB |
Output is correct |
14 |
Correct |
11 ms |
16000 KB |
Output is correct |
15 |
Correct |
15 ms |
16128 KB |
Output is correct |
16 |
Correct |
11 ms |
16000 KB |
Output is correct |
17 |
Correct |
11 ms |
16000 KB |
Output is correct |
18 |
Correct |
21 ms |
16000 KB |
Output is correct |
19 |
Correct |
664 ms |
24440 KB |
Output is correct |
20 |
Correct |
597 ms |
24548 KB |
Output is correct |
21 |
Correct |
460 ms |
24532 KB |
Output is correct |
22 |
Correct |
497 ms |
24568 KB |
Output is correct |
23 |
Correct |
556 ms |
25208 KB |
Output is correct |
24 |
Correct |
260 ms |
24312 KB |
Output is correct |
25 |
Correct |
410 ms |
31608 KB |
Output is correct |
26 |
Correct |
135 ms |
31096 KB |
Output is correct |
27 |
Correct |
549 ms |
38520 KB |
Output is correct |
28 |
Correct |
1896 ms |
60792 KB |
Output is correct |
29 |
Correct |
1898 ms |
60664 KB |
Output is correct |
30 |
Correct |
610 ms |
38392 KB |
Output is correct |
31 |
Correct |
527 ms |
38520 KB |
Output is correct |
32 |
Correct |
814 ms |
38640 KB |
Output is correct |
33 |
Correct |
1225 ms |
32888 KB |
Output is correct |
34 |
Correct |
1807 ms |
46708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
16000 KB |
Output is correct |
2 |
Correct |
11 ms |
16000 KB |
Output is correct |
3 |
Correct |
10 ms |
16000 KB |
Output is correct |
4 |
Correct |
11 ms |
16000 KB |
Output is correct |
5 |
Correct |
13 ms |
16000 KB |
Output is correct |
6 |
Correct |
11 ms |
16000 KB |
Output is correct |
7 |
Correct |
20 ms |
16000 KB |
Output is correct |
8 |
Correct |
11 ms |
16000 KB |
Output is correct |
9 |
Correct |
11 ms |
16000 KB |
Output is correct |
10 |
Correct |
15 ms |
16000 KB |
Output is correct |
11 |
Correct |
11 ms |
16000 KB |
Output is correct |
12 |
Correct |
11 ms |
16000 KB |
Output is correct |
13 |
Correct |
11 ms |
16000 KB |
Output is correct |
14 |
Correct |
11 ms |
16000 KB |
Output is correct |
15 |
Correct |
15 ms |
16128 KB |
Output is correct |
16 |
Correct |
11 ms |
16000 KB |
Output is correct |
17 |
Correct |
11 ms |
16000 KB |
Output is correct |
18 |
Correct |
21 ms |
16000 KB |
Output is correct |
19 |
Correct |
10 ms |
16000 KB |
Output is correct |
20 |
Correct |
10 ms |
16000 KB |
Output is correct |
21 |
Correct |
13 ms |
16128 KB |
Output is correct |
22 |
Correct |
14 ms |
16128 KB |
Output is correct |
23 |
Correct |
18 ms |
16128 KB |
Output is correct |
24 |
Correct |
14 ms |
16128 KB |
Output is correct |
25 |
Correct |
13 ms |
16128 KB |
Output is correct |
26 |
Correct |
15 ms |
16128 KB |
Output is correct |
27 |
Correct |
13 ms |
16128 KB |
Output is correct |
28 |
Correct |
26 ms |
16128 KB |
Output is correct |
29 |
Correct |
27 ms |
16128 KB |
Output is correct |
30 |
Correct |
19 ms |
16128 KB |
Output is correct |
31 |
Correct |
14 ms |
16128 KB |
Output is correct |
32 |
Correct |
14 ms |
16128 KB |
Output is correct |
33 |
Correct |
16 ms |
16128 KB |
Output is correct |
34 |
Correct |
13 ms |
16128 KB |
Output is correct |
35 |
Correct |
19 ms |
16128 KB |
Output is correct |
36 |
Correct |
18 ms |
16128 KB |
Output is correct |
37 |
Correct |
15 ms |
16128 KB |
Output is correct |
38 |
Correct |
24 ms |
16128 KB |
Output is correct |
39 |
Correct |
664 ms |
24440 KB |
Output is correct |
40 |
Correct |
597 ms |
24548 KB |
Output is correct |
41 |
Correct |
460 ms |
24532 KB |
Output is correct |
42 |
Correct |
497 ms |
24568 KB |
Output is correct |
43 |
Correct |
556 ms |
25208 KB |
Output is correct |
44 |
Correct |
260 ms |
24312 KB |
Output is correct |
45 |
Correct |
410 ms |
31608 KB |
Output is correct |
46 |
Correct |
135 ms |
31096 KB |
Output is correct |
47 |
Correct |
549 ms |
38520 KB |
Output is correct |
48 |
Correct |
1896 ms |
60792 KB |
Output is correct |
49 |
Correct |
1898 ms |
60664 KB |
Output is correct |
50 |
Correct |
610 ms |
38392 KB |
Output is correct |
51 |
Correct |
527 ms |
38520 KB |
Output is correct |
52 |
Correct |
814 ms |
38640 KB |
Output is correct |
53 |
Correct |
1225 ms |
32888 KB |
Output is correct |
54 |
Correct |
1807 ms |
46708 KB |
Output is correct |
55 |
Correct |
56 ms |
17408 KB |
Output is correct |
56 |
Correct |
50 ms |
16888 KB |
Output is correct |
57 |
Correct |
183 ms |
24824 KB |
Output is correct |
58 |
Correct |
91 ms |
33128 KB |
Output is correct |
59 |
Correct |
466 ms |
36880 KB |
Output is correct |
60 |
Correct |
2232 ms |
59768 KB |
Output is correct |
61 |
Correct |
620 ms |
39928 KB |
Output is correct |
62 |
Correct |
538 ms |
38776 KB |
Output is correct |
63 |
Correct |
831 ms |
38648 KB |
Output is correct |
64 |
Correct |
2315 ms |
42980 KB |
Output is correct |
65 |
Correct |
1925 ms |
47640 KB |
Output is correct |
66 |
Correct |
2670 ms |
60640 KB |
Output is correct |
67 |
Correct |
302 ms |
51380 KB |
Output is correct |
68 |
Correct |
969 ms |
46180 KB |
Output is correct |
69 |
Correct |
1193 ms |
46496 KB |
Output is correct |
70 |
Correct |
1012 ms |
44880 KB |
Output is correct |