codeforces 707D-(DFS+bitset)
题目链接:http://codeforces.com/contest/707/problem/D
根据询问建立一棵树然后DFS。
#include<bits/stdc++.h>
using namespace std;
const int N=1e3+3;
const int maxn=1e5+3;
int n,m,q;
vector<int> G[maxn];
bitset<N> bit[N];
bitset<N> opp;
int ope[maxn],a[maxn],b[maxn],ans[maxn];
void dfs(int x)
{
if(ope[x]==1)
{
int flag=0;
if(!bit[a[x]][b[x]])
{
bit[a[x]][b[x]]=1;
ans[x]++;
flag=1;
}
for(int i=0;i<G[x].size();i++)
{
ans[G[x][i]]=ans[x];
dfs(G[x][i]);
}
if(flag)
bit[a[x]][b[x]]=0; }
else if(ope[x]==2)
{
int flag=0;
if(bit[a[x]][b[x]])
{
bit[a[x]][b[x]]=0;
ans[x]--;
flag=1;
}
for(int i=0;i<G[x].size();i++)
{
ans[G[x][i]]=ans[x];
dfs(G[x][i]);
}
if(flag)
bit[a[x]][b[x]]=1;
}
else if(ope[x]==3)
{
ans[x]+=m-2*bit[a[x]].count();
bit[a[x]]^=opp;
for(int i=0;i<G[x].size();i++)
{
ans[G[x][i]]=ans[x];
dfs(G[x][i]);
}
bit[a[x]]^=opp;
}
else
{
for(int i=0;i<G[x].size();i++)
{
ans[G[x][i]]=ans[x];
dfs(G[x][i]);
}
}
}
int main()
{
scanf("%d %d %d",&n,&m,&q);
for(int i=1;i<=m;i++)
opp[i]=1;
for(int i=1;i<=q;i++)
{
scanf("%d",&ope[i]);
if(ope[i]==1)
{
scanf("%d %d",a+i,b+i);
G[i-1].push_back(i);
}
else if(ope[i]==2)
{
scanf("%d %d",a+i,b+i);
G[i-1].push_back(i);
}
else if(ope[i]==3)
{
scanf("%d",a+i);
G[i-1].push_back(i);
}
else
{
scanf("%d",a+i);
G[a[i]].push_back(i);
}
}
for(int i=0;i<G[0].size();i++)
{
ans[G[0][i]]=0;
dfs(G[0][i]);
}
for(int i=1;i<=q;i++)
printf("%d\n",ans[i]);
return 0;
}
codeforces 707D-(DFS+bitset)的更多相关文章
- Persistent Bookcase CodeForces - 707D (dfs 离线处理有根树模型的问题&&Bitset)
Persistent Bookcase CodeForces - 707D time limit per test 2 seconds memory limit per test 512 megaby ...
- [HIHO1041]国庆出游(DFS, bitset)
题目链接:http://hihocoder.com/problemset/problem/1041 学会了用C++的bitset哈,可喜可贺.以后遇到超过64位想用位来表示状态就不愁了哈. 这题用bi ...
- Military Problem CodeForces 1006E (dfs序)
J - Military Problem CodeForces - 1006E 就是一道dfs序的问题 给定一个树, 然后有q次询问. 每次给出u,k, 求以u为根的子树经过深搜的第k个儿子,如果一个 ...
- Graph Without Long Directed Paths CodeForces - 1144F (dfs染色)
You are given a connected undirected graph consisting of nn vertices and mm edges. There are no self ...
- [CSP-S模拟测试]:世界线(DFS+bitset)
题目描述 时间并不是一条单一的线,而是有许多世界线构成的流. 在一些时刻,世界线会发生分裂:同样的,它们也有可能在一些时刻收束在一起.如果将这些时刻抽象成点,那么这些世界线构成的网络,实际上是一张有向 ...
- 素数环(dfs+回溯)
题目描述: 输入正整数n,把整数1,2...n组成一个环,使得相邻两个数和为素数.输出时从整数1开始逆时针排列并且不能重复: 例样输入: 6 例样输出: 1 4 3 2 5 6 1 6 5 2 3 4 ...
- UVA 291 The House Of Santa Claus(DFS算法)
题意:从 节点1出发,一笔画出 圣诞老人的家(所谓一笔画,就是遍访所有边且每条边仅访问一次). 思路:深度优先搜索(DFS算法) #include<iostream> #include&l ...
- 历届试题 邮局(dfs+剪枝)
历届试题 邮局 时间限制:1.0s 内存限制:256.0MB 问题描述 C村住着n户村民,由于交通闭塞,C村的村民只能通过信件与外界交流.为了方便村民们发信,C村打算在C村建设k ...
- POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE
POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...
- codeforces 731C(DFS)
题目链接:http://codeforces.com/contest/731/problem/C 题意:有n只袜子(1~n),k种颜色(1~k),在m天中,左脚穿下标为l,右脚穿下标为r的袜子,问最少 ...
随机推荐
- hdu 4405Aeroplane chess(概率DP)
Aeroplane chess Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu----(1950)Bridging signals(最长递增子序列 (LIS) )
Bridging signals Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- Anagrams [LeetCode]
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...
- Javascript之对象的继承
继承是面向对象语言一个非常重要的部分.许多OOP语言都支持接口继承和实现继承两种方式.接口继承:继承方法签名:实现继承:继承实际的方法.在ECMAScript中函数是没有签名的,所以也就无法实现接口继 ...
- c++ boost 汉字和模式串混用的例子
*=============================================================== * Copyright (C) All rights reserved ...
- c# 关键字delegate、event(委托与事件)[MSDN原文摘录][1]
A delegate is a type that safely encapsulates a method, similar to a function pointer in C and C++. ...
- [转]Java FileInputStream与FileReader的区别
在解释Java中FileInputStream和FileReader的具体区别之前,我想讲述一下Java中InputStream和Reader的根本差异,以及分别什么时候使用InputStream和R ...
- select option居中显示
<style> .ch-select{ padding:0px;} .ch-select input[type=text]{ width:100%; position:relative; ...
- 本地调试WordPress计划终告失败
小猪本来想把博客的网站数据迁移到自己的电脑上面,mysql数据库还是放在主机供应商,这样就能缓解一下每次写博客时访问速度捉急的状况. 计划是美满的,但是只到实施的时候才发现各种问题.先是直接运行程序时 ...
- c# 配置文件之configSections配置
对于小型项目来说,配置信息可以通过appSettings进行配置,而如果配置信息太多,appSettings显得有些乱,而且在开发人员调用时,也不够友好,节点名称很容易写错,这时,我们有几种解决方案 ...