# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1105056 |
2024-10-25T09:03:26 Z |
whoknow |
Race (IOI11_race) |
C++17 |
|
0 ms |
0 KB |
#include <bits/stdc++.h>
#define ll long long
#define F first
#define S second
#define MAXN 200005
#define ii pair<int,int>
#define bit(i,j) ((i>>j)&1)
#define sz(i) (int)i.size()
#define endl '\n'
using namespace std;
const int INF = 1e9 + 7;
int n, K;
vector<ii>g[MAXN];
namespace sub1
{
int res = INF;
int sz[MAXN], h[MAXN], s[MAXN];
bool del[MAXN];
vector<int>dp(MAXN);
vector<bool>dd(MAXN);
int cnt(int u, int p)
{
sz[u] = 1;
for(ii i : g[u])
{
int v = i.F, w = i.S;
if(v == p || del[v])
continue;
sz[u] += cnt(v, u);
}
return sz[u];
}
int centroid(int u, int p, int cnt)
{
for(ii i : g[u])
{
if(i.F == p || del[i.F])
continue;
if(sz[i.F] > cnt / 2)
return centroid(i.F, u, cnt);
}
return u;
}
void dfs(int u, int p)
{
h[u] = h[p] + 1;
if(K >= s[u])
if(dp[K - s[u]] && dd[K - s[u]])
res = min(res, dp[K - s[u]] + h[u]);
for(ii i : g[u])
{
int v = i.F, w = i.S;
if(v == p || del[v])
continue;
h[v] = h[u] + w;
dfs(v, u);
}
}
void clr(int u, int p)
{
if(dd[s[u]])
dp[s[u]] = min(dp[s[u]], h[u]);
else
dp[s[u]] = h[u];
dd[s[u]] = 1;
s[u] = 0, h[u] = 0;
for(ii i : g[u])
{
int v = i.F;
if(v == p || del[v])
continue;
clr(v, u);
}
}
void build(int u, int p)
{
int mid = centroid(u, 0, cnt(u, 0));
dd[0] = 1;
for(ii i : g[mid])
{
int v = i.F, w = i.S;
if(del[v])
continue;
s[v] = s[mid] + w;
dfs(v, mid);
clr(v, mid);
}
vector<int>(MAXN).swap(dp);
vector<bool>(MAXN).swap(dd);
del[mid] = 1;
for(ii i : g[mid])
{
int v = i.F;
if(del[v])
continue;
build(v, mid);
}
}
void solve()
{
build(1, 0);
cout << res;
}
}
main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> K;
for(int i = 1; i < n; i++)
{
int x, y, z;
cin >> x >> y >> z;
x++, y++;
g[x].push_back({y, z});
g[y].push_back({x, z});
}
sub1::solve();
}
/**
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
**/
Compilation message
race.cpp: In function 'int sub1::cnt(int, int)':
race.cpp:26:22: warning: unused variable 'w' [-Wunused-variable]
26 | int v = i.F, w = i.S;
| ^
race.cpp: At global scope:
race.cpp:105:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
105 | main()
| ^~~~
/usr/bin/ld: /tmp/ccWuydUV.o: in function `main':
race.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccTCxTRT.o:grader.cpp:(.text.startup+0x0): first defined here
/usr/bin/ld: /tmp/ccTCxTRT.o: in function `main':
grader.cpp:(.text.startup+0x28): undefined reference to `best_path(int, int, int (*) [2], int*)'
collect2: error: ld returned 1 exit status