Bzoj 1924: [Sdoi2010] The treasure of the Camel King "Tarjan"

Source: Internet
Author: User

Description

In the vast desert of Africa, a group of industrious and courageous alpaca family lived. Alpaca L. Sotomon, known as the Prophet by the people, is the leader of the family, and outsiders call it "the King of the Camel." The Camel King devoted his life to maintaining the stability and harmony of the family, he had personally rate the army to crush the savage invasion of the river Crab imperialism, for the people of the illustrious exploits. The king of his life treasures countless, but because of his frugal low-key, he buried the treasure in his own design of the underground palace, which is today's Henry Curtis story starting point. Henry was a skinflint greedy fellow, and very clever, and he had the time to plot the theft, to crack a lot of institutions to come to this underground palace. The whole palace is matrix-shaped, composed of rxc rectangular palace, of which there is a treasure buried in the N Palace, known as the Palace of the Tibetan treasure. The palace inside and outside, the adjacent palace between the hard solid wall barrier, from one palace to another can only through the king of the original mobile way-portal. The camel door King Absalom this n treasure Palace each room is set up a portal, no treasure Palace no portal, all the palace portals are divided into three kinds:

1. "Heng Tian Men": from the door can be transmitted to any one of the palace;

2. "Longitudinal portal": by the door can be transmitted to the same column of any palace;

3. "From You": the door can be transferred to the palace of the gate around the center of any one of the 8 palace (if the target palace exist).

The farsighted Henry had, of course, got the King's Palace in advance. The tender book of the same year, detailed records of each portal belongs to the palace and type. And, although apart from inside and outside the palace, he prepared himself a portable portal that could transmit himself to any palace in the temple and begin to treasure it, and deliver it after the end of any palace. The entire palace is only allowed in and out once, and the portable door cannot be transferred between palace. But fortunately, the use of indoor portal is not limited to the number of times, each palace can also be multiple access. Now Henry has opened the portable door and is about to choose a palace to enter. In order to get as many treasures as possible, he wanted to arrange a route so that the different treasure Palace traveled as much. Please tell Henry that this route is up to the number of different treasure Palace.

Input

The first line gives three positive integers N, R, C. The following N lines, each row gives information about a portal, containing three positive integers xi, Yi, ti, indicating that the portal is located in the Second XI Line Yi column of the Treasure Palace, type Ti. TI is an integer between the two, 1 means that can be transferred to any column XI line of "Tianmen", 2 means that can be transferred to any row of Yi column "longitudinal portal", 3 means can be transferred to the surrounding 8 lattice palace "from you door." Guaranteed 1≤xi≤r,1≤yi≤c, all portal locations are different.

Output

There is only a positive integer that indicates the maximum number of different treasure palace that you have determined for the route.

Sample Input

10 7 7
2 2 1
2 4 2
1 7 2
2 7 3
4 2 2
4 4 1
6 7 3
7 7 1
7 5 2
5 2 1

Sample Output

9

HINT

Test point number N R C 1 16 20 20 2 300 1,000 1,000 3 500 100,000 100,000 4 2,500 5,000 5,000 5 50,000 5,000 5,000 6 50,000 1,000,000 1,000,000 7 80,000 1,000,000 1,000,000 8 100,000 1,000,000 1,000,000 9 100,000 1,000,000 1,000,000 10 100,000 1,000,000 1 , 000,000

Idea: At first, I thought we didn't give the data range, and after a few rounds of re, we found the test point number is the data range.

With each portal as a node edge, the way to memorize the search for the most points after shrinking points

#include <cstdio>

#include <string.h>

#include <iostream>

#include <algorithm>

#include <queue>

#include <map>

#define MAXN 400009

#define MAXM 4000009

using namespace Std;

const int DX[10]={0,0,0,1,1,1,-1,-1,-1};

const int dy[10]={0,1,-1,-1,0,1,-1,0,1};

int XX[MAXN],YY[MAXN],VV[MAXN];

Vector<vector<int> >line (MAXN), row (MAXN);

int Head[maxn],next[maxm],point[maxm],now;

int head2[maxn],next2[maxm],point2[maxm],now2;

int dfn[maxn],low[maxn],col,tim,stack[maxn],top;

int POIN[MAXN],BELONG[MAXN];

int DP[MAXN];

BOOL INSTACK[MAXN];

map<pair<int,int>,int>mapp;

void Add (int x,int y) {

Next[++now]=head[x];head[x]=now;point[now]=y;

}

void Tarjan (int k)

{

Dfn[k]=low[k]=++tim;

Stack[++top]=k;instack[k]=1;

int u;

for (int i=head[k];i;i=next[i])

{

U=point[i];

if (dfn[u]==0) {

Tarjan (U);

Low[k]=min (Low[k],low[u]);

}

else if (Instack[u]) low[k]=min (Low[k],low[u]);

}

if (Dfn[k]==low[k])

{

col++;

do{

u=stack[top--];

Belong[u]=col;

instack[u]=0;

poin[col]++;

}while (U!=K);

}

}

void add2 (int x,int y) {

Next2[++now2]=head2[x];head2[x]=now2;point2[now2]=y;

}

int dfs (int k)

{

if (dp[k]!=-1) return dp[k];

int ret=0;

for (int i=head2[k];i;i=next2[i])

{

int u=point2[i];

Ret=max (Ret,dfs (U));

}

return dp[k]=ret+poin[k];

}

int main ()

{

int n,r,c;

scanf ("%d%d%d", &n,&r,&c);

for (int i=1;i<=n;i++)

{

scanf ("%d%d%d", &xx[i],&yy[i],&vv[i]);

Line[xx[i]].push_back (i);

Row[yy[i]].push_back (i);

Mapp[make_pair (Xx[i],yy[i])]=i;

}

for (int i=1;i<=n;i++)

{

if (vv[i]==1)

{

int u=line[xx[i]].size ();

for (int j=0;j<u;j++) if (line[xx[i]][j]!=i)

{

Add (I,line[xx[i]][j]);

}

}

else if (vv[i]==2)

{

int u=row[yy[i]].size ();

for (int j=0;j<u;j++) if (row[yy[i]][j]!=i)

{

Add (I,row[yy[i]][j]);

}

}

else for (int j=1;j<=8;j++)

{

int X=XX[I]+DX[J],Y=YY[I]+DY[J];

int U=mapp[make_pair (x, y)];

if (u!=0) Add (i,u);

}

}

for (int i=1;i<=n;i++) if (dfn[i]==0) Tarjan (i);

for (int i=1;i<=n;i++)

{

for (int j=head[i];j;j=next[j])

{

int U=POINT[J];

if (Belong[i]!=belong[u])

ADD2 (Belong[i],belong[u]);

}

}

int ans=0;

Memset (Dp,-1,sizeof (DP));

for (int i=1;i<=col;i++)

{

Ans=max (Ans,dfs (i));

}

printf ("%d\n", ans);

return 0;

}

Bzoj 1924: [Sdoi2010] The treasure of the Camel King "Tarjan"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.