题目链接: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的更多相关文章

  1. Codeforces Round #368 (Div. 2) D. Persistent Bookcase

    Persistent Bookcase Problem Description: Recently in school Alina has learned what are the persisten ...

  2. Codeforces Round #368 (Div. 2) D. Persistent Bookcase 离线 暴力

    D. Persistent Bookcase 题目连接: http://www.codeforces.com/contest/707/problem/D Description Recently in ...

  3. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  4. Codeforces Round #368 (Div. 2) C. Pythagorean Triples(数学)

    Pythagorean Triples 题目链接: http://codeforces.com/contest/707/problem/C Description Katya studies in a ...

  5. Codeforces Round #368 (Div. 2) B. Bakery (模拟)

    Bakery 题目链接: http://codeforces.com/contest/707/problem/B Description Masha wants to open her own bak ...

  6. Codeforces Round #368 (Div. 2) A. Brain's Photos (水题)

    Brain's Photos 题目链接: http://codeforces.com/contest/707/problem/A Description Small, but very brave, ...

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

  8. Codeforces Round #368 (Div. 2) E. Garlands 二维树状数组 暴力

    E. Garlands 题目连接: http://www.codeforces.com/contest/707/problem/E Description Like all children, Ale ...

  9. Codeforces Round #368 (Div. 2) C. Pythagorean Triples 数学

    C. Pythagorean Triples 题目连接: http://www.codeforces.com/contest/707/problem/C Description Katya studi ...

随机推荐

  1. Ubuntu服务器断网问题解决

    原因:dns服务器没有了配置信息. 配置dns服务器          ubuntu 的dns服务器信息,放在 /etc/resolv.conf中,          添加dns服务器地址,如192. ...

  2. LeetCode OJ 226. Invert Binary Tree

    Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was ...

  3. iOS 上架提示ipad需要显示四个方位,而我们只能竖屏的时候的解决办法

    勾选requires sull screen

  4. 【转】虚拟机下安装小红帽Linux9.0图解

    http://blog.163.com/ly676830315@126/blog/static/10173372220119148583539/

  5. /etc/rc.local 与 /etc/init.d Linux 开机自动运行程序

    1. /etc/rc.local 这是使用者自订开机启动程序,把需要开机自动运行的程序写在这个脚本里 --------引用---------------------- 在完成 run level 3 ...

  6. Linux文件系统中硬链接和软链接的区别 (转)

    建立硬链接命令:ln src-link dest-link建立软链接:ln -s src-link dest-link   1. 硬链接是别名,软链接是快捷方式   2. 硬链接和源链接指向同一个i节 ...

  7. 线段树+dp+贪心 Codeforces Round #353 (Div. 2) E

    http://codeforces.com/contest/675/problem/E 题目大意:有n个车站,每个车站只能买一张票,这张票能从i+1到a[i].定义p[i][j]为从i到j所需要买的最 ...

  8. 使用btoa和atob来进行Base64转码和解码

      btoa: 将普通字符串转为Base64字符串 atob: 将Base64字符串转为普通字符串   说明:window.btoa不支持汉字:   ===>使用window.encodeURI ...

  9. hiho 1015 KMP

    input 1<=T<=20 string1 1<=strlen(string1)<=1e4 string2 2<=strlen(string2)<=1e6 out ...

  10. 华哥倒酒<区间标记,二分>

    题目链接 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; t ...