【题目链接】

点击打开链接

【算法】

LCA + 树上差分

【代码】

#include<bits/stdc++.h>

using namespace std;

int i,x,y,N,K,l,maxn;
int anc[][],d[],sum[];
vector<int> E[]; void build(int x,int pre) {
int i;
for (i = ; i <= ; i++) anc[x][i] = anc[anc[x][i-]][i-];
for (i = ; i < E[x].size(); i++) {
if (E[x][i] != pre) {
anc[E[x][i]][] = x;
d[E[x][i]] = d[x] + ;
build(E[x][i],x);
}
}
} int lca(int x,int y) {
int i,f;
if (d[x] > d[y]) swap(x,y);
f = d[y] - d[x];
for (i = ; i <= ; i++) {
if (f & ( << i))
y = anc[y][i];
}
if (x == y) return x;
for (i = ; i >= ; i--) {
if (anc[x][i] != anc[y][i]) {
x = anc[x][i];
y = anc[y][i];
}
}
return anc[x][];
} void query(int x,int pre) {
int i;
for (i = ; i < E[x].size(); i++) {
if (E[x][i] != pre) {
query(E[x][i],x);
sum[x] += sum[E[x][i]];
}
}
maxn = max(maxn,sum[x]);
} int main() { cin >> N >> K;
for (i = ; i < N; i++) {
cin >> x >> y;
E[x].push_back(y);
E[y].push_back(x);
} build(,); for (i = ; i <= K; i++) {
cin >> x >> y;
l = lca(x,y);
sum[x]++; sum[y]++;
sum[l]--;
if (anc[l][]) sum[anc[l][]]--;
} query(,); cout<< maxn << endl; return ;
}

【USACO】 Max Flow的更多相关文章

  1. POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 【USACO】距离咨询(最近公共祖先)

    POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 [USACO]距离咨询(最近公共祖先) Description F ...

  2. 【CF708D】Incorrect Flow 最小费用可行流

    [CF708D]Incorrect Flow 题意:给你一个点数为n,边数为m的流网络,每条边有一个容量c和流量f,这个网络可能是不合法的.你可以花费1的代价使c或f减少或增加1,可以修改无限次.你不 ...

  3. 1642: 【USACO】Payback(还债)

    1642: [USACO]Payback(还债) 时间限制: 1 Sec 内存限制: 64 MB 提交: 190 解决: 95 [提交] [状态] [讨论版] [命题人:外部导入] 题目描述 &quo ...

  4. 1519: 【USACO】超级书架

    1519: [USACO]超级书架 时间限制: 1 Sec 内存限制: 64 MB 提交: 1735 解决: 891 [提交] [状态] [讨论版] [命题人:外部导入] 题目描述 Farmer Jo ...

  5. Java实现【USACO】1.1.2 贪婪的礼物送礼者 Greedy Gift Givers

    [USACO]1.1.2 贪婪的礼物送礼者 Greedy Gift Givers 题目描述 对于一群要互送礼物的朋友,你要确定每个人送出的礼物比收到的多多少(and vice versa for th ...

  6. 【USACO】Optimal Milking

    题目链接 :        [POJ]点击打开链接        [caioj]点击打开链接 算法 : 1:跑一遍弗洛伊德,求出点与点之间的最短路径 2:二分答案,二分”最大值最小“ 3.1:建边,将 ...

  7. 【USACO】 Balanced Photo

    [题目链接] 点击打开链接 [算法] 树状数组 [代码] #include<bits/stdc++.h> using namespace std; int i,N,ans,l1,l2; ] ...

  8. 【USACO】 Balanced Lineup

    [题目链接] 点击打开链接 [算法] 这是一道经典的最值查询(RMQ)问题. 我们首先想到线段树.但有没有更快的方法呢?对于这类问题,我们可以用ST表(稀疏表)算法求解. 稀疏表算法.其实也是一种动态 ...

  9. 【USACO】 奶牛政坛

    [题目链接] 点击打开链接 [算法] tarjan算法求LCA [代码] #include<bits/stdc++.h> #define MAXN 200010 #pragma GOC o ...

随机推荐

  1. codeforces #472(div 1)

    B(two point) 题意: 给出长度为n的非递减数组E[1..n],对于所有三元组(i,j,k),1<=i<j<k<=n且Ek-Ei<=U,我们需要计算出最大的(E ...

  2. spring解决乱码

    spring提供的工具类解决乱码问题 在web.xml配置中添加如下代码: <!--乱码处理--> <filter> <filter-name>encodingFi ...

  3. cef network-settings

    Network Settings 目录 1 System network settings 2 Preference service for network settings 3 Command-li ...

  4. IntelliJ IDEA设置properties文件显示中文

    配置这里: 注意:上面是Default Settings,还需要在Settings中设置成上面一样的.

  5. Linux下使用Shell命令控制任务Jobs执行(转)

    一.下列命令可以用来操纵进程任务: ps列出系统中正在运行的进程. kill发送信号给一个或多个进程(经常用来杀死一个进程). jobs列出当前shell环境中已启动的任务状态,若未指定jobsid, ...

  6. windows 下QT5.5+vs2013开发环境搭建

    开发环境搭建: 1.下载QT,下载的网址如下: http://download.qt.io/official_releases/vsaddin/ http://download.qt.io/offic ...

  7. Object中的wait,notify,notifyAll基本使用(转)

    让线程停止运行/睡眠的方法只有两个:Thread.sleep()或者obj.wait() 记住obj.nofity()并不能停止线程运行,因为notify虽然释放了锁,但依然会急促执行完synchro ...

  8. 【hibernate】报错:org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.DataException: could not execute statement

    报错如下: org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a ...

  9. 【转】C语言中整型运算取Ceiling问题

    原文:http://blog.csdn.net/laciqs/article/details/6662472 --------------------------------------------- ...

  10. Android 特别大的Activity和Fragment的生命周期图

    这么 这么大的图.不做太多解释,哈哈,真的是棒棒的. 代码測试下载:http://download.csdn.net/detail/pcaxb/8906085