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 ...
随机推荐
- Centos中压缩(zip)和解压(unzip)命令
摘自:http://liuzhichao.com/p/681.html 1.我下载了一个yasuo.zip文件,想解压缩: # unzip yasuo.zip 2.我当前目录下有abc1.zip,ab ...
- java 集合中将元素倒序排列
方法一:实现Comparable接口排序package collsort.comparable; package com.cvicse.sort.comparable; public class Ca ...
- Vim常用命令【转载】
下面基本是vim的基本用法,刚开始学习可能有些不习惯.但贵在坚持,即使不习惯,也要坚持使用,做到不经过思考就能操作,你会发现真的很方便.很多操作可以通过不同的命令达到,我这里只列出常用的. 基础命令 ...
- SSH returns “too many authentication failures” error – HostGator
I am an avid fan of using HostGator for small business WordPress website hosting. I love that they u ...
- iptables进行端口重定向
使用iptables进行端口重定向,方法如下: 在机器A上操作: #iptables -t nat -A PREROUTING -d 192.168.203.173 -p tcp -m tcp --d ...
- vim 设置默认显示行号
vim 要默认打开的时候显示行号,这样就可以不用每次编辑的时候在手动在命令模式下:set number 来显示行号. 具体做法是: 创建一个 .vimrc的配置文件. 在启动vim时,当前用户根目录下 ...
- 设置TabBar分栏控制器上图片的大小问题
我们都知道,iOS因为屏幕分辨率的问题,UID在交付我们iOS开发人员程序配图的时候,一般是三套图,分别对应三种不同的分辨率,对不同size的屏幕系统会自动使用不同像素的图片,我们只需要在命名时给三套 ...
- xcconfig 文件配置文件 问题
与公司 QA 聊天,已不止一次被吐槽说移动端从开发环境转到生产环境时,还要靠修改代码来配置对应的环境参数.她认为,从 App 转测试之后,就不应该再修改代码,可以把所有的环境配置都整合到配置文件中,这 ...
- Linux + Apache + PHP 环境搭建
搭建环境: Ubuntu 15.04 Apache 2.4.16 PHP 5.6.15 1 安装Apache 先安装依赖程序(都安装在 /usr/local/ 目录下) apr-1.5.2.tar.g ...
- panel 绑定鼠标滚轮事件
void formsample_mousewheel(object sender, MouseEventArgs e) { //获取光标位置 Point mousepoint = new Point( ...