-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
226 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#include <algorithm> | ||
#include <cstdio> | ||
#include <cstring> | ||
const int size = 2005; | ||
struct Edge { | ||
int to, nxt, w; | ||
} E[size * 2]; | ||
int last[size], cnt = 0; | ||
void addEdge(int u, int v, int w) { | ||
++cnt; | ||
E[cnt].to = v, E[cnt].nxt = last[u], E[cnt].w = w; | ||
last[u] = cnt; | ||
} | ||
int n, k, nk; | ||
typedef long long Int64; | ||
#define asInt64 static_cast<Int64> | ||
Int64 dp[size][size]; | ||
void CAS(Int64& a, Int64 b) { | ||
a = std::max(a, b); | ||
} | ||
int DFS(int u, int p) { | ||
int siz = 1; | ||
for(int i = last[u]; i; i = E[i].nxt) { | ||
int v = E[i].to; | ||
if(v == p) | ||
continue; | ||
int vsiz = DFS(v, u); | ||
siz += vsiz; | ||
int vend = std::min(vsiz, k); | ||
for(int j = 0; j <= vend; ++j) { | ||
int wc = vsiz - j; | ||
dp[v][j] += asInt64(E[i].w) * | ||
(j * (k - j) + wc * (nk - wc)); | ||
} | ||
int end = std::min(siz, k), | ||
oend = std::min(siz - vsiz, k); | ||
for(int j = end; j >= 0; --j) { | ||
int fbeg = std::max(0, j - vend), | ||
fend = std::min(oend, j); | ||
for(int l = fend; l >= fbeg; --l) | ||
CAS(dp[u][j], dp[u][l] + dp[v][j - l]); | ||
} | ||
} | ||
return siz; | ||
} | ||
int main() { | ||
scanf("%d%d", &n, &k); | ||
nk = n - k; | ||
for(int i = 1; i < n; ++i) { | ||
int u, v, w; | ||
scanf("%d%d%d", &u, &v, &w); | ||
addEdge(u, v, w); | ||
addEdge(v, u, w); | ||
} | ||
DFS(1, 0); | ||
printf("%lld\n", dp[1][k]); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
#include <algorithm> | ||
#include <cstdio> | ||
#include <cstdlib> | ||
#include <cstring> | ||
namespace IO { | ||
char in[1 << 19], *S; | ||
void init() { | ||
fread(in, 1, sizeof(in), stdin); | ||
S = in; | ||
} | ||
int scanInt() { | ||
return strtol(S, &S, 10); | ||
} | ||
void scanMat(char* out, int n, int m) { | ||
for(int i = 0; i < n; ++i) { | ||
S += (*S == '\r' ? 2 : 1); | ||
memcpy(out, S, m); | ||
out += m, S += m; | ||
} | ||
} | ||
} | ||
const int size = 100005; | ||
struct Edge { | ||
int to, nxt, f; | ||
} E[size * 8]; | ||
int last[size], cnt = 1; | ||
void addEdgeImpl(int u, int v, int f) { | ||
++cnt; | ||
E[cnt].to = v, E[cnt].nxt = last[u], E[cnt].f = f; | ||
last[u] = cnt; | ||
} | ||
void addEdge(int u, int v) { | ||
addEdgeImpl(u, v, 1); | ||
addEdgeImpl(v, u, 0); | ||
} | ||
int d[size], q[size], S, T; | ||
bool BFS(int siz) { | ||
memset(d + 1, -1, sizeof(int) * siz); | ||
d[S] = 0, q[0] = S; | ||
int b = 0, e = 1; | ||
while(b != e) { | ||
int u = q[b++]; | ||
for(int i = last[u]; i; i = E[i].nxt) { | ||
int v = E[i].to; | ||
if(E[i].f && d[v] == -1) { | ||
d[v] = d[u] + 1; | ||
if(v == T) | ||
return true; | ||
q[e++] = v; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
int now[size]; | ||
int DFS(int u, int f) { | ||
if(u == T || f == 0) | ||
return f; | ||
int res = 0, k; | ||
for(int& i = now[u]; i; i = E[i].nxt) { | ||
int v = E[i].to; | ||
if(d[v] == d[u] + 1 && | ||
(k = DFS(v, std::min(f, E[i].f)))) { | ||
E[i].f -= k, E[i ^ 1].f += k; | ||
res += k, f -= k; | ||
if(f == 0) | ||
break; | ||
} | ||
} | ||
if(res == 0) | ||
d[u] = -1; | ||
return res; | ||
} | ||
int dinic(int siz) { | ||
int res = 0; | ||
while(BFS(siz)) { | ||
memcpy(now + 1, last + 1, sizeof(int) * siz); | ||
res += DFS(S, 1 << 30); | ||
} | ||
return res; | ||
} | ||
char map[size]; | ||
int id[size]; | ||
int main() { | ||
IO::init(); | ||
int n = IO::scanInt(); | ||
int m = IO::scanInt(); | ||
IO::scanMat(map, n, m); | ||
int siz = n * m, res = 0, icnt = 0; | ||
for(int i = 0; i < siz; ++i) { | ||
if(map[i] == '2') | ||
++res; | ||
else if(map[i] != '*') | ||
id[i] = ++icnt; | ||
} | ||
S = ++icnt; | ||
T = ++icnt; | ||
for(int i = 0, p = 0; i < n; ++i) | ||
for(int j = 0; j < m; ++j, ++p) { | ||
char cu = map[p]; | ||
if(!(cu & 1)) | ||
continue; | ||
int cid = id[p]; | ||
if(cu == '1') | ||
addEdge(S, cid); | ||
else | ||
addEdge(cid, T); | ||
if(cu == '1') { | ||
if(j && map[p - 1] == '3') | ||
addEdge(cid, id[p - 1]); | ||
if(j != m - 1 && map[p + 1] == '3') | ||
addEdge(cid, id[p + 1]); | ||
if(i && map[p - m] == '3') | ||
addEdge(cid, id[p - m]); | ||
if(i != n - 1 && map[p + m] == '3') | ||
addEdge(cid, id[p + m]); | ||
} | ||
} | ||
printf("%d\n", res + dinic(icnt)); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
最大流:LOJ101 | ||
费用流:LOJ102 | ||
最小割树:LOJ2042 | ||
李超树:BZOJ1568 | ||
LCT:LOJ558 | ||
可并堆:LuoguP3377 | ||
线段树分治:LOJ534 | ||
Miller-Rabin:LOJ143 | ||
ExCRT:LuoguP4477 | ||
ExLucas:LuoguP4720 | ||
杜教筛:LOJ6491 | ||
Min25筛:LOJ6053 | ||
FFT:UOJ34 | ||
FWT:LuoguP5387 | ||
矩阵求逆:LuoguP4783 | ||
Simpson积分:LuoguP4525 | ||
生成函数+多项式Exp:LOJ556 | ||
单位根反演:LOJ6485 | ||
动态DP:LuoguP4751 | ||
斜率优化+WQS二分:bzoj3675 | ||
分治DP:LOJ6039 | ||
折半状压:LOJ2264 | ||
长链剖分:CF1009F | ||
Dsu On Tree:CF570D | ||
虚树+广义圆方树:LOJ2562 | ||
点分治:LuoguP3806 | ||
Kruskal重构树:LOJ2718 | ||
MatrixTree:LOJ6271 | ||
2-SAT:bzoj3495 | ||
圆方树:bzoj2125 | ||
ACM:LOJ2444 | ||
卷积字符串匹配+Manacher:bzoj3160 | ||
PAM:bzoj3676 | ||
启发式分裂+SA:LOJ6198 | ||
广义SAM+根号特技:bzoj3277 | ||
凸包:bzoj2829 | ||
快速凸包+生成树:bzoj2395 | ||
半平面交:LOJ2008 | ||
最小圆覆盖:LOJ6360 | ||
分数规划:bzoj1758 | ||
线性规划:bzoj1061 | ||
回滚莫队:LOJ2874 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,3 +58,4 @@ | |
| 2019/02/08 | 228 | | ||
| 2019/02/09 | 217 | | ||
| 2019/03/28 | 243 | | ||
| 2019/05/22 | 261 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters