#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define debug(x) cout << #x << "= " << x << ", "
#define ll long long
#define fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define SZ(x) (int) x.size()
#define wall cout << endl;
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 1e6 + 10;
int n , dp[maxn] , m , t , par[maxn];
vector <int> adj[maxn];
bool vis[maxn];
struct amirhoseinfar1385team{
int cnt[(maxn << 2)] , col[(maxn << 2)];
void Add(int ind , int node = 1 , int nl = 0 , int nr = n)
{
cnt[node]++;
if(nr - nl == 1) return;
int mid = (nr + nl) >> 1 , lc = node << 1 , rc = lc | 1;
if(ind < mid) Add(ind , lc , nl , mid);
else Add(ind , rc , mid , nr);
}
int Find_mx(int num , int node = 1 , int nl = 0 , int nr = n)
{
if(nr - nl == 1) return nl;
int mid = (nl + nr) >> 1 , lc = node << 1 , rc = lc | 1;
if(num <= cnt[rc]) return Find_mx(num , rc , mid , nr);
else return Find_mx(num - cnt[rc] , lc , nl , mid);
}
int Find_col(int ind , int node = 1 , int nl = 0 , int nr = n)
{
if(nr - nl == 1) return col[node];
int mid = (nr + nl) >> 1 , lc = node << 1 , rc = lc | 1;
if(ind < mid) return col[rc] + Find_col(ind , lc , nl , mid);
else return Find_col(ind , rc , mid , nr);
}
void Add_col(int ind , int node = 1 , int nl = 0 , int nr = n)
{
col[node]++;
if(nr - nl == 1) return;
int mid = (nr + nl) >> 1 , lc = node << 1 , rc = lc | 1;
if(ind < mid) Add_col(ind , lc , nl , mid);
else Add_col(ind , rc , mid , nr);
}
} seg;
void dfs(int v)
{
vector <int> vec = {0 , 0};
for(auto u : adj[v]) if(u != par[v])
{
par[u] = v;
dfs(u);
vec.pb(dp[u]);
dp[v]++;
}
sort(vec.rbegin() , vec.rend());
dp[v] += vec[1];
}
int32_t main()
{
fast;
cin >> n >> t >> m;
for(int i = 0 ; i < n - 1 ; i++)
{
int v , u; cin >> v >> u;
adj[v].pb(u);
adj[u].pb(v);
}
dfs(t);
int now = m , sum_deg = 0 , prev = 1;
vis[t] = true;
vector <int> path;
while(now != t)
{
vis[now] = true;
path.pb(now);
sum_deg += (SZ(adj[now]) - prev);
prev = 2;
now = par[now];
}
int ans = 0;
for(int i = 0 ; i < SZ(path) ; i++)
{
int deg_now = 0 , mxc = -1 , v = path[i];
for(auto u : adj[v]) if(!vis[u])
{
deg_now++;
seg.Add(dp[u]);
mxc = max(mxc , dp[u]);
}
int mx = 0;
if(i + 2 <= seg.cnt[1]) mx = seg.Find_mx(i + 2);
int tmp = seg.Find_col(mx);
ans = max(ans , sum_deg + tmp + mx);
sum_deg -= deg_now;
if(mxc != -1)
seg.Add_col(mxc);
}
cout << ans << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
13 ms |
23832 KB |
Output is correct |
4 |
Correct |
11 ms |
23828 KB |
Output is correct |
5 |
Correct |
12 ms |
23816 KB |
Output is correct |
6 |
Correct |
11 ms |
23764 KB |
Output is correct |
7 |
Correct |
13 ms |
23800 KB |
Output is correct |
8 |
Correct |
13 ms |
23828 KB |
Output is correct |
9 |
Correct |
13 ms |
23784 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
314 ms |
63064 KB |
Output is correct |
2 |
Correct |
303 ms |
59080 KB |
Output is correct |
3 |
Correct |
785 ms |
64060 KB |
Output is correct |
4 |
Correct |
346 ms |
43932 KB |
Output is correct |
5 |
Correct |
840 ms |
64036 KB |
Output is correct |
6 |
Correct |
815 ms |
64032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
13 ms |
23832 KB |
Output is correct |
4 |
Correct |
11 ms |
23828 KB |
Output is correct |
5 |
Correct |
12 ms |
23816 KB |
Output is correct |
6 |
Correct |
11 ms |
23764 KB |
Output is correct |
7 |
Correct |
13 ms |
23800 KB |
Output is correct |
8 |
Correct |
13 ms |
23828 KB |
Output is correct |
9 |
Correct |
13 ms |
23784 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
11 ms |
23772 KB |
Output is correct |
12 |
Correct |
11 ms |
23836 KB |
Output is correct |
13 |
Correct |
11 ms |
23872 KB |
Output is correct |
14 |
Correct |
12 ms |
24020 KB |
Output is correct |
15 |
Correct |
15 ms |
23964 KB |
Output is correct |
16 |
Correct |
12 ms |
23892 KB |
Output is correct |
17 |
Incorrect |
12 ms |
23888 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
13 ms |
23832 KB |
Output is correct |
4 |
Correct |
11 ms |
23828 KB |
Output is correct |
5 |
Correct |
12 ms |
23816 KB |
Output is correct |
6 |
Correct |
11 ms |
23764 KB |
Output is correct |
7 |
Correct |
13 ms |
23800 KB |
Output is correct |
8 |
Correct |
13 ms |
23828 KB |
Output is correct |
9 |
Correct |
13 ms |
23784 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
314 ms |
63064 KB |
Output is correct |
12 |
Correct |
303 ms |
59080 KB |
Output is correct |
13 |
Correct |
785 ms |
64060 KB |
Output is correct |
14 |
Correct |
346 ms |
43932 KB |
Output is correct |
15 |
Correct |
840 ms |
64036 KB |
Output is correct |
16 |
Correct |
815 ms |
64032 KB |
Output is correct |
17 |
Correct |
11 ms |
23772 KB |
Output is correct |
18 |
Correct |
11 ms |
23836 KB |
Output is correct |
19 |
Correct |
11 ms |
23872 KB |
Output is correct |
20 |
Correct |
12 ms |
24020 KB |
Output is correct |
21 |
Correct |
15 ms |
23964 KB |
Output is correct |
22 |
Correct |
12 ms |
23892 KB |
Output is correct |
23 |
Incorrect |
12 ms |
23888 KB |
Output isn't correct |
24 |
Halted |
0 ms |
0 KB |
- |