Codeforces Round #368 (Div. 2)D. Persistent Bookcase DFS
题目链接:http://codeforces.com/contest/707/my
看了这位大神的详细分析,一下子明白了。链接:http://blog.csdn.net/queuelovestack/article/details/52269321
搞了两天,终于彻底理解了。本来觉得怎么也不可能和DFS扯上关系,结果以操作的编号建树,对于其他3个操作,直接dfs即可。
在执行操作④之后,书架的状态其实已经回到了之前的某种状态k那么,如果我们从状态k出发,直接去推算出所有经操作④会回到状态k的状态,这样整体就是一个树。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3+;
int n,m,k;
struct node
{
int x,y;
};
node q[];
vector<int> g[];
int l[maxn][maxn];
int op[];
int ans[];
void dfs(int u,int sum)
{
ans[u] = sum;
for(int i=;i<g[u].size();i++)
{
int v = g[u][i];
int opp = op[v];
int x = q[v].x;
int y = q[v].y;
if(opp==)
{
if(l[x][y])
{
dfs(v,sum);
}
else
{
l[x][y] = ;
dfs(v,sum+);
l[x][y] = ;
}
}
else if(opp==)
{
if(!l[x][y])
{
dfs(v,sum);
}
else
{
l[x][y] = ;
dfs(v,sum-);
l[x][y] = ;
}
}
else if(opp==)
{
int cur = ;
for(int j=;j<=m;j++)
{
if(l[x][j])
{
l[x][j] = ;
cur--;
}
else
{
l[x][j] = ;
cur++;
}
}
dfs(v,sum+cur);
for(int j=;j<=m;j++)
{
if(l[x][j]) l[x][j] = ;
else l[x][j] = ;
}
}
else
{
dfs(v,sum);
}
}
}
int main()
{
cin>>n>>m>>k;
for(int i=;i<=k;i++)
{
int sel;
scanf("%d",&sel);
op[i] = sel;
if(sel!=)
{
if(sel<=)
{
scanf("%d %d",&q[i].x,&q[i].y);
g[i-].push_back(i);
}
else
{
scanf("%d",&q[i].x);
g[i-].push_back(i);
}
}
else
{
scanf("%d",&q[i].x);
g[q[i].x].push_back(i);
}
}
dfs(,);
for(int i=;i<=k;i++)
{
printf("%d\n",ans[i]);
}
return ;
}
Codeforces Round #368 (Div. 2)D. Persistent Bookcase DFS的更多相关文章
- Codeforces Round #368 (Div. 2) D. Persistent Bookcase
Persistent Bookcase Problem Description: Recently in school Alina has learned what are the persisten ...
- Codeforces Round #368 (Div. 2) D. Persistent Bookcase 离线 暴力
D. Persistent Bookcase 题目连接: http://www.codeforces.com/contest/707/problem/D Description Recently in ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- Codeforces Round #368 (Div. 2) C. Pythagorean Triples(数学)
Pythagorean Triples 题目链接: http://codeforces.com/contest/707/problem/C Description Katya studies in a ...
- Codeforces Round #368 (Div. 2) B. Bakery (模拟)
Bakery 题目链接: http://codeforces.com/contest/707/problem/B Description Masha wants to open her own bak ...
- Codeforces Round #368 (Div. 2) A. Brain's Photos (水题)
Brain's Photos 题目链接: http://codeforces.com/contest/707/problem/A Description Small, but very brave, ...
- D. Persistent Bookcase(Codeforces Round #368 (Div. 2))
D. Persistent Bookcase time limit per test 2 seconds memory limit per test 512 megabytes input stand ...
- Codeforces Round #368 (Div. 2) E. Garlands 二维树状数组 暴力
E. Garlands 题目连接: http://www.codeforces.com/contest/707/problem/E Description Like all children, Ale ...
- Codeforces Round #368 (Div. 2) C. Pythagorean Triples 数学
C. Pythagorean Triples 题目连接: http://www.codeforces.com/contest/707/problem/C Description Katya studi ...
随机推荐
- asp.net 基础
前台HTML,javascript,后台C# 代码能不在后台写,就不在后台写 WebSite和WebApplication的区别 1)当改变后台代码时,WebApplication需重启浏览器或者重新 ...
- 状压dp找寻环的个数 Codeforces Beta Round #11 D
http://codeforces.com/problemset/problem/11/D 题目大意:给你n个点,m条边,找该图中有几个换 思路:定义dp[i][j]表示i是圈的集合,j表示该集合的终 ...
- 在vim中使用perltidy美化perl代码
来源: http://www.cnblogs.com/itech/archive/2013/02/18/2915279.html 格式优美的perl代码不但让人赏心悦目,而且可以方便阅读. perlt ...
- 修改某个UITextField的键盘的返回键类型以及监听键盘的高度变化,取到键盘动画退出弹出的时间,一起随着键盘顶出来或者压下去,
1.修改某个UITextField的键盘的返回键类型: [_bottomTextView setReturnKeyType:UIReturnKeyDone]; 1.1.textFied点击return ...
- php 特定类型测试函数
is_array() 检查变量是否是数组 is_double().is_float().is_real() 检查变量是否是浮点数 is_long().is_int().is_integer()检查变量 ...
- 以excel方式输出数据
主类Test: public class D201 {//get set 方法略去 private String d201_01; private String d201_02; private St ...
- hibernate5 中的schemaExport
hibernate5中的schemaExport与之前版本中的用法有所不同,具体用法如下: ServiceRegistry serviceRegistry = new StandardServiceR ...
- eclipse无法导入已有android项目
问题: 今天发现我拷贝的一个android项目无法导入到eclipse,但是其它的已有android项目却可以导入 思路 现在网络这么流行,当然是上网查,得益于eclipse无法导入Android工程 ...
- MYSQL和ORACLE的触发器与存储过程语法差异
整改了一番脚本,遇到了一些两种数据库之间的差异,记录一下: 触发器: 差异 MYSQL ORACLE 说明 创建语句不同 create trigger `AA` BEFORE INSERT on `B ...
- 取得GridView某行的DataKey
首先绑定DataKeyNames GridView.DataKeyNames = new string[] { "字段名称" }; 取值 string aaa= GridView. ...