#include<bits/stdc++.h>
#define ll long long
#define ff first
#define ss second
#define pb push_back
using namespace std;
const int N = 1e6 + 5;
int n, m, t;
int w[N], P[N];
vector<int> gr[N];
vector<pair<int,int>> L;
vector<int> path;
void dfs(int x, int p){
P[x] = p;
for(auto to : gr[x]){
if(to == p) continue;
dfs(to, x);
}
}
int CNT;
void count_w(int x, int cnt, int dep){
if(gr[x].size() == 1){
w[x] = cnt + dep;
return ;
}
cnt += gr[x].size() - 2;
vector<int>v;
for(int to : gr[x]){
if(to == P[x]) continue;
count_w(to, cnt, dep+1);
v.pb(w[to]);
}
cnt -= gr[x].size() - 2;
sort(v.rbegin(),v.rend());
if(v.size() == 1){
w[x] = 1 + dep + cnt;
} else {
w[x] = v[1];
}
}
inline bool check(int x){
int B = 0;
for(int i = 0; i < L.size(); i++){
auto p = L[i];
//if(i == L.size() - 1) return p.ss + B <= x;
if(B >= p.ff){
if(p.ss + B > x) return false;
} else
if(p.ss + B > x){
B++;
}
if(B > x) return false;
}
return true;
}
int main(){
//ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> t >> m;
for(int i = 1; i < n; i++){
int x, y;
//cin >> x >> y;
scanf("%d%d",&x,&y);
gr[x].pb(y);
gr[y].pb(x);
}
dfs(t,0);
int ver = m;
while(ver != t){
path.pb(ver);
ver = P[ver];
}
reverse(path.begin(), path.end());
for(int i = 0; i < path.size(); i++){
int x = path[i];
int k = 3;
if(i == path.size() - 1) k--;
CNT += max(0, (int)gr[x].size() - k);
//cout << CNT << endl;
for(int to : gr[x]){
if(to == P[x]) continue;
if(path.size() - 1 != i && to == path[i+1]) continue;
count_w(to, CNT, 1);
L.pb({path.size() - i, w[to]});
}
if((int)gr[x].size() >= 3)CNT++;
}
//for(int i = 1; i <= n; i++) cout << w[i] << " "; cout << endl;
sort(L.begin(), L.end());
//for(auto p : L) cout << p.ff << " " << p.ss << endl;
int l = 0, r = 1e6, answer = -1;
while(l <= r) {
int mid = l+r; mid >>= 1;
if(check(mid)){
answer = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
cout << answer << endl;
}
Compilation message
mousetrap.cpp: In function 'bool check(int)':
mousetrap.cpp:50:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < L.size(); i++){
~~^~~~~~~~~~
mousetrap.cpp: In function 'int main()':
mousetrap.cpp:88:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < path.size(); i++){
~~^~~~~~~~~~~~~
mousetrap.cpp:91:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(i == path.size() - 1) k--;
~~^~~~~~~~~~~~~~~~~~
mousetrap.cpp:96:44: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(path.size() - 1 != i && to == path[i+1]) continue;
~~~~~~~~~~~~~~~~^~~~
mousetrap.cpp:73:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&x,&y);
~~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
23808 KB |
Output is correct |
2 |
Correct |
14 ms |
23808 KB |
Output is correct |
3 |
Correct |
20 ms |
23808 KB |
Output is correct |
4 |
Correct |
19 ms |
23808 KB |
Output is correct |
5 |
Correct |
14 ms |
23808 KB |
Output is correct |
6 |
Correct |
17 ms |
23808 KB |
Output is correct |
7 |
Correct |
14 ms |
23808 KB |
Output is correct |
8 |
Correct |
17 ms |
23808 KB |
Output is correct |
9 |
Correct |
18 ms |
23808 KB |
Output is correct |
10 |
Correct |
15 ms |
23808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
489 ms |
62968 KB |
Output is correct |
2 |
Correct |
435 ms |
59000 KB |
Output is correct |
3 |
Correct |
1081 ms |
64248 KB |
Output is correct |
4 |
Correct |
498 ms |
44024 KB |
Output is correct |
5 |
Correct |
1075 ms |
64088 KB |
Output is correct |
6 |
Correct |
1108 ms |
64248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
23808 KB |
Output is correct |
2 |
Correct |
14 ms |
23808 KB |
Output is correct |
3 |
Correct |
20 ms |
23808 KB |
Output is correct |
4 |
Correct |
19 ms |
23808 KB |
Output is correct |
5 |
Correct |
14 ms |
23808 KB |
Output is correct |
6 |
Correct |
17 ms |
23808 KB |
Output is correct |
7 |
Correct |
14 ms |
23808 KB |
Output is correct |
8 |
Correct |
17 ms |
23808 KB |
Output is correct |
9 |
Correct |
18 ms |
23808 KB |
Output is correct |
10 |
Correct |
15 ms |
23808 KB |
Output is correct |
11 |
Correct |
15 ms |
23808 KB |
Output is correct |
12 |
Correct |
15 ms |
23808 KB |
Output is correct |
13 |
Correct |
15 ms |
23808 KB |
Output is correct |
14 |
Correct |
15 ms |
23936 KB |
Output is correct |
15 |
Correct |
15 ms |
23936 KB |
Output is correct |
16 |
Correct |
16 ms |
23936 KB |
Output is correct |
17 |
Correct |
15 ms |
23808 KB |
Output is correct |
18 |
Correct |
15 ms |
23808 KB |
Output is correct |
19 |
Correct |
15 ms |
23808 KB |
Output is correct |
20 |
Correct |
15 ms |
23808 KB |
Output is correct |
21 |
Correct |
16 ms |
23808 KB |
Output is correct |
22 |
Correct |
15 ms |
23808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
23808 KB |
Output is correct |
2 |
Correct |
14 ms |
23808 KB |
Output is correct |
3 |
Correct |
20 ms |
23808 KB |
Output is correct |
4 |
Correct |
19 ms |
23808 KB |
Output is correct |
5 |
Correct |
14 ms |
23808 KB |
Output is correct |
6 |
Correct |
17 ms |
23808 KB |
Output is correct |
7 |
Correct |
14 ms |
23808 KB |
Output is correct |
8 |
Correct |
17 ms |
23808 KB |
Output is correct |
9 |
Correct |
18 ms |
23808 KB |
Output is correct |
10 |
Correct |
15 ms |
23808 KB |
Output is correct |
11 |
Correct |
489 ms |
62968 KB |
Output is correct |
12 |
Correct |
435 ms |
59000 KB |
Output is correct |
13 |
Correct |
1081 ms |
64248 KB |
Output is correct |
14 |
Correct |
498 ms |
44024 KB |
Output is correct |
15 |
Correct |
1075 ms |
64088 KB |
Output is correct |
16 |
Correct |
1108 ms |
64248 KB |
Output is correct |
17 |
Correct |
15 ms |
23808 KB |
Output is correct |
18 |
Correct |
15 ms |
23808 KB |
Output is correct |
19 |
Correct |
15 ms |
23808 KB |
Output is correct |
20 |
Correct |
15 ms |
23936 KB |
Output is correct |
21 |
Correct |
15 ms |
23936 KB |
Output is correct |
22 |
Correct |
16 ms |
23936 KB |
Output is correct |
23 |
Correct |
15 ms |
23808 KB |
Output is correct |
24 |
Correct |
15 ms |
23808 KB |
Output is correct |
25 |
Correct |
15 ms |
23808 KB |
Output is correct |
26 |
Correct |
15 ms |
23808 KB |
Output is correct |
27 |
Correct |
16 ms |
23808 KB |
Output is correct |
28 |
Correct |
15 ms |
23808 KB |
Output is correct |
29 |
Correct |
24 ms |
23808 KB |
Output is correct |
30 |
Correct |
488 ms |
62896 KB |
Output is correct |
31 |
Correct |
469 ms |
62968 KB |
Output is correct |
32 |
Correct |
474 ms |
110188 KB |
Output is correct |
33 |
Correct |
564 ms |
188220 KB |
Output is correct |
34 |
Correct |
1389 ms |
63992 KB |
Output is correct |
35 |
Correct |
1401 ms |
63992 KB |
Output is correct |
36 |
Correct |
1076 ms |
70936 KB |
Output is correct |
37 |
Correct |
1284 ms |
70892 KB |
Output is correct |
38 |
Correct |
822 ms |
72708 KB |
Output is correct |
39 |
Correct |
753 ms |
72556 KB |
Output is correct |
40 |
Correct |
825 ms |
72692 KB |
Output is correct |