# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1099123 |
2024-10-10T15:25:55 Z |
icebear |
Race (IOI11_race) |
C++14 |
|
0 ms |
0 KB |
// ~~ icebear ~~
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
#define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
#define FORR(i,a,b) for(int i=(a); i>=(b); --i)
#define rep(i, n) for(int i=0; i<(n); ++i)
#define red(i, n) for(int i=(n)-1; i>=0; --i)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ar(x) array<int, (x)>
#define all(x) x.begin(), x.end()
#define task "icebearat"
const int MOD = 1e9 + 7;
const int inf = 1e9 + 27092008;
const ll LLinf = 1e18 + 27092008;
const int N = 2e5 + 5;
int numNode, K;
vector<ii> G[N];
bool isCentroid[N];
int sz[N];
void DFS(int u, int par) {
sz[u] = 1;
for(ii e : G[u])
if (e.fi != par && !isCentroid[e.fi]) {
DFS(e.fi, u);
sz[u] += sz[e.fi];
}
}
int findCentroid(int u, int par, const int &subtreeSize) {
for(ii e : G[u])
if (e.fi != par && !isCentroid[e.fi] && sz[e.fi] > subtreeSize / 2)
return findCentroid(e.fi, u, subtreeSize);
return u;
}
int ans;
map<int, int> minEdge;
void dfs(int u, bool filling, int numWeight, int numEdge = 1, int par = -1) {
if (numWeight > K) return;
if (filling) {
if (minEdge.find(numWeight) == minEdge.end())
minEdge[numWeight] = numEdge;
else
minEdge[numWeight] = min(minEdge[numWeight], numEdge);
}
else {
if (minEdge.find(K - numWeight) != minEdge.end())
ans = min(ans, minEdge[K - numWeight] + numEdge);
}
for(ii e : G[u])
if (e.fi != par && !isCentroid[e.fi])
dfs(e.fi, filling, numWeight + e.se, numEdge + 1, u);
}
void buildCentroid(int u) {
DFS(u, -1);
int centroid = findCentroid(u, -1, sz[u]);
isCentroid[centroid] = true;
minEdge.clear();
minEdge[0] = 0;
for(ii e : G[centroid])
if (!isCentroid[e.fi]) {
dfs(e.fi, false, e.se);
dfs(e.fi, true, e.se);
}
for(ii e : G[centroid])
if (!isCentroid[e.fi])
buildCentroid(e.fi);
}
void solve() {
cin >> numNode >> K;
FOR(i, 1, numNode - 1) {
int u, v, w;
cin >> u >> v >> w;
u++, v++;
G[u].pb(mp(v, w));
G[v].pb(mp(u, w));
}
ans = inf;
buildCentroid(1);
cout << (ans == inf ? -1 : ans);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (fopen(task".inp", "r")){
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int tc = 1;
// cin >> tc;
while(tc--) solve();
return 0;
}
Compilation message
race.cpp: In function 'int main()':
race.cpp:107:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
107 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
race.cpp:108:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
108 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/ld: /tmp/cczDMHok.o: in function `main':
race.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccAAApsn.o:grader.cpp:(.text.startup+0x0): first defined here
/usr/bin/ld: /tmp/ccAAApsn.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