# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
846612 |
2023-09-08T07:01:01 Z |
vjudge1 |
Torrent (COI16_torrent) |
C++17 |
|
312 ms |
25932 KB |
#include <bits/stdc++.h>
#define pll pair <long long, long long>
#define fi first
#define se second
using namespace std;
#define task "code"
#define all(s) s.begin(), s.end()
typedef long long ll;
const int ar = 3e5+2;
const ll mod = 998244353;
const int oo = 1e9;
int n, a, b, child[ar], dp[ar], pre[ar], suf[ar], f[ar], res = oo;
vector <int> g[ar], trace;
void caltrace(int u, int p) {
if(trace.empty() || trace.back() != b) {
trace.push_back(u);
}
for(auto v : g[u]) if(v != p)
caltrace(v, u);
if(trace.back() != b)
trace.pop_back();
}
void dfs(int u, int p) {
vector <int> tg;
dp[u] = 0;
for(auto v : g[u]) if(v != p && v != child[u]) {
dfs(v, u);
// if(u == 3) cout << v << " 3333\n";
tg.push_back(dp[v]);
}
sort(all(tg), greater<int>());
// if(u == 3) cout << tg.size() << '\n';
for(int i = 0; i < tg.size(); ++i)
dp[u] = max(dp[u], tg[i] + i + 1);
// cout << u << ' ' << dp[u] << '\n';
}
bool incr(int i) {
child[trace[i - 1]] = trace[i];
child[trace[i]] = trace[i - 1];
dfs(a, 0);
dfs(b, 0);
res = min(res, max(dp[a], dp[b]));
child[trace[i - 1]] = child[trace[i]] = 0;
return dp[a] < dp[b];
}
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);
}
cin >> n >> a >> b;
for(int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
caltrace(a, 0);
if(a == b) return cout << dp[a], 0;
int m = trace.size();
// for(auto x : trace) cout << x << ' '; cout << '\n';
int l = 1, r = m - 1;
while(l <= r) {
int mid = l + r >> 1;
if(incr(mid)) l = mid + 1;
else r = mid - 1;
}
cout << res;
return 0;
}
Compilation message
torrent.cpp: In function 'void dfs(int, int)':
torrent.cpp:39:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for(int i = 0; i < tg.size(); ++i)
| ~~^~~~~~~~~~~
torrent.cpp: In function 'int main()':
torrent.cpp:78:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
78 | int mid = l + r >> 1;
| ~~^~~
torrent.cpp:60:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
torrent.cpp:61:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10852 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
2 ms |
10852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
312 ms |
22544 KB |
Output is correct |
2 |
Correct |
281 ms |
23996 KB |
Output is correct |
3 |
Correct |
280 ms |
25484 KB |
Output is correct |
4 |
Correct |
274 ms |
24888 KB |
Output is correct |
5 |
Correct |
270 ms |
22096 KB |
Output is correct |
6 |
Correct |
245 ms |
22728 KB |
Output is correct |
7 |
Correct |
256 ms |
25932 KB |
Output is correct |