CodeForces 1073F Choosing Two Paths】的更多相关文章

Description You are given an undirected unweighted tree consisting of \(n\) vertices. An undirected tree is a connected undirected graph with \(n−1\) edges. Your task is to choose two pairs of vertices of this tree (all the chosen vertices should be…
[codeforces 293]B. Distinct Paths 试题描述 You have a rectangular n × m-cell board. Some cells are already painted some of k colors. You need to paint each uncolored cell one of the k colors so that any path from the upper left square to the lower right…
题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:102400000, 102400000") #include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #include…
http://codeforces.com/problemset/problem/219/D 题目大意: 给出一棵树,但是它的边是有向边,选择一个城市,问最少调整多少条边的方向能使一个选中城市可以到达所有的点,输出最小的调整的边数,和对应的点. 思路:先预处理一个点为根的代价,然后去dfs移动,总复杂度是O(n) #include<cstdio> #include<cmath> #include<algorithm> #include<cstring> #i…
Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output The country Treeland consists of n cities, some pairs of them are connected with unidirectional roads. Overall…
题目链接:http://codeforces.com/contest/219/problem/D 题目大意: 给定一个n个节点的数和连接n个节点的n - 1条有向边,现在要选定一个节点作为起始节点,从这个点出发需要能走到其余每个节点,途中必然要调整有向边的方向,请求出当选定哪些节点作为初始节点时,所要调整的有向边最少,输出最小调整的边数和这些节点. 分析: Hint:城市结构是树型结构. 代码如下: #include <bits/stdc++.h> using namespace std; #…
http://codeforces.com/problemset/problem/219/D 题意 给一颗树但边是单向边,求至少旋转多少条单向边的方向,可以使得树上有一点可以到达树上任意一点,若有多个答案按顺序全部输出. 分析 把边的方向化为权值,正向为0,逆向为1.问题转化为找哪些点的在遍历全图后总权值最小.这就是树形DP了,它可以从子树收获价值,也可以从父亲收获.所以dfs两遍. 定义dp[u][0]为以u为根的的子树全可达的修改次数,随便选定一个点,用子节点信息更新父节点,dp[u][0]…
[题目]F. Paths [题意]给定数字n,图上有编号为1~n的点,两点当且仅当gcd(u,v)≠1时有连边,定义d(u,v)为两点间最短距离(若不连通则为0),求Σd(u,v),1<=u<v<=n,n<=10^7. [算法]数论 [题解]对于1<=x<=n,当x=1或x是大于n/2的素数时,x是孤立节点. 令p[x]表示x的最小素因子,考虑任意一对点的连边情况: 1.d=0:当至少一个点是孤立节点时,d(u,v)=0,否则都能通过下面的情况到达. 2.d=1:当gc…
题目链接:http://codeforces.com/problemset/problem/219/D 题意: 给你一棵树,n个节点. 树上的边都是有向边,并且不一定是从父亲指向儿子的. 你可以任意翻转一些边的方向. 现在让你找一个节点,使得从这个节点出发能够到达其他所有节点,并保证翻转边的数量最小. 问你最少翻转多少条边,并输出所有满足此条件的节点编号. 题解: 本题要解两个dp: dp1 & dp2 首先考虑dp1: 表示状态: dp1[i]表示使节点i能够到达i的子树中的所有节点,翻转边的…
Codeforces 题目传送门 & 洛谷题目传送门 首先考虑 \(p>50\) 的时候怎么处理,也就是求一个区间的绝对众数.我们知道众数这个东西是不能用线段树直接维护的,因为对于区间 \([l,r]\) 和 \(x\in [l,r)\),区间 \([l,r]\) 的众数不一定是 \([l,x]\) 的众数或 \([x+1,r]\) 的众数. 不过此题有一个特殊之处,就是我们要求区间的绝对众数而不是区间的众数.这时候就可以用线段树维护了,因为区间 \([l,r]\) 的绝对众数一定至少是区间…