Codeforces Round 239

Posted on By 二价氢

#Div2.

##A

##B

#Div1.

##A Xor-tree

###Task

一棵树,每个节点有一个值,我们可以翻转一个节点的值(0变1,1变0),同时这个点的偶数代子孙的值也会翻转,奇数代不变

问要修改哪些节点使得每个节点的值变为目标的值

###Solution

猜想1:仅存在一种操作数最小的方案

猜想2:然后就硬推

###Code

最长耗时93 MS
最多内存8400 KB
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn=100005;
vector <int> e[maxn];
vector <int> s[maxn];
typedef vector<int>::iterator ii;
int n;
int vis[maxn],q[maxn],ql,qr;
int init[maxn],goal[maxn];
int sel[maxn],fasel[maxn],fafasel[maxn];
void dfs(int u)
{
	if ((fafasel[u]^init[u])!=goal[u])
		sel[u]=true;
	for (ii v=s[u].begin();v!=s[u].end();v++)
	{
		fasel[*v]^=(sel[u]^fafasel[u]);
		fafasel[*v]^=fasel[u];
		dfs(*v);
	}
}
int main()
{
	int u,v;
	scanf("%d",&n);
	for (int i=1;i<n;i++)
		scanf("%d%d",&u,&v),e[u].push_back(v),e[v].push_back(u);
	for (int i=1;i<=n;i++)
		scanf("%d",&init[i]);
	for (int i=1;i<=n;i++)
		scanf("%d",&goal[i]);
	q[ql=qr=1]=1;
	for (;ql<=qr;ql++)
	{
		vis[u=q[ql]]=1;
		for (ii v=e[u].begin();v!=e[u].end();v++)
			if (!vis[*v])
				s[u].push_back(q[++qr]=*v);
	}
	dfs(1);
	int cnt=0;
	for (int i=1;i<=n;i++)
		cnt+=sel[i];
	printf("%d\n",cnt);
	for (int i=1;i<=n;i++)
	{
		if (sel[i])
			printf("%d\n",i);
	}
	return 0;
}

##B Working out

###Task

Summer is coming! It’s time for Iahub and Iahubina to work out, as they both want to look hot at the beach. The gym where they go is a matrix with lines and columns. Let number represents the calories burned by performing workout at the cell of gym in the -th line and the -th column.

Iahub starts with workout located at line and column . He needs to finish with workout . After finishing workout , he can go to workout or . Similarly, Iahubina starts with workout and she needs to finish with workout . After finishing workout from cell , she goes to either or .

There is one additional condition for their training. They have to meet in exactly one cell of gym. At that cell, none of them will work out. They will talk about fast exponentiation (pretty odd small talk) and then both of them will move to the next workout.

If a workout was done by either Iahub or Iahubina, it counts as total gain. Please plan a workout for Iahub and Iahubina such as total gain to be as big as possible. Note, that Iahub and Iahubina can perform workouts with different speed, so the number of cells that they use to reach meet cell may differs.

###Solution

我们可以知道,锻炼的路径仅在一点相交,不然我们一定能找到一种方案使得答案更优。

###Code

最长耗时249 MS
最多内存39500 KB
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=1005;
long long Map[maxn][maxn],f[maxn][maxn],g[maxn][maxn],F[maxn][maxn],G[maxn][maxn];
int m,n;
int main()
{
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++)
		for (int j=1;j<=m;j++)
			scanf("%lld",&Map[i][j]);
	for (int i=1;i<=n;i++)
		for (int j=1;j<=m;j++)
			f[i][j]=max(f[i-1][j],f[i][j-1])+Map[i][j];
	for (int i=n;i>=1;i--)
		for (int j=1;j<=m;j++)
			g[i][j]=max(g[i+1][j],g[i][j-1])+Map[i][j];
	for (int i=n;i>=1;i--)
		for (int j=m;j>=1;j--)
			F[i][j]=max(F[i+1][j],F[i][j+1])+Map[i][j];
	for (int i=1;i<=n;i++)
		for (int j=m;j>=1;j--)
			G[i][j]=max(G[i-1][j],G[i][j+1])+Map[i][j];
	long long ans=0,tin,tout;
	for (int i=2;i<n;i++)
		for (int j=2;j<m;j++)
		{
			ans=max(ans,
			max(f[i-1][j]+g[i][j-1]+F[i+1][j]+G[i][j+1],
			    f[i][j-1]+g[i+1][j]+F[i][j+1]+G[i-1][j]));
		}
	printf("%lld\n",ans);
}

##C

##D

##E