n个人,每个人有一个喜欢的人。现在从一个人开始报数,报t次,每次报数的人从自己转移到自己喜欢的人。问对于每个人,是否存在一个最小的t,从自己开始报t*2次又回到自己。
建一个n个点n条边的有向图,每个人向自己喜欢的人连边,图当中若每个点入度都为1则有解,因为此时图中全为环。这时,因为每个点的入度出度都为1,建出的图一定是很多个环。对于每个环,算出环的点数。如果点数x为奇数,那么要走t*2步之后回到自己,t最小只能取点数x的倍数。如果点数x为偶数,那么要走t*2步之后回到自己,t最小可以取x/2。答案就是所有奇点数x和偶点数x/2的最小公倍数。
一开始以为是个模拟,被坑了两发。
#include
#include
#include
#include
#include
Just to remind, girls in Arpa's land are really nice.
Mehrdad wants to invite some Hoses to the palace for a dancing party. Each Hos has some weight wi and some beauty bi. Also each Hos may have some friends. Hoses are divided in some friendship groups. Two Hoses x and y are in the same friendship group if and only if there is a sequence of Hoses a1, a2, ..., aksuch that ai and ai + 1 are friends for each 1 ≤ i < k, and a1 = x and ak = y.
Arpa allowed to use the amphitheater of palace to Mehrdad for this party. Arpa's amphitheater can hold at most w weight on it.
Mehrdad is so greedy that he wants to invite some Hoses such that sum of their weights is not greater than w and sum of their beauties is as large as possible. Along with that, from each friendship group he can either invite all Hoses, or no more than one. Otherwise, some Hoses will be hurt. Find for Mehrdad the maximum possible total beauty of Hoses he can invite so that no one gets hurt and the total weight doesn't exceed w.
The first line contains integers n, m and w (1 ≤ n ≤ 1000, , 1 ≤ w ≤ 1000) — the number of Hoses, the number of pair of friends and the maximum total weight of those who are invited.
The second line contains n integers w1, w2, ..., wn (1 ≤ wi ≤ 1000) — the weights of the Hoses.
The third line contains n integers b1, b2, ..., bn (1 ≤ bi ≤ 106) — the beauties of the Hoses.
The next m lines contain pairs of friends, the i-th of them contains two integers xi and yi (1 ≤ xi, yi ≤ n, xi ≠ yi), meaning that Hoses xi and yi are friends. Note that friendship is bidirectional. All pairs (xi, yi) are distinct.
Print the maximum possible total beauty of Hoses Mehrdad can invite so that no one gets hurt and the total weight doesn't exceed w.
3 1 5 3 2 5 2 4 2 1 2
6
4 2 11 2 4 6 6 6 4 2 1 1 2 2 3
7
In the first sample there are two friendship groups: Hoses {1, 2} and Hos {3}. The best way is to choose all of Hoses in the first group, sum of their weights is equal to 5 and sum of their beauty is 6.
In the second sample there are two friendship groups: Hoses {1, 2, 3} and Hos {4}. Mehrdad can't invite all the Hoses from the first group because their total weight is 12 > 11, thus the best way is to choose the first Hos from the first group and the only one from the second group. The total weight will be 8, and the total beauty will be 7.
n个美女参加舞会,这些人组成了很多朋友圈子。每个美女 i 有一个美丽程度bi, 邀请的成本wi.现在要使邀请的成本不大于w,且对于每个朋友圈,要么全部邀请,要么只邀请其中一个美女。问最大的美丽程度和是多少。
一个简单的二维背包问题。对于每个朋友圈,单独考虑即可。
#include
#include
#include
#include
#include
Note that girls in Arpa’s land are really attractive.
Arpa loves overnight parties. In the middle of one of these parties Mehrdad suddenly appeared. He saw npairs of friends sitting around a table. i-th pair consisted of a boy, sitting on the ai-th chair, and his girlfriend, sitting on the bi-th chair. The chairs were numbered 1 through 2n in clockwise direction. There was exactly one person sitting on each chair.
There were two types of food: Kooft and Zahre-mar. Now Mehrdad wonders, was there any way to serve food for the guests such that:
Find the answer for the Mehrdad question. If it was possible, find some arrangement of food types that satisfies the conditions.
The first line contains an integer n (1 ≤ n ≤ 105) — the number of pairs of guests.
The i-th of the next n lines contains a pair of integers ai and bi (1 ≤ ai, bi ≤ 2n) — the number of chair on which the boy in the i-th pair was sitting and the number of chair on which his girlfriend was sitting. It's guaranteed that there was exactly one person sitting on each chair.
If there is no solution, print -1.
Otherwise print n lines, the i-th of them should contain two integers which represent the type of food for the i-th pair. The first integer in the line is the type of food the boy had, and the second integer is the type of food the girl had. If someone had Kooft, print 1, otherwise print 2.
If there are multiple solutions, print any of them.
3 1 4 2 5 3 6
1 2 2 1 1 2
有n对情侣,坐在圆桌的n*2个座位上参加宴会。现在有两种菜品,要求给每个人分配一种菜,使得情侣间的菜品不同,且座位上任意连续三个位置不能选择相同菜品。要求输出分配方案,不能则输出-1.
有趣的构造题。
首先,这题不存在无解的情况。
我们构造一个无向图,每条边表示边的两个端点不能选择同样菜品。则情侣之间肯定要连边。下面,我们把坐在2*n-1和2*n号座位的人连边,这样的话,就满足任意连续三个位置菜品不相同的条件了。
这样构造之后的图,每个点度数都为2,一定是若干个环。在环里dfs染色,相邻的两点涂上不同颜色。
#include
#include
#include
#include
#include
Just in case somebody missed it: we have wonderful girls in Arpa’s land.
Arpa has a rooted tree (connected acyclic graph) consisting of n vertices. The vertices are numbered 1through n, the vertex 1 is the root. There is a letter written on each edge of this tree. Mehrdad is a fan of Dokhtar-kosh things. He call a string Dokhtar-kosh, if we can shuffle the characters in string such that it becomes palindrome.
He asks Arpa, for each vertex v, what is the length of the longest simple path in subtree of v that form a Dokhtar-kosh string.
The first line contains integer n (1 ≤ n ≤ 5·105) — the number of vertices in the tree.
(n - 1) lines follow, the i-th of them contain an integer pi + 1 and a letter ci + 1 (1 ≤ pi + 1 ≤ i, ci + 1 is lowercase English letter, between a and v, inclusively), that mean that there is an edge between nodes pi + 1 and i + 1 and there is a letter ci + 1 written on this edge.
Print n integers. The i-th of them should be the length of the longest simple path in subtree of the i-th vertex that form a Dokhtar-kosh string.
4
1 s
2 a
3 s
3 1 1 0
5
1 a
2 h
1 a
4 h
4 1 0 1 0
D题是一个细节很多的dsu on tree
需要用到压位
题解链接