zoukankan      html  css  js  c++  java
  • 【离线】【深搜】【树】Codeforces 707D Persistent Bookcase

    题目链接:

      http://codeforces.com/problemset/problem/707/D

    题目大意:

      一个N*M的书架,支持4种操作

      1.把(x,y)变为有书。

      2.把(x,y)变为没书。

      3.把x行上的所有书状态改变,有变没,没变有。

      4.回到第K个操作时的状态。

      求每一次操作后书架上总共多少书。

    题目思路:

      【离线】【深搜】【树】

      现场有思路不过没敢写哈。还是太弱了。

      总共只用保存一张图,把操作看成一棵树,一开始I操作连接在I-1操作后,如果遇到操作4的话,把I操作与I-1操作的边断开,改为连接到K下。

      这样把所有操作链完以后得到一棵多叉树,接下来深搜一遍,记录答案,回溯的时候把图的状态改回去即可。

      1 //
      2 //by coolxxx
      3 //#include<bits/stdc++.h>
      4 #include<iostream>
      5 #include<algorithm>
      6 #include<string>
      7 #include<iomanip>
      8 #include<map>
      9 #include<memory.h>
     10 #include<time.h>
     11 #include<stdio.h>
     12 #include<stdlib.h>
     13 #include<string.h>
     14 //#include<stdbool.h>
     15 #include<math.h>
     16 #define min(a,b) ((a)<(b)?(a):(b))
     17 #define max(a,b) ((a)>(b)?(a):(b))
     18 #define abs(a) ((a)>0?(a):(-(a)))
     19 #define lowbit(a) (a&(-a))
     20 #define sqr(a) ((a)*(a))
     21 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
     22 #define mem(a,b) memset(a,b,sizeof(a))
     23 #define eps (1e-8)
     24 #define J 10
     25 #define mod 1000000007
     26 #define MAX 0x7f7f7f7f
     27 #define PI 3.14159265358979323
     28 #define N 1004
     29 #define M 100005
     30 using namespace std;
     31 typedef long long LL;
     32 int cas,cass;
     33 int n,m,lll,ans;
     34 struct xxx
     35 {
     36     int next,to,q,x,y;;
     37 }a[M];
     38 int last[M],an[M];
     39 bool mapp[N][N];
     40 void add(int x,int y)
     41 {
     42     a[++lll].next=last[x];
     43     a[lll].to=y;
     44     last[x]=lll;
     45 }
     46 void dfs(int now,int sum)
     47 {
     48     int i,j;
     49     an[now]=sum;
     50     for(i=last[now];i;i=a[i].next)
     51     {
     52         if(a[i].q==1)
     53         {
     54             if(mapp[a[i].x][a[i].y])
     55                 dfs(a[i].to,sum);
     56             else
     57             {
     58                 mapp[a[i].x][a[i].y]=1;
     59                 dfs(a[i].to,sum+1);
     60                 mapp[a[i].x][a[i].y]=0;
     61             }
     62         }
     63         else if(a[i].q==2)
     64         {
     65             if(mapp[a[i].x][a[i].y])
     66             {
     67                 mapp[a[i].x][a[i].y]=0;
     68                 dfs(a[i].to,sum-1);
     69                 mapp[a[i].x][a[i].y]=1;
     70             }
     71             else dfs(a[i].to,sum);
     72         }
     73         else if(a[i].q==3)
     74         {
     75             int k=0;
     76             for(j=1;j<=m;j++)
     77             {
     78                 if(mapp[a[i].x][j])k--;
     79                 else k++;
     80                 mapp[a[i].x][j]^=1;
     81             }
     82             dfs(a[i].to,sum+k);
     83             for(j=1;j<=m;j++)mapp[a[i].x][j]^=1;
     84         }
     85         else if(a[i].q==4)
     86             dfs(a[i].to,sum);
     87     }
     88 }
     89 int main()
     90 {
     91     #ifndef ONLINE_JUDGE
     92 //    freopen("1.txt","r",stdin);
     93 //    freopen("2.txt","w",stdout);
     94     #endif
     95     int i,j,k;
     96     int x,y;
     97 //    for(scanf("%d",&cas);cas;cas--)
     98 //    for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
     99 //    while(~scanf("%s",s+1))
    100     while(~scanf("%d",&n))
    101     {
    102         //lll=0;mem(fa,0);mem(last,0);
    103         scanf("%d%d",&m,&cas);
    104         for(i=1;i<=cas;i++)
    105         {
    106             scanf("%d",&a[i].q);
    107             if(a[i].q<3)
    108                 scanf("%d%d",&a[i].x,&a[i].y);
    109             else if(a[i].q==3)
    110                 scanf("%d",&a[i].x);
    111             else if(a[i].q==4)
    112             {
    113                 scanf("%d",&a[i].x);
    114                 add(a[i].x,i);
    115                 continue;
    116             }
    117             add(i-1,i);
    118         }
    119         dfs(0,0);
    120         for(i=1;i<=cas;i++)
    121             printf("%d
    ",an[i]);
    122         puts("");
    123     }
    124     return 0;
    125 }
    126 /*
    127 //
    128 
    129 //
    130 */
    View Code
  • 相关阅读:
    java 数组转list的两种方式(可新增和删除list元素)
    SpringBoot配置404跳转页面的两种方式
    idea java常量字符串过长解决办法
    Spring-BeanValidation校验@RequestParam参数 (控制器单参数验证)
    【Java】使用@Valid+BindingResult进行controller参数校验
    Spring MVC利用Hibernate Validator实现后端数据校验
    springMvc 整合hibernate-validator(简单配置)
    vue中动态给自定义属性data-xx赋值并读取内容
    Tomcat配置SSL安全证书
    springmvc 接收json对象的两种方式
  • 原文地址:https://www.cnblogs.com/Coolxxx/p/5792237.html
Copyright © 2011-2022 走看看