bzoj 1924: [Sdoi2010]所驼门王的宝藏

23333这个垃圾题之前扒过题解了2333

然而这一次做又错了。。。

直接暴力连边,然后缩一下点,重构变成DAG,然后DP。。

(然而在打tarjan的时候忘掉了inq[now]=0....而且把DP搞成了搜索2333)

(附:此题内存玄学)

  1 #include<cstdio>
  2 #include<iostream>
  3 #include<algorithm>
  4 #include<map>
  5 #define N 1000005
  6 #define LL long long
  7 using namespace std;
  8 inline int ra()
  9 {
 10     int x=0,f=1; char ch=getchar();
 11     while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
 12     while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
 13     return x*f;
 14 }
 15 struct in_data{
 16     int x,y,id,opt;
 17 }a[N];
 18 struct edge{
 19     int to,next;
 20 }e[N<<2],re[N];
 21 int head[N],rehead[N>>1],cnt,recnt,ind,top,n,x,y;
 22 bool inq[N],vis[N>>1];
 23 map<LL , int > hh;
 24 int ans; 
 25 int q[N],dfn[N],low[N];
 26 int size[N>>1],belong[N],tot;
 27 bool cmp_x(in_data a, in_data b){
 28     return a.x<b.x;
 29 }
 30 bool cmp_y(in_data a, in_data b){
 31     return a.y<b.y;
 32 }
 33 void insert(int x, int y){
 34     e[++cnt].to=y; e[cnt].next=head[x]; head[x]=cnt;
 35 }
 36 void reinsert(int x, int y){
 37     re[++recnt].to=y; re[recnt].next=rehead[x]; rehead[x]=recnt;
 38 }
 39 void tarjan(int x)
 40 {
 41     q[++top]=x; dfn[x]=low[x]=++ind;
 42     inq[x]=1;
 43     for (int i=head[x];i;i=e[i].next)
 44         if (!dfn[e[i].to])
 45         {
 46             tarjan(e[i].to);
 47             low[x]=min(low[x],low[e[i].to]);
 48         }
 49         else if (inq[e[i].to]) low[x]=min(low[x],dfn[e[i].to]);
 50     int now=-1;
 51     if (dfn[x]==low[x])
 52     {
 53         tot++;
 54         while (now!=x)
 55         {
 56             now=q[top--];
 57             belong[now]=tot;
 58             size[tot]++;
 59             inq[now]=0;    //........
 60         }
 61     }
 62 }
 63 void rebuild()
 64 {
 65     for (int x=1; x<=n; x++)
 66         for (int i=head[x];i;i=e[i].next)
 67             if (belong[x]!=belong[e[i].to])    
 68                 reinsert(belong[x],belong[e[i].to]);
 69 }
 70 void dfs(int x)
 71 {
 72     vis[x]=1; int mx=0;
 73     for (int i=rehead[x];i;i=re[i].next)
 74     {
 75         if (!vis[re[i].to]) dfs(re[i].to);
 76         mx=max(size[re[i].to],mx);
 77     }
 78     ans=max(ans,size[x]+=mx);
 79 }
 80 int main()
 81 {
 82     n=ra(); x=ra(); y=ra();
 83     for (int i=1; i<=n; i++) a[i].x=ra(),a[i].y=ra(),a[i].opt=ra(),hh[(LL)a[i].x*N+a[i].y]=i,a[i].id=i;
 84     sort(a+1,a+n+1,cmp_x);
 85     for (int i=1; i<=n; i++)
 86         if (a[i].opt==1)
 87         {
 88             for (int j=i-1; j>=1; j--)
 89                 if (a[i].x==a[j].x) insert(a[i].id,a[j].id);
 90                     else break;
 91             for (int j=i+1; j<=n; j++)
 92                 if (a[i].x==a[j].x) insert(a[i].id,a[j].id);
 93                     else break;
 94         }
 95     sort(a+1,a+n+1,cmp_y);
 96     for (int i=1; i<=n; i++)
 97         if (a[i].opt==2)
 98         {
 99             for (int j=i-1; j>=1; j--)
100                 if (a[i].y==a[j].y) insert(a[i].id,a[j].id);
101                     else break;
102             for (int j=i+1; j<=n; j++)
103                 if (a[i].y==a[j].y) insert(a[i].id,a[j].id);
104                     else break;
105         }
106     for (int i=1; i<=n; i++)
107         if (a[i].opt==3)
108         {
109             for (int j=a[i].x-1; j<=a[i].x+1; j++)
110                 for (int k=a[i].y-1; k<=a[i].y+1; k++)
111                     if (j!=a[i].x || k!=a[i].y)
112                         if (hh[(LL)j*N+k]) insert(a[i].id,hh[(LL)j*N+k]);
113         }
114     for (int i=1; i<=n; i++)
115         if (!dfn[i]) tarjan(i);
116     rebuild();
117     for (int i=1; i<=tot; i++)
118         if (!vis[i]) dfs(i);
119     printf("%d\n",ans);
120     return 0;
121 }

 

posted @ 2017-03-03 21:21  ws_ccd  阅读(352)  评论(0编辑  收藏  举报