问题 I: Monitoring Ski Paths 树链剖分+LCA+树状数组+贪心
问题 I: Monitoring Ski Paths
时间限制: 1 Sec 内存限制: 128 MB
[提交] [状态] [命题人:admin]
题目描述
Fresh powder on a sunny day: it is a great time to ski! Hardcore skiers flock to a large mountain in the Rockies to enjoy these perfect conditions. The only way up the mountain is by helicopter; skiers jump out and ski down the mountain.
This process sounds a bit chaotic, so some regulations are in place. Skiers can only enter or exit the mountain at a set of designated locations called junctions. Once on the mountain, they are only allowed to travel along designated ski paths, each of which starts at one junction and ends at another junction lower on the mountain. Multiple ski paths might start at the same junction, but no two ski paths end at the same junction to avoid collisions between skiers.
Finally, each skier must register a ski plan a day in advance with the helicopter service. The ski plan specifies the junction they fly up to and the junction lower on the mountain where they get picked up. If a skier shows up to the mountain, they must follow their plan; but some skiers get sick and do not show up at all.
Your job is to look through the ski plans and set up monitors at some of the junctions to count how many skiers actually show up. To keep operating costs as low as possible, you should determine the minimum number of junctions that need to be monitored so that each skier passes through at least one monitored junction.
Figure 1: Illustration of the first sample input.
Figure 1 shows the first sample input. The dashed lines indicate the five different plans that were registered by skiers. By monitoring junctions 5, 9, and 10, you can ensure that all plans include at least one monitored junction. Monitoring fewer functions would miss some skiers.
输入
The first line of input has three integers n, k, and m, where n (2≤n≤250000) is the number of junctions, k (1≤k<n) is the number of ski paths, and m (1≤m≤250000) is the number of routes.
Then k lines follow, each containing two integers 1≤u,v≤n indicating that there is a ski path that starts at junction u and ends at junction v. No (u,v) pair appears more than once.
Then m lines follow, each containing two distinct integers 1≤s,t≤n. Each line indicates that a skier plans to land at junction s and ski down the mountain to junction t. It is guaranteed it is possible to reach junction t from junction s by following ski paths and that junction t is at the base of the mountain (i.e. no ski paths start at t). No (s,t) pair appears more than once.
输出
Output the minimum number of junctions that need to be monitored so each ski plan includes at least one monitored junction.
样例输入
复制样例数据
10 8 5 1 5 5 6 5 4 7 3 3 10 3 9 10 2 10 8 1 6 5 4 7 2 3 9 10 8
样例输出
3
题解:这个题和 HDU6203 几乎一样,唯一不同的就是,这个不一定是连通图,那么就并查集把他们都连起来算就好了
HDU6203:https://blog.csdn.net/mmk27_word/article/details/95198205
#include <bits/stdc++.h>
using namespace std;
const int N = 250000 + 10;
struct Edge {int to, nex;
}e[N * 2];
int n, p, m ;
int head[N], len;
int deep[N];
int in[N], out[N], cnt;
int sum[N];
int num[N], son[N], top[N], f[N];
int ff[N];
void Init() {for(int i = 0; i <= n; i++) {head[i] = -1;deep[i] = -1;num[i] = 0;son[i] = -1;top[i] = -1;f[i] = -1;sum[i] = 0;ff[i] = i;}sum[n + 1] = 0;len = 0;cnt = 0;
}
void Addedge(int x, int y) {e[len].to = y;e[len].nex = head[x];head[x] = len++;
}
void dfs1(int u, int fa) {f[u] = fa;int to;num[u] = 1;for(int i = head[u]; i != -1; i = e[i].nex) {to = e[i].to;if(to == fa) continue;deep[to] = deep[u] + 1;dfs1(to, u);num[u] += num[to];if(son[u] == -1 || num[to] > num[son[u]])son[u] = to;}
}
void dfs2(int u, int rt, int f) {top[u] = rt;in[u] = ++cnt; if(son[u] != -1) {dfs2(son[u], rt, u);}int to;for(int i = head[u]; i != -1; i = e[i].nex) {to = e[i].to;if(to == f || to == son[u]) continue;dfs2(to, to, u);}}
struct Node {int x, y, lca;
}b[N ];
int getlca(int x, int y) {int fx = top[x], fy = top[y];while(fx != fy) {if(deep[fx] < deep[fy]) {swap(fx, fy);swap(x, y);}x = f[fx];fx = top[x];}if(deep[x] < deep[y]) swap(x, y);return y;
}bool cmp(Node x, Node y) {return deep[x.lca] > deep[y.lca];
}int lowbit(int x) {return x & (- x);
}
void AddPoint(int x) {while(x <= n + 1) {sum[x]++;x +=lowbit(x);}return ;
}
int query(int x) {
// if(x == 0) return 0;int res = 0;while(x) {// cout<<x<<endl;res += sum[x];x -= lowbit(x);}return res;
}
int judge(int x, int y) {int fx = top[x], fy = top[y];while(fx != fy) {if(deep[fx] < deep[fy]) {swap(fx, fy);swap(x, y);}if(query(in[x]) - query(in[fx] - 1) != 0) return true;x = f[fx];fx = top[x];}if(deep[x] < deep[y]) swap(x, y);if(query(in[x]) - query(in[y] - 1) != 0) return true;return false;
}
int fath(int x) {return x == ff[x] ? ff[x] : ff[x] = fath(ff[x]);
}
int main() {int x, y, z;int ans, cnt;int xx, yy;while(~scanf("%d %d %d", &n, &m, &p)) {Init();for(int i = 1; i <= m; i++) {scanf("%d %d", &x, &y);xx = fath(x); yy = fath(y);ff[yy] = xx;Addedge(x, y);Addedge(y, x);}x = fath(1);for(int i = 2; i <= n; i++) {y = fath(i);if(x == y) continue;// cout << x <<" * " << y << endl;ff[y] = x;Addedge(x, y);Addedge(y, x);}// cout <<"WJY" << endl;deep[x] = 1;dfs1(x, -1);// cout <<"ZX" << endl;dfs2(x, x, -1);// cout << "xjl" << endl;for(int i = 1; i <= p; i++) {scanf("%d %d", &x, &y);z = getlca(x, y);b[i].x = x;b[i].y = y;b[i].lca = z;// cout<<z<<endl;// cout << in[x] << " " << in[y] << endl;}sort(b + 1, b + 1 + p, cmp);ans = 0;for(int i = 1; i <= p; i++ ) {if(!judge(b[i].x, b[i].y) ) {ans++;AddPoint(in[b[i].lca]);}}printf("%d\n", ans);}return 0;
}
相关文章:

POJ - 3580 SuperMemo 伸展树
题目链接:https://cn.vjudge.net/problem/POJ-3580#author0 伸展树学习: 很全:https://blog.csdn.net/qq_33184171/article/details/73549164 图:https://blog.csdn.net/wr132/article/details/50599747 效率:http…...

3224: Tyvj 1728 普通平衡树 伸展树
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id3224 题解:灵活运用,像这种不对区间操作的,相同的数放到一块就可以了 #include <bits/stdc.h> using namespace std; #define INF 2e9 10 const int N 1e5 …...

HDU - 1890 Robotic Sort 伸展树
题目链接:https://cn.vjudge.net/problem/HDU-1890 题意:每次将第i个位置到第i大的数所在位置 之间的数进行翻转,输出的是第i大的数所在的位置 题解:按照1-n建树,原序列排序,每次把节点 a[i].id 放到顶部…...

2019百度之星 - 复赛 HDU-6725 Diversity 树形dp
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid6725 题解:每个节点要么取l[i],要么取r[i],对于每个节点维护下取最小最大值时的结果即可 #include <bits/stdc.h> using namespace std; typedef long long ll; cons…...

2019百度之星 - 复赛 HDU-6726 Transformation 搜索剪枝
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid6726 题解:我们倒着往前找,对于(a,2b-a)->(x,y)那么xy为偶数,(x, y) 往前的话就是(x, (xy)/2)&…...

2019百度之星 - 复赛 HDU-6727 Quasi Binary Search Tree 构造 二叉树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid6727 题解:对于当前节点,肯定分为两个区间一个在左一个在右,就把小区间分到最小编号在的那个分支中,如果当前节点最小,那就尽可能让当前节点的标号小…...

2019 南京网络赛 A.The beautiful values of the palace 构造螺旋矩阵 + CDQ / 离线 + 树状数组
题目链接:https://nanti.jisuanke.com/t/41298 题解:首先我们要把每个坐标的val求出来,很明显每一圈的个数是一个等差数列,第x圈 a[x] -8x4n4,然后就是先等差前n项和求出之前每一圈的总个数,然后对于x 与…...

2018北京网络赛 HihoCoder - 1835 K-Dimensional Foil II 计算几何 贪心 二分
题目链接:https://vjudge.net/problem/HihoCoder-1835 题解:首先我们应该能想到到达图形的距离最近那肯定是垂直过去,也就是坐标变为(x1 - k, x2 - k, x3 - k...),假设图形原点的坐标为(x10, x20, x30 ...),如果某个x…...
2019南京网络赛 D.Robots 树上期望dp
题目链接:https://nanti.jisuanke.com/t/41301 题意:N个点M条边的有向无环图,1为唯一入度为0的点,N为唯一出度为0的点,现在你从1号点出发,每单位时间,你有相同的概率,走向下一节点或…...

bzoj1568: [JSOI2008]Blue Mary开公司 李超线段树
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id1568 李超线段树: 问题: 现在要求你在线动态维护一个二维平面直角坐标系,支持插入一条线段,询问与直线xx0相交的所有线段中交点y的最大/最小值 更新&a…...
牛客练习赛51 C、勾股定理 只一边求另外两边 结论
链接:https://ac.nowcoder.com/acm/contest/1083/C 来源:牛客网 勾股定理 时间限制:C/C 1秒,其他语言2秒 空间限制:C/C 32768K,其他语言65536K Special Judge, 64bit IO Format: %lld 题目描述 给出直…...

2019徐州网络赛 G Colorful String 马拉车+后缀
题目链接:https://nanti.jisuanke.com/t/41389 题解:马拉车处理回文串,每个位置记录后面每个字符出现的第一个位置,然后把每种字符额贡献加起来即可 #include<bits/stdc.h> using namespace std; typedef long long ll; c…...

2019徐州网络赛 I query 线段树 离线
题目链接:https://nanti.jisuanke.com/t/41391 题解:先找出每一对(pi,pj),那么i, j 记录为 区间[l, r],则查询就变为,在[L,R]内有多少区间,然后我们离线线段树就可以搞定了,按照…...

2019徐州网络赛 J、Random Access Iterator 树上 概率 dp
题目链接:https://nanti.jisuanke.com/t/41392 题解:dp[u] 表示 u这个点向下能到达最深深度的概率,因为对于每个点要找k次,只要其中有一次能到达最深即可,所以我们用容斥的思想,即表示为 1 - 不能到达最深…...

2019银川网络赛A? FZU - 2105 Digits Count 线段树 区间操作 | ^
题目链接:https://vjudge.net/problem/FZU-2105 其实我想放银川网络赛的链接,emmm 题解:首先或和与是能相互掩盖的,因此我们就可以把这两者用一个laz标记,laz1表示区间都变为1,laz-1表示区间都为0&#x…...

2019 徐州网络赛 F. Little M's attack plan 树上倍增+dfs序+二分
题目链接:https://nanti.jisuanke.com/t/41388 题解:这个题比赛最后也没改对,训练时突然想到有个地方写了emmmm 思路:我们先按照dfs序把每个点跑出来,然后vector记录下每个深度的点,很容易就可以想到&…...

HDU - 5489 Removed Interval LIS+枚举技巧
题目链接:https://vjudge.net/problem/HDU-5489 题解:删除m个后的最优的LIS在原序列中肯定存在一段间距大于等于m,那么我们就以这一段作为划分线,以后面第一个数x开始作为后半部分,那么后半部分的LIS我们可以倒着nlo…...

2019 南昌网络赛 C Hello 2019 / CodeForces - 750ENew Year and Old Subsequence 线段树维护矩阵
题目链接:https://vjudge.net/problem/CodeForces-750E 题解:哇,原来这个也四个原题。。。。。。维护矩阵的还真是第一次做,这里就说一下CF那题的思路吧 因为最后要2017,那么我们分为5个子序列 空 2 20 201 2017 每…...

构造螺旋矩阵 n*m
构造n * m的螺旋矩阵 讲解:https://wenku.baidu.com/view/7253dc78a26925c52cc5bff4.htm #include <bits/stdc.h> using namespace std; int getnum(int n, int m, int x, int y) {int lev min(min(x, n - 1 - x), min(y, m - 1 - y));int d x y - lev *…...

2019 上海网络赛 B、 Light bulbs 区间差分 / 线段树点维护区间
题目链接:https://nanti.jisuanke.com/t/41399 题解:看见的第一思路应该都是差分,但是这个用数组肯定就gg了,所以当时的时候就想到动态开点线段树,mlog(n)但是T了,然后我就把点离散了一下,用点…...

2019 上海网络赛 G、Substring 字符串哈希
题目链接:https://nanti.jisuanke.com/t/41415 题解:对于两边的,要哈希出相应的权重,中间的字符顺序无所谓,只关心是什么字符,所以哈希出每个字符的权重即可。 官方题解: • 但是因为询问有2…...

2019 沈阳网络赛 H. Texas hold'em Poker 模拟
题目链接:https://nanti.jisuanke.com/t/41408 细心细心细心 #include <bits/stdc.h> using namespace std; const int N 1e5 10; struct node {char name[15];int id;int b[5]; }p[N]; int n; int a[10]; bool judge1() {for(int i 1; i < 5; i) {if…...

2019 沈阳网络赛 B. Dudu's maze dfs 联通块选取
题目链接:https://nanti.jisuanke.com/t/41402 题解:首先和1联通的糖果屋都能到达,其他不能到达的肯定被恶魔屋挡住了,那就在挡住的所以恶魔屋中选择一个最优的就可以了,怎么算最优呢,因为到达恶魔屋之后&…...

51nod 1571 最近等对 CQD分治
题目链接:https://www.51nod.com/Challenge/Problem.html#problemId1571 现在有一个序列 a1, a2, ..., ana1, a2, ..., an ,还有m个查询 lj, rj (1 ≤ lj ≤ rj ≤ n)lj, rj (1 ≤ lj ≤ rj ≤ n) 。对于每一个查询,请找出距离最近的两个元素…...

牛客 Fruit Ninja 随机大法
链接:https://ac.nowcoder.com/acm/contest/163/A 来源:牛客网 题目描述 Fruit Ninja is a juicy action game enjoyed by millions of players around the world, with squishy, splat and satisfying fruit carnage! Become the ultimate bringer of…...

HDU - 6242 Geometry Problem 随机数
题目链接:https://vjudge.net/problem/HDU-6242 题意:n个点,找到一个圆,满足圆上点的数目大于等于⌈N / 2⌉ 题解:三个不在一条直线上的点确定一个圆,随机到圆上点的概率是1 / 2,那么三次就是…...

HDU - 6393 Traffic Network in Numazu 树上倍增求lca+dfs序+线段树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid6393 题意:n个点n条边的图,求两点间的最小距离 题解:如果是一个树的话,那么我们就可以倍增dfs序,然后用线段树维护下就可以了这样是nlogn的复杂度&a…...

CodeForces - 1220D Alex and Julian 二分图性质
题目链接:https://vjudge.net/problem/CodeForces-1220D 题意:n个数,如果节点 i 和 j 满足 |i - j | 属于B,则i和j有一条边,问B集合最少删除多少边,使得得到的图是个二分图 题解:根据二分图的…...

CodeForces - 1220E Tourism 无向图缩点+树形dp
题目链接:https://vjudge.net/problem/CodeForces-1220E 题意:n个点m条边的无向连通图,起点s,每个点有权值,求遍历无向图得到的最大权值和。但是不能走回头路,即如果从U走到V那么下一步不可以从V走到U 题…...

HDU - 6156 Palindrome Function 数位dp
题目链接:https://vjudge.net/problem/HDU-6156 题意:区间[L, R]的数,写成k进制(l<k<r)后,如果是回文串,那么f(x,k)k,否则f(x,k)1,对f求和 题解:一看这种题就是数位dp,当时想…...

HDU - 6153 A Secret 扩展kmp
题目链接:https://vjudge.net/problem/HDU-6153 题意:对于s2的每一个后缀,假设长度为l,在s1出现的次数为k,求l*k的和 题解:我们把两个串都倒过来,变为s1,s2,那么问题就变为&#x…...

Gym - 100589A Queries on the Tree 树状数组+分块
题目链接:https://vjudge.net/problem/Gym-100589A 题意:n个点,根节点为1的树,两种操作,1 L y 与根节点距离为L的节点权值全部加上y,2 x x子树的权值总和 题解:对于更新操作,因为更…...

HDU-6731 Angle Beats 计算几何 直角三角形个数
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid6731 题意:n个点,m次查询,每次给你一个点,从n个点挑出两个能组成多少直角三角形 题解:假设询问的是点p,直角可能是p也可能是其他两点的某…...

HDU - 5927 Auxiliary Set dfs序+瞎搞
题目链接:https://vjudge.net/problem/HDU-5927 题意:给一棵树,然后每次询问时给出哪些点是不重要的点,让求这棵树上重要的点以及是两个不同的重要的点的lca一共有多少个 题解:我们只需要看给出的每一个不重要的点是不是重要的点就可以了&a…...

CodeForces - 689D Friends and Subsequences ST表+二分
题目链接:https://codeforces.com/problemset/problem/689/D 题意:有多少区间[l, r]满足,max(al..ar) min(bl..br) 题解:对于先固定l,那么对于max(a),是一个递增的序列,对于min(b)是一个递减…...

2017南宁 I. Rake It In dfs博弈
题目链接:https://nanti.jisuanke.com/t/A1538 题解:因为k只有3,所以最多取6次,每次有9种选取可能,所以直接dfs选取即可 #include <bits/stdc.h> using namespace std; typedef long long ll; int b[6][6]; in…...

CodeForces - 369E Valera and Queries 树状数组+离线+容斥思想
题目链接:https://vjudge.net/problem/CodeForces-369E 题意:n条线段,m次询问,给出cnt个点,问这些点在多少线段上 题解:如果每个点对于所在的线段上没有影响的话,那么我们就可以建立权值线段树…...

Gym - 101889I Imperial roads 树链剖分+最小生成树之必选一边
题目链接:https://vjudge.net/problem/Gym-101889I 题意:n个点,m条边,q次询问,每次必须选一边,求最小生成树的权值 题解:我们先跑一边最小生成树,然后按照最小边建树,必…...

CodeForces - 1228C Primes and Multiplication 数贡献
题目链接:https://vjudge.net/problem/CodeForces-1228C 题解:把x的每个质因子找出来,计算n!对每个因子的贡献 #include <bits/stdc.h> using namespace std; typedef long long ll; const ll mod 1e9 7; const int N …...

CodeForces - 1228D Complete Tripartite 图上的哈希
题目链接:https://vjudge.net/problem/CodeForces-1228D 题解:我们把每个点哈希,这样就能计算出和每个点连接的其他点的总哈希值,若只有三种哈希值的和,那么就是符合的,当然还有其他很多种做法。 #includ…...

2017 ICPC Asia Urumqi I. A Possible Tree 带权并查集
题目链接:https://nanti.jisuanke.com/t/40520 题解:因为他们都是联通的且只有唯一路径,所以不用管之前怎么连的,直接按照他给的查询,带权并查集判断即可 #include <bits/stdc.h> using namespace std; const …...

HDU - 5923 Prediction 多个并查集
题目链接:https://vjudge.net/problem/HDU-5923 题解:因为n只有500个,所以我们可以每个点维护一个并查集,所以判断某些点的时候,这些并查集在合起来即可 #include <bits/stdc.h> using namespace std; const int N 510; struct edge…...

2018徐州 Gym - 102028J Carpets Removal 概率事件+二维差分
题目链接:https://vjudge.net/problem/Gym-102028J 题意:去掉两个矩形,让剩下的矩形覆盖的点最少 题解:使剩下的点少,那么我们就是去掉的两个矩形覆盖的尽可能多,也就是两个矩形各自占据的格子和只有两个…...
Gym - 102307J Jail Destruction 线段树
题目链接:https://vjudge.net/problem/Gym-102307J The semester has just begun at Universidad Nacional (UNAL). As usual, a group of vandals enjoys bringing chaos to all students and damaging the university facilities. Those guys have been planning…...

Gym - 101955L Machining Disc Rotors 计算几何 圆剩余部分直径
题目链接:https://vjudge.net/problem/Gym-101955L 题意:n个圆和 圆(0,0,R) 部分相交,求剩余圆的直径 题解:如果剩余部分存在直径的话,那就是2*R,这个怎么判断呢,对于每…...

Gym - 102307E Extreme Image 线段树 扫描线
题目链接:https://vjudge.net/problem/Gym-102307E 题意:平面上n个点,给定区域的w和d范围,求区域最大的点数 题解:其实和在平面上给定长和宽 是一样的道理,这个只需要对角度排序,然后对于每个…...

Gym - 102307C Common Subsequence dp
题目链接:https://vjudge.net/problem/Gym-102307C 题意:两个字符串的匹配长度能不能达到99%; 题解:因为n为1e5,所以不能直接dp[i][j]表示到(i,j)的最长匹配长度了,因为失配的最多1000,因此dp…...

18南京 Gym - 101981M Mediocre String Problem 扩展kmp+马拉车
题目链接:https://vjudge.net/problem/Gym-101981M 题意:求 s的一个子串后面加上t的前缀为回文串并且满足|s| > |t| 的个数 题解:因为牵扯到了前缀和子串的问题,大体就能想到要用到扩展kmp,我们把s倒置࿰…...

CodeForces - 374D Inna and Sequence 线段树
题目连接:https://vjudge.net/problem/CodeForces-374D 题意:n次操作,先给出m个数,原先有一个空序列,每次操作给出一个数,为1或0都放在后面,为-1时,删除m个位置的数 题解ÿ…...

ICPC Asia HongKong 2017
南京已成炮灰,徐州加油! A:java二分 import java.math.*; import java.util.*; public class Main {public static void main(String [] args){Scanner cinnew Scanner(System.in);int t,n;BigInteger z,zn,znjyn,ans,tans,x,XBigInteger.O…...