/*input
11 12
0 1 3
0 2 4
2 3 5
3 4 4
4 5 6
0 6 3
6 7 2
6 8 5
8 9 6
8 10 7
*/
#include<bits/stdc++.h>
#include "race.h"
using namespace std;
const int N=2e5 + 100;
const int mod=1e9 + 7;
const int inf=1e7;
#define pii pair<int, int>
#define f first
#define s second
#define mp make_pair
//Trace prints the name of the variable and the value.
int k, n;vector< pii > adjlist[N];
queue<int> roots;bool cent[N];
class Cent
{
bool visited[N];
int sum[N];stack<int> cvals;
void dfs(int i)
{
sum[i]=1;visited[i]=1;
for(auto j:adjlist[i])
{
if(visited[j.f]||cent[j.f]) continue;
dfs(j.f);sum[i]+=sum[j.f];
}
cvals.push(i);
}
public:
void init() {
for(int i=1;i<=n;i++) {visited[i]=0;sum[i]=0;}
}
bool solve()
{
init();bool check=0;
for(int i=1;i<=n;i++)
{
if(visited[i]||cent[i]) continue;check=1;
dfs(i);int tot=sum[cvals.top()];int cr, mval=inf;
while(!cvals.empty())
{
int cmax=tot-sum[cvals.top()];
for(auto j:adjlist[cvals.top()])
{
if(sum[j.f]>sum[cvals.top()]||cent[j.f]) continue;
cmax=max(cmax, sum[j.f]);
}
if(cmax<mval)
{
mval=cmax;cr=cvals.top();
}
cvals.pop();
}
roots.push(cr);cent[cr]=1;
}
return check;
}
};
int tally[N*5];int optans;
void query(int i, int p, int depth, int dist)
{
if(dist>k) return;
if(tally[k-dist]!=inf)
{
optans=min(optans, tally[k-dist] + depth);
}
for(auto j:adjlist[i])
{
if(cent[j.f]||j.f==p) continue;
query(j.f, i, depth+1, dist+j.s);
}
}
void update(int i, int p, int depth, int dist, int ind)
{
if(dist>k) return;
if(ind==0) tally[dist]=min(tally[dist], depth);
else tally[dist]=inf;
for(auto j:adjlist[i])
{
if(cent[j.f]||j.f==p) continue;
update(j.f, i, depth+1, dist+j.s, ind);
}
}
Cent obj;
int best_path(int NC, int K, int H[][2], int L[])
{
n=NC;k=K;optans=inf;
for(int i=0;i<=k;i++) tally[i]=inf;
for(int i=1;i<=n;i++) cent[i]=0;
for(int i=0;i<n-1;i++)
{
adjlist[H[i][0]+1].push_back(mp(H[i][1]+1, L[i]));adjlist[H[i][1]+1].push_back(mp(H[i][0]+1, L[i]));
}
tally[0]=0;int iter=0;
while(obj.solve())
{
while(!roots.empty())
{
int cur=roots.front();
for(auto j:adjlist[cur])
{
if(cent[j.f]) continue;
query(j.f, cur, 1, j.s);update(j.f, cur, 1, j.s, 0);
}
for(auto j:adjlist[cur])
{
if(cent[j.f]) continue;
update(j.f, cur, 1, j.s, 1);
}
roots.pop();
}
}
if(optans==inf) return -1;return optans;
}
/*signed main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int NC, K, H[N][2], L[N];
cin>>NC>>K;
for(int i=0;i<NC-1;i++)
{
cin>>H[i][0]>>H[i][1]>>L[i];
}
cout<<best_path(NC, K, H, L);
}*/
Compilation message
race.cpp: In member function 'bool Cent::solve()':
race.cpp:50:4: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
if(visited[i]||cent[i]) continue;check=1;
^~
race.cpp:50:37: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
if(visited[i]||cent[i]) continue;check=1;
^~~~~
race.cpp: In function 'int best_path(int, int, int (*)[2], int*)':
race.cpp:125:2: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
if(optans==inf) return -1;return optans;
^~
race.cpp:125:28: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
if(optans==inf) return -1;return optans;
^~~~~~
race.cpp:106:17: warning: unused variable 'iter' [-Wunused-variable]
tally[0]=0;int iter=0;
^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5120 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
7 ms |
5120 KB |
Output is correct |
4 |
Correct |
7 ms |
5120 KB |
Output is correct |
5 |
Correct |
7 ms |
4992 KB |
Output is correct |
6 |
Correct |
7 ms |
5120 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
7 ms |
5120 KB |
Output is correct |
9 |
Correct |
7 ms |
5120 KB |
Output is correct |
10 |
Correct |
7 ms |
5120 KB |
Output is correct |
11 |
Correct |
7 ms |
5120 KB |
Output is correct |
12 |
Correct |
7 ms |
5120 KB |
Output is correct |
13 |
Correct |
7 ms |
5120 KB |
Output is correct |
14 |
Correct |
7 ms |
5120 KB |
Output is correct |
15 |
Correct |
7 ms |
5120 KB |
Output is correct |
16 |
Correct |
7 ms |
5120 KB |
Output is correct |
17 |
Correct |
7 ms |
5120 KB |
Output is correct |
18 |
Correct |
7 ms |
5120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5120 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
7 ms |
5120 KB |
Output is correct |
4 |
Correct |
7 ms |
5120 KB |
Output is correct |
5 |
Correct |
7 ms |
4992 KB |
Output is correct |
6 |
Correct |
7 ms |
5120 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
7 ms |
5120 KB |
Output is correct |
9 |
Correct |
7 ms |
5120 KB |
Output is correct |
10 |
Correct |
7 ms |
5120 KB |
Output is correct |
11 |
Correct |
7 ms |
5120 KB |
Output is correct |
12 |
Correct |
7 ms |
5120 KB |
Output is correct |
13 |
Correct |
7 ms |
5120 KB |
Output is correct |
14 |
Correct |
7 ms |
5120 KB |
Output is correct |
15 |
Correct |
7 ms |
5120 KB |
Output is correct |
16 |
Correct |
7 ms |
5120 KB |
Output is correct |
17 |
Correct |
7 ms |
5120 KB |
Output is correct |
18 |
Correct |
7 ms |
5120 KB |
Output is correct |
19 |
Correct |
7 ms |
4992 KB |
Output is correct |
20 |
Correct |
7 ms |
5120 KB |
Output is correct |
21 |
Correct |
8 ms |
5120 KB |
Output is correct |
22 |
Correct |
10 ms |
8704 KB |
Output is correct |
23 |
Correct |
11 ms |
8064 KB |
Output is correct |
24 |
Correct |
10 ms |
8448 KB |
Output is correct |
25 |
Correct |
10 ms |
8448 KB |
Output is correct |
26 |
Correct |
9 ms |
6400 KB |
Output is correct |
27 |
Correct |
10 ms |
8192 KB |
Output is correct |
28 |
Correct |
9 ms |
5888 KB |
Output is correct |
29 |
Correct |
9 ms |
6400 KB |
Output is correct |
30 |
Correct |
9 ms |
6528 KB |
Output is correct |
31 |
Correct |
10 ms |
7680 KB |
Output is correct |
32 |
Correct |
10 ms |
7936 KB |
Output is correct |
33 |
Correct |
10 ms |
8192 KB |
Output is correct |
34 |
Correct |
9 ms |
7424 KB |
Output is correct |
35 |
Correct |
11 ms |
8320 KB |
Output is correct |
36 |
Correct |
10 ms |
8704 KB |
Output is correct |
37 |
Correct |
10 ms |
8192 KB |
Output is correct |
38 |
Correct |
9 ms |
7168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5120 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
7 ms |
5120 KB |
Output is correct |
4 |
Correct |
7 ms |
5120 KB |
Output is correct |
5 |
Correct |
7 ms |
4992 KB |
Output is correct |
6 |
Correct |
7 ms |
5120 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
7 ms |
5120 KB |
Output is correct |
9 |
Correct |
7 ms |
5120 KB |
Output is correct |
10 |
Correct |
7 ms |
5120 KB |
Output is correct |
11 |
Correct |
7 ms |
5120 KB |
Output is correct |
12 |
Correct |
7 ms |
5120 KB |
Output is correct |
13 |
Correct |
7 ms |
5120 KB |
Output is correct |
14 |
Correct |
7 ms |
5120 KB |
Output is correct |
15 |
Correct |
7 ms |
5120 KB |
Output is correct |
16 |
Correct |
7 ms |
5120 KB |
Output is correct |
17 |
Correct |
7 ms |
5120 KB |
Output is correct |
18 |
Correct |
7 ms |
5120 KB |
Output is correct |
19 |
Correct |
250 ms |
12356 KB |
Output is correct |
20 |
Correct |
240 ms |
12280 KB |
Output is correct |
21 |
Correct |
246 ms |
12280 KB |
Output is correct |
22 |
Correct |
238 ms |
12280 KB |
Output is correct |
23 |
Correct |
148 ms |
12536 KB |
Output is correct |
24 |
Correct |
91 ms |
12412 KB |
Output is correct |
25 |
Correct |
192 ms |
13944 KB |
Output is correct |
26 |
Correct |
126 ms |
15864 KB |
Output is correct |
27 |
Correct |
287 ms |
19832 KB |
Output is correct |
28 |
Correct |
466 ms |
27000 KB |
Output is correct |
29 |
Correct |
434 ms |
26488 KB |
Output is correct |
30 |
Correct |
301 ms |
19832 KB |
Output is correct |
31 |
Correct |
287 ms |
19836 KB |
Output is correct |
32 |
Correct |
473 ms |
19832 KB |
Output is correct |
33 |
Correct |
482 ms |
18552 KB |
Output is correct |
34 |
Correct |
383 ms |
19528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5120 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
7 ms |
5120 KB |
Output is correct |
4 |
Correct |
7 ms |
5120 KB |
Output is correct |
5 |
Correct |
7 ms |
4992 KB |
Output is correct |
6 |
Correct |
7 ms |
5120 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
7 ms |
5120 KB |
Output is correct |
9 |
Correct |
7 ms |
5120 KB |
Output is correct |
10 |
Correct |
7 ms |
5120 KB |
Output is correct |
11 |
Correct |
7 ms |
5120 KB |
Output is correct |
12 |
Correct |
7 ms |
5120 KB |
Output is correct |
13 |
Correct |
7 ms |
5120 KB |
Output is correct |
14 |
Correct |
7 ms |
5120 KB |
Output is correct |
15 |
Correct |
7 ms |
5120 KB |
Output is correct |
16 |
Correct |
7 ms |
5120 KB |
Output is correct |
17 |
Correct |
7 ms |
5120 KB |
Output is correct |
18 |
Correct |
7 ms |
5120 KB |
Output is correct |
19 |
Correct |
7 ms |
4992 KB |
Output is correct |
20 |
Correct |
7 ms |
5120 KB |
Output is correct |
21 |
Correct |
8 ms |
5120 KB |
Output is correct |
22 |
Correct |
10 ms |
8704 KB |
Output is correct |
23 |
Correct |
11 ms |
8064 KB |
Output is correct |
24 |
Correct |
10 ms |
8448 KB |
Output is correct |
25 |
Correct |
10 ms |
8448 KB |
Output is correct |
26 |
Correct |
9 ms |
6400 KB |
Output is correct |
27 |
Correct |
10 ms |
8192 KB |
Output is correct |
28 |
Correct |
9 ms |
5888 KB |
Output is correct |
29 |
Correct |
9 ms |
6400 KB |
Output is correct |
30 |
Correct |
9 ms |
6528 KB |
Output is correct |
31 |
Correct |
10 ms |
7680 KB |
Output is correct |
32 |
Correct |
10 ms |
7936 KB |
Output is correct |
33 |
Correct |
10 ms |
8192 KB |
Output is correct |
34 |
Correct |
9 ms |
7424 KB |
Output is correct |
35 |
Correct |
11 ms |
8320 KB |
Output is correct |
36 |
Correct |
10 ms |
8704 KB |
Output is correct |
37 |
Correct |
10 ms |
8192 KB |
Output is correct |
38 |
Correct |
9 ms |
7168 KB |
Output is correct |
39 |
Correct |
250 ms |
12356 KB |
Output is correct |
40 |
Correct |
240 ms |
12280 KB |
Output is correct |
41 |
Correct |
246 ms |
12280 KB |
Output is correct |
42 |
Correct |
238 ms |
12280 KB |
Output is correct |
43 |
Correct |
148 ms |
12536 KB |
Output is correct |
44 |
Correct |
91 ms |
12412 KB |
Output is correct |
45 |
Correct |
192 ms |
13944 KB |
Output is correct |
46 |
Correct |
126 ms |
15864 KB |
Output is correct |
47 |
Correct |
287 ms |
19832 KB |
Output is correct |
48 |
Correct |
466 ms |
27000 KB |
Output is correct |
49 |
Correct |
434 ms |
26488 KB |
Output is correct |
50 |
Correct |
301 ms |
19832 KB |
Output is correct |
51 |
Correct |
287 ms |
19836 KB |
Output is correct |
52 |
Correct |
473 ms |
19832 KB |
Output is correct |
53 |
Correct |
482 ms |
18552 KB |
Output is correct |
54 |
Correct |
383 ms |
19528 KB |
Output is correct |
55 |
Correct |
20 ms |
5760 KB |
Output is correct |
56 |
Correct |
22 ms |
5760 KB |
Output is correct |
57 |
Correct |
115 ms |
12412 KB |
Output is correct |
58 |
Correct |
53 ms |
12260 KB |
Output is correct |
59 |
Correct |
132 ms |
16632 KB |
Output is correct |
60 |
Correct |
785 ms |
30456 KB |
Output is correct |
61 |
Correct |
310 ms |
19960 KB |
Output is correct |
62 |
Correct |
357 ms |
23800 KB |
Output is correct |
63 |
Correct |
579 ms |
23804 KB |
Output is correct |
64 |
Correct |
986 ms |
22136 KB |
Output is correct |
65 |
Correct |
440 ms |
20856 KB |
Output is correct |
66 |
Correct |
641 ms |
29176 KB |
Output is correct |
67 |
Correct |
142 ms |
24548 KB |
Output is correct |
68 |
Correct |
496 ms |
24476 KB |
Output is correct |
69 |
Correct |
491 ms |
24700 KB |
Output is correct |
70 |
Correct |
477 ms |
23928 KB |
Output is correct |