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 ...
随机推荐
- js获取不同浏览器盒子宽度高度
DTD 已声明 IE document.documentElement.scrollHeight 浏览器所有内容高 度 ,document.body.scrollHeight 浏览器所有内容高度 do ...
- cdn是什么
CDN的全称是Content Delivery Network,即内容分发网络.其目的是通过在现有的Internet中增加一层新的网络架构, 将网站的内容发布到最接近用户的网络”边缘”,使用户可以就近 ...
- device-mapper: multipath: Failing path recovery【转载】
digoal 2016-04-05 10:09:42 浏览180 评论0 摘要: 由于扇区损坏导致多路径设备failed. 现象如下 : # dmesg : device-mapper: mul ...
- label不换行的问题
除了numberOfLines属性label有一个preferredMaXLayoutWidth属性.设置试试
- s7-300 第二讲
- sql中 replace函数
例用 xxx 替换 abcdefghi 中的字符串 cde. SELECT REPLACE(''abcdefghicde'',''cde'',''xxx'')
- 扩展方法where方法查询不到数据,不会抛异常,也不是返回的null
如题,“扩展方法where方法查询不到数据,不会抛异常,也不是返回的null”,示例代码如下: Product类: public class Product { private string name ...
- 自定义switch开关
自定义一个switch开关 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...
- ignite客户端找不到服务端的时候如何设置退出
ignite启动客户端时需要有服务端支持: Ignition.setClientMode(true); Ignition.start("ignite.xml"); 这里有个问题,当 ...
- FileSystemXmlApplicationContext方法的绝对路径问题
public AgentServer(Socket c,String confDir) { this.client = c; ApplicationContext ac = new FileSyste ...