Codeforces Round #383 (Div. 1)

A. Arpa's loud Owf and Mehrdad's evil plan
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

As you have noticed, there are lovely girls in Arpa’s land.

People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi.

Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows.

The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushxcalls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time.

Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t ≥ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from yx would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible.

Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i).

Input

The first line of input contains integer n (1 ≤ n ≤ 100) — the number of people in Arpa's land.

The second line contains n integers, i-th of them is crushi (1 ≤ crushi ≤ n) — the number of i-th person's crush.

Output

If there is no t satisfying the condition, print -1. Otherwise print such smallest t.

Examples
input
4
2 3 1 4
output
3
input
4
4 4 4 4
output
-1
input
4
2 1 4 3
output
1
Note

In the first sample suppose t = 3.

If the first person starts some round:

The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1.

The process is similar for the second and the third person.

If the fourth person starts some round:

The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4.

In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa.




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;
}

B. Arpa's weak amphitheater and Mehrdad's valuable Hoses
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

The first line contains integers nm and w (1  ≤  n  ≤  10001 ≤ 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 ≤ nxi ≠ yi), meaning that Hoses xi and yi are friends. Note that friendship is bidirectional. All pairs (xi, yi) are distinct.

Output

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.

Examples
input
3 1 5
3 2 5
2 4 2
1 2
output
6
input
4 2 11
2 4 6 6
6 4 2 1
1 2
2 3
output
7
Note

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;
}

C. Arpa’s overnight party and Mehrdad’s silent entering
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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:

  • 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.

Input

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.

Output

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.

Example
input
3
1 4
2 5
3 6
output
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;
}

D. Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths
time limit per test
 3 seconds
memory limit per test
 256 megabytes
input
 standard input
output
 standard output

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.

Input

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  ≤  ici + 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.

Output

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.

Examples
input
4
1 s
2 a
3 s
output
3 1 1 0 
input
5
1 a
2 h
1 a
4 h
output
4 1 0 1 0 


D题是一个细节很多的dsu on tree

需要用到压位

题解链接





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值