n个人,每个人有一个喜欢的人。现在从一个人开始报数,报t次,每次报数的人从自己转移到自己喜欢的人。问对于每个人,是否存在一个最小的t,从自己开始报t*2次又回到自己。
建一个n个点n条边的有向图,每个人向自己喜欢的人连边,图当中若每个点入度都为1则有解,因为此时图中全为环。这时,因为每个点的入度出度都为1,建出的图一定是很多个环。对于每个环,算出环的点数。如果点数x为奇数,那么要走t*2步之后回到自己,t最小只能取点数x的倍数。如果点数x为偶数,那么要走t*2步之后回到自己,t最小可以取x/2。答案就是所有奇点数x和偶点数x/2的最小公倍数。
一开始以为是个模拟,被坑了两发。
#include <cstdio>
#include <iostream>
#include <string.h>
#include <string>
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=105,inf=0x3f3f3f3f;
const ll llinf=0x3f3f3f3f3f3f3f3f;
const ld pi=acos(-1.0L);
int a[maxn],in[maxn];
ll b[maxn];
bool visit[maxn];
int cn=0;
ll gcd(ll x,ll y) {
ll z;
if (x<y) swap(x,y);
while (y) {
z=y;y=x%y;x=z;
}
return x;
}
void dfs(int now) {
visit[now]=1;
b[cn]++;
if (!visit[a[now]]) dfs(a[now]);
}
int main() {
int n,i,j,t;
ll ans=1;
scanf("%d",&n);
for (i=1;i<=n;i++) {
scanf("%d",&a[i]);
in[a[i]]++;
}
for (i=1;i<=n;i++) {
if (in[i]!=1) {
printf("-1\n");
return 0;
}
}
mem0(visit);
for (i=1;i<=n;i++) {
if (!visit[i]) {
cn++;
dfs(i);
}
}
for (i=1;i<=cn;i++) {
if (b[i]%2==0) b[i]/=2;
ll p=gcd(b[i],ans);
ans=(b[i]/p)*ans;
}
printf("%d\n",ans);
return 0;
}
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.
![](https://i-blog.csdnimg.cn/blog_migrate/390add03933cbe533a51127978715def.png)
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 <cstdio>
#include <iostream>
#include <string.h>
#include <string>
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=1005,inf=0x3f3f3f3f;
const ll llinf=0x3f3f3f3f3f3f3f3f;
const ld pi=acos(-1.0L);
int dp[maxn][maxn];
int w[maxn],val[maxn];
bool visit[maxn];
vector<int> v[maxn],l;
int num=0,wn,vn;
void dfs(int now) {
l.push_back(now);
visit[now]=1;
wn+=w[now];vn+=val[now];
for (int i=0;i<v[now].size();i++) {
int to=v[now][i];
if (!visit[to]) dfs(to);
}
}
int main() {
int n,m,mw,x,y,i,j,k;
scanf("%d%d%d",&n,&m,&mw);
for (i=1;i<=n;i++) scanf("%d",&w[i]);
for (i=1;i<=n;i++) scanf("%d",&val[i]);
for (i=1;i<=m;i++) {
scanf("%d%d",&x,&y);
v[x].push_back(y);
v[y].push_back(x);
}
mem0(visit);
for (i=1;i<=n;i++) {
if (!visit[i]) {
l.clear();
num++;
wn=vn=0;
dfs(i);
for (k=mw;k>=0;k--) dp[num][k]=dp[num-1][k];
for (j=0;j<l.size();j++) {
int r=l[j];
for (k=mw;k>=w[r];k--)
dp[num][k]=max(dp[num][k],dp[num-1][k-w[r]]+val[r]);
}
for (k=mw;k>=wn;k--)
dp[num][k]=max(dp[num][k],dp[num-1][k-wn]+vn);
}
}
printf("%d\n",dp[num][mw]);
return 0;
}
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.
![](https://i-blog.csdnimg.cn/blog_migrate/6c319c64557d00208ecc93282ee88d0a.png)
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:
- Each person had exactly one type of food,
- No boy had the same type of food as his girlfriend,
- Among any three guests sitting on consecutive chairs, there was two of them who had different type of food. Note that chairs 2n and 1 are considered consecutive.
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 <cstdio>
#include <iostream>
#include <string.h>
#include <string>
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=100005,inf=0x3f3f3f3f;
const ll llinf=0x3f3f3f3f3f3f3f3f;
const ld pi=acos(-1.0L);
int c[maxn*2],x[maxn],y[maxn];
vector<int> v[maxn*2];
bool visit[maxn*2];
bool dfs(int now,int p) {
visit[now]=1;
c[now]=p+1;
if (!visit[v[now][0]]) dfs(v[now][0],p^1);
else if (!visit[v[now][1]]) dfs(v[now][1],p^1);
return true;
}
int main() {
int n,i,j;
scanf("%d",&n);
for (i=1;i<=n;i++) {
scanf("%d%d",&x[i],&y[i]);
v[x[i]].push_back(y[i]);
v[y[i]].push_back(x[i]);
v[i*2].push_back(i*2-1);
v[i*2-1].push_back(i*2);
}
mem0(visit);
for (i=1;i<=n*2;i++) {
if (!visit[i]) dfs(i,0);
}
for (i=1;i<=n;i++) {
printf("%d %d\n",c[x[i]],c[y[i]]);
}
return 0;
}
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.
![](https://i-blog.csdnimg.cn/blog_migrate/cde345a297ce4cc4c95f7129e5539765.png)
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
需要用到压位