Notes![what is notes.io? What is notes.io?](/theme/images/whatisnotesio.png)
![]() ![]() Notes - notes.io |
#define ll long long
#define pb push_back
#define loop(a,n) for(ll i = a; i < n; i++)
#define loop2(a,n) for(ll j = a; j < n; j++)
#define trace(x) for(auto it:x)
using namespace std;
const ll MAX = 1e5+5;
const ll MOD = 1e9 + 7;
const ll inf = 1e18;
const ll level = 18;
vector<ll>adj[MAX];
ll par[MAX] = {}, sum[MAX] = {};
ll vis[MAX] = {};
ll rnk[MAX]= {};
ll depth[MAX] = {};
ll parent[MAX][level];
void dfs(int u, int v){
par[u] = v;
depth[u] = depth[v] + 1;
parent[u][0] = v;
vis[u] = 1;
trace(adj[u]){
if(!vis[it]){
dfs(it, u);
}
}
}
int LCA(int u, int v)
{
if(depth[u] < depth[v])
return LCA(v, u);
int diff = depth[u] - depth[v];
for (int i = 0; i < 15; ++i)
{
if((diff>>i) & 1)
u = parent[u][i];
}
if(u != v)
{
for (int i = 14; i >= 0; --i)
{
if(parent[u][i] != parent[v][i])
{
u = parent[u][i];
v = parent[v][i];
}
}
u = parent[u][0];
}
return u;
}
void solve(int v){
trace(adj[v]){
if(it - par[v])
{
solve(it);
sum[v] += sum[it];
}
}
}
void bfs(ll v){
queue<ll>q;
q.push(v);
rnk[v] = 1;
while(!q.empty()){
ll u = q.front();
vis[u] = 1;
q.pop();
// cout<<u<<endl;
trace(adj[u]){
if(it - par[u])
{
rnk[it] = rnk[u] + 1;
q.push(it);
}
}
}
}
int main() {
memset(&parent,0,sizeof(parent));
ll n, m;
cin>>n>>m;
loop(0, n - 1){
ll u, v;
cin>>u>>v;
adj[u].pb(v);
adj[v].pb(u);
}
dfs(1, 0);
//memset(&vis, 0, sizeof vis);
for(int i = 0; i < 15; ++i){
for (int j = 1; j <= n; ++j)
{
if(parent[j][i] != 0)
parent[j][i+1]=parent[parent[j][i]][i];
}
}
bfs(1);
//loop(1, 2 + n)cout<<rnk[i]<<" ";
//cout<<endl;
loop(0, m){
ll x, y;
cin>>x>>y;
ll lca = LCA(x,y);
if(rnk[x] > rnk[y])swap(x,y);
// cout<<x<<" "<<y<<endl;
if(lca != x && lca != y){
sum[x]++;
sum[y]++;
sum[lca]--;
sum[par[lca]]--;
}else{
sum[y]++;
sum[par[x]]--;
}
}
solve(1);
//loop(1, 1 + n)cout<<sum[i]<<" ";
ll ans = 0;
//cout<<endl;
loop(1, 1 + n)ans = max(ans, sum[i]);
cout<<ans;
return 0;
}
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team