Fork me on GitHub

[SDOI2010] 所驼门王的宝藏

一道思维难度很简单,代码实现难度非常$mmp$的毒瘤题…..(这很$SDOI…..$)

题目连接:SDOI2010 所驼门王的宝藏

好,我们直接跳过题目背景。

然后我们会发现,如果这道题能把图建出来,那么直接一遍$DAG$上$DP$就过了。

蒽,这不是$sb$题吗?

然后让我们看数据范围:$1<=n<=1000000$

笑容逐渐消失

但是我们考虑一下,既然我们要建一张$DAG$。那么,我们完全没有必要对所有的点进行两两连边。
我们只需要对每一行和每一列进行连边,把他们连成一个环,这样的话,建边复杂度就会从$O(n^2)$变成$O(n)$了。

然后对于自由门的情况,对不起,我不知道怎么优化,所以我决定暴力建边。 (分析可以发现即使全是自由门,你暴力建边也不会$TLE$)

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <stack>
#include <map>

const int N = 1e6 + 10;
const int M = 5e6 + 10;

using std :: map;
using std :: stack;

int ans = -0x3f3f3f3f;
int n , r , c , t;
int dx[] = { 0 , 0 , 1 , 1 , 1 , -1 , -1 , -1 };
int dy[] = { 1 , -1 , 1 , 0 , -1 , 1 , 0 , -1 };
struct Node {
int x;
int y;
int opts;
int id;
}p[N];
struct Edge {
int from;
int to;
int next;
}e[M];
int idx , Bcnt;
int head[N] , dfn[N] , low[N];
int Belong[N] , size[N] , f[N];
int degree[N];
bool instack[N];
map < std :: pair < int , int > , int > mp;
map < std :: pair < int , int > , int > mat;
stack < int > st;

inline int read () {
int s = 0 , w = 1;
char ch = getchar ();
while ( ch > '9' || ch < '0' ) { if ( ch == '-' ) w = -1; ch = getchar ();}
while ( ch >= '0' && ch <= '9' ) { s = s * 10 + ch - '0'; ch = getchar ();}
return s * w;
}
template < class T >
inline T min ( T x , T y ) {
return x < y ? x : y;
}
template < class T >
inline T max ( T x , T y ) {
return x > y ? x : y;
}
inline void add ( int x , int y ) {
e[++t].to = y;
e[t].from = x;
e[t].next = head[x];
head[x] = t;
return;
}
bool cmp1 ( Node a , Node b ) {
if ( a.x != b.x )
return a.x < b.x;
if ( a.opts == 1 )
return 1;
if ( b .opts == 1 )
return 0;
return a.y < b.y;
}
bool cmp2 ( Node a , Node b ) {
if ( a.y != b.y )
return a.y < b.y;
if ( a.opts == 2 )
return 1;
if ( b.opts == 2 )
return 0;
return a.x < b.x;
}
void Tarjan ( int cur ) {
dfn[cur] = low[cur] = ++idx;
instack[cur] = 1;
st.push ( cur );
for ( int i = head[cur] ; i ; i = e[i].next ) {
int j = e[i].to;
if ( !dfn[j] ) {
Tarjan ( j );
low[cur] = min ( low[cur] , low[j] );
}
else if ( instack[j] )
low[cur] = min ( low[cur] , dfn[j] );
}
int k;
if ( low[cur] == dfn[cur] ) {
Bcnt++;
do {
k = st.top ();;
st.pop ();
instack[k] = 0;
Belong[k] = Bcnt;
size[Bcnt]++;
} while ( k != cur );
}
return;
}
void dfs ( int cur , int father ) {
if ( f[cur] > size[cur] )
return;
f[cur] = size[cur];
for ( int i = head[cur] ; i ; i = e[i].next ) {
int j = e[i].to;
if ( j == father )
continue;
dfs ( j , cur );
f[cur] = max ( f[cur] , f[j] + size[cur] );
}
return;
}

int main ( void ) {
n = read () , r = read () , c = read ();
for ( int i = 1 ; i <= n ; i++ ) {
p[i].x = read ();
p[i].y = read ();
p[i].opts = read ();
p[i].id = i;
mp[std::make_pair ( p[i].x , p[i].y )] = i;
}
std :: sort ( p + 1 , p + n + 1 , cmp1 );
int first = 1 , last = 1;
for ( int i = 1 ; i <= n ; i++ ) {
if ( p[i].x != p[i + 1].x ) {
if ( first != last )
add ( p[last].id , p[first].id );
last = first = i + 1;
}
else {
if ( p[last].opts == 1 )
add ( p[last].id , p[i + 1].id );
if ( p[i + 1].opts == 1 )
last = i + 1;
if ( p[first].opts != 1 )
last = first = i + 1;
}
}
first = last = 1;
std :: sort ( p + 1 , p + 1 + n , cmp2 );
for ( int i = 1 ; i <= n ; i++ ) {
if ( p[i].y != p[i + 1].y ) {
if ( first != last )
add ( p[last].id , p[first].id );
last = first = i + 1;
}
else {
if ( p[last].opts == 2 )
add ( p[last].id , p[i + 1].id );
if ( p[i + 1].opts == 2 )
last = i + 1;
if ( p[first].opts != 2 )
last = first = i + 1;
}
}
//printf ( "%d\n" , t );
for ( int i = 1 ; i <= n ; i++ )
if ( p[i].opts == 3 )
for ( int j = 0 ; j < 8 ; j++ ) {
int xx = p[i].x + dx[j];
int yy = p[i].y + dy[j];
if ( mp[std :: pair < int , int > ( xx , yy )] )
add ( p[i].id , mp[std :: pair < int , int > ( xx , yy )] );
}
// printf ( "%d\n" , t );
// for ( int i = 1 ; i <= t ; i++ )
// printf ( "%d %d\n" , e[i].from , e[i].to );
for ( int i = 1 ; i <= n ; i++ )
if ( !Belong[i] )
Tarjan ( i );
for ( int i = 1 ; i <= n ; i++ )
for ( int j = head[i] ; j ; j = e[j].next ) {
int k = e[j].to;
if ( Belong[i] != Belong[k] )
mat[std :: make_pair ( Belong[i] , Belong[k] )] = 1;
}
t = 0;
memset ( head, 0 , sizeof ( head ) );
for ( auto it = mat.begin () ; it != mat.end() ; it++ ) {
add ( it -> first.first , it -> first.second );
degree[it -> first.second]++;
}
// for ( int i = 1 ; i <= Bcnt ; i++ )
// printf ( "%d " , degree[i] );
for ( int i = 1 ; i <= Bcnt ; i++ )
if ( degree[i] == 0 ) {
dfs ( i , 0 );
ans = max ( ans , f[i] );
}
printf ( "%d\n" , ans );
return 0;
}
因为知道了自己是多么的菜,所以才要更加努力去追求那个永远也不可能实现的梦想

本文标题:[SDOI2010] 所驼门王的宝藏

文章作者:KRrrrrrrrr

发布时间:2018年11月05日 - 10:11

最后更新:2019年10月21日 - 11:10

原始链接:http://krrrr.xyz/2018/11/05/i-t/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。