# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
846685 |
2023-09-08T08:59:08 Z |
vjudge1 |
Torrent (COI16_torrent) |
C++17 |
|
338 ms |
28100 KB |
#include <bits/stdc++.h>
#define task ""
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<long long, long long>
using namespace std;
using ll = long long;
const ll mod = 998244353;
const int inf = 1e9 + 1;
const int arr = 300001;
ll n, a, b, dp[arr], child[arr];
vector<ll> adj[arr];
vector<ll> trace;
void findpath(ll u, ll p) {
if(trace.empty() || trace.back() != b){
trace.push_back(u);
}
for(auto v : adj[u]) if(v != p)
findpath(v, u);
if(trace.back() != b)
trace.pop_back();
}
// bool cmp(ll a, ll b){
// return a > b;
// }
void dfs(ll u, ll p){
vector<ll> time;
dp[u] = 0;
for(auto v : adj[u]){
if(v != p && v != child[u]){
dfs(v, u);
time.push_back(dp[v]);
}
}
sort(time.begin(), time.end(), greater<ll>());
for(ll i = 0; i < time.size(); i++){
dp[u] = max(dp[u], time[i] + i + 1);
}
}
pll calc(){
//memset(dp, 0, sizeof dp);
dfs(a, 0);
//memset(dp, 0, sizeof dp);
dfs(b, 0);
ll ra = dp[a];
ll rb = dp[b];
return {ra, rb};
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
// freopen(task".inp" , "r" , stdin);
// freopen(task".out" , "w" , stdout);
cin >> n >> a >> b;
for(ll i = 1; i <= n; i++){
ll x, y; cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
findpath(a, 0);
ll l = 1, r = trace.size() - 1, res = 1e9;
while(l <= r){
ll mid = (l + r) / 2;
child[trace[mid]] = trace[mid - 1];
child[trace[mid - 1]] = trace[mid];
pll tmp = calc();
child[trace[mid]] = child[trace[mid - 1]] = 0;
res = min(res, max(tmp.fi, tmp.se));
if(tmp.fi > tmp.se)r = mid - 1;
else l = mid + 1;
}
cout << res << '\n';
}
Compilation message
torrent.cpp: In function 'void dfs(ll, ll)':
torrent.cpp:40:21: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for(ll i = 0; i < time.size(); i++){
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
10844 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
320 ms |
25228 KB |
Output is correct |
2 |
Correct |
296 ms |
26476 KB |
Output is correct |
3 |
Correct |
275 ms |
27596 KB |
Output is correct |
4 |
Correct |
338 ms |
27088 KB |
Output is correct |
5 |
Correct |
316 ms |
24496 KB |
Output is correct |
6 |
Correct |
269 ms |
25348 KB |
Output is correct |
7 |
Correct |
261 ms |
28100 KB |
Output is correct |