bzoj 1179 1202 1207 1257 1293 1295 1303 1912 2257 3097 4198 和一个调了两个小时的傻逼模板

master
大蒟蒻 9 years ago
parent 083fa23f2e
commit eaf3fc044c

@ -0,0 +1,72 @@
#include <cstdio>
#include <cstring>
inline int min(int a, int b) { return a < b ? a : b; }
const int maxn = 210;
int head[maxn], from[maxn << 1], to[maxn << 1], cap[maxn << 1], next[maxn << 1], ecnt, m, n;
int d[maxn], que[maxn], cur[maxn], num[maxn], fa[maxn];
inline void addEdge(int f, int t, int c)
{
next[ecnt] = head[f];
head[f] = ecnt;
from[ecnt] = f;
to[ecnt] = t;
cap[ecnt] = c;
ecnt++;
}
int ISAP(int s, int e)
{
int flow = 0;
int h = 0, t = 0, x;
for (int i = 1; i <= n; i++) d[i] = n;
d[que[t++] = e] = 0;
while (h != t) for (int i = head[x = que[h++]]; i; i = next[i])
if (cap[i ^ 1] > 0 && d[to[i]] > d[x] + 1)
d[que[t++] = to[i]] = d[x] + 1;
memset(num, 0, sizeof(num));
for (int i = 1; i <= n; i++) num[d[i]]++, cur[i] = head[i];
x = s;
while (d[s] < n)
{
if (x == e)
{
int curFlow = 0x3f3f3f3f;
for (x = e; x != s; x = from[fa[x]])
curFlow = min(curFlow, cap[fa[x]]);
for (x = e; x != s; x = from[fa[x]])
cap[fa[x]] -= curFlow, cap[fa[x] ^ 1] += curFlow;
flow += curFlow;
x = s;
}
bool needRetreat = true;
for (int i = cur[x]; needRetreat && ~i; i = next[i])
if (cap[i] && d[to[i]] == d[x] - 1)
needRetreat = false, cur[x] = i, fa[x = to[i]] = i;
if (needRetreat)
{
int mn = n - 1;
for (int i = head[x]; ~i; i = next[i])
if (cap[i]) mn = min(mn, d[to[i]]);
if (--num[d[x]] == 0) break;
num[d[x] = mn + 1]++;
cur[x] = head[x];
if (x != s) x = from[fa[x]];
}
}
return flow;
}
int main()
{
while (~scanf("%d%d", &m, &n))
{
memset(head, -1, sizeof(head));
ecnt = 0;
for (int i = 0, x, y, z; i < m; i++)
{
scanf("%d%d%d", &x, &y, &z);
addEdge(x, y, z);
addEdge(y, x, 0);
}
printf("%d\n", ISAP(1, n));
}
return 0;
}

@ -1,105 +1,105 @@
#include <cstdio>
#include <cstring>
struct Edge
{
int from, to, cap;
Edge *next, *rev;
Edge(int f, int t, int c) :from(f), to(t), cap(c), next(0), rev(0) {}
};
typedef Edge* lpEdge;
inline int min(int a, int b) { return (a < b ? a : b); }
lpEdge addEdge(lpEdge* G, int from, int to, int cap)
{
lpEdge newEdge = new Edge(from, to, cap);
newEdge->next = G[from];
G[from] = newEdge;
return newEdge;
}
const int maxn = 201, inf = 0x3f3f3f3f;
int m, n;
int num[maxn], d[maxn];
lpEdge G[maxn], cur[maxn], fa[maxn];
int ISAP(int s, int e, int cnt)
{
int flow = 0, x;
int* que = new int[maxn];
int head, tail;
head = tail = 0;
for (int i = 0; i < cnt; i++) d[i] = cnt;
d[e] = 0;
que[tail++] = e;
while (head < tail)
{
x = que[head++];
for (lpEdge i = G[x]; i; i = i->next)
if (i->rev->cap > 0 && d[i->to] >= cnt)
que[tail++] = i->to,
d[i->to] = d[x] + 1;
}
delete[] que;
memset(cur, 0, sizeof(cur));
memset(num, 0, sizeof(num));
for (int i = 0; i < cnt; i++) num[d[i]]++;
x = s;
while (d[s] < cnt)
{
if (x == e)
{
int curFlow = inf;
lpEdge curEdge;
while (x != s)
{
curEdge = fa[x];
curFlow = min(curFlow, curEdge->cap);
x = curEdge->from;
}
x = e;
while (x != s)
{
curEdge = fa[x];
curEdge->cap -= curFlow;
curEdge->rev->cap += curFlow;
x = curEdge->from;
}
flow += curFlow;
x = s;
}
bool needRetreat = true;
for (lpEdge& i = (cur[x] ? cur[x] : cur[x] = G[x]); i; i = i->next)
if (i->cap > 0 && d[x] == d[i->to] + 1)
{
fa[i->to] = i;
x = i->to;
needRetreat = false; break;
}
if (needRetreat)
{
int dis = cnt - 1;
for (lpEdge i = G[x]; i; i = i->next)
if (i->cap)
dis = min(dis, d[i->to]);
if (--num[d[x]] == 0) break;
num[d[x] = dis + 1]++;
if (x != s) x = fa[x]->from;
}
}
return flow;
}
int main()
{
lpEdge edge1, edge2;
while (~scanf("%d%d", &m, &n))
{
memset(G, 0, sizeof(G));
for (int i = 0, s, e, c; i < m; i++)
{
scanf("%d%d%d", &s, &e, &c);
edge1 = addEdge(G, s, e, c);
edge2 = addEdge(G, e, s, 0);
edge1->rev = edge2;
edge2->rev = edge1;
}
printf("%d\n", ISAP(1, n, n + 1));
}
return 0;
}
#include <cstdio>
#include <cstring>
struct Edge
{
int from, to, cap;
Edge *next, *rev;
Edge(int f, int t, int c) :from(f), to(t), cap(c), next(0), rev(0) {}
};
typedef Edge* lpEdge;
inline int min(int a, int b) { return (a < b ? a : b); }
lpEdge addEdge(lpEdge* G, int from, int to, int cap)
{
lpEdge newEdge = new Edge(from, to, cap);
newEdge->next = G[from];
G[from] = newEdge;
return newEdge;
}
const int maxn = 201, inf = 0x3f3f3f3f;
int m, n;
int num[maxn], d[maxn];
lpEdge G[maxn], cur[maxn], fa[maxn];
int ISAP(int s, int e, const int cnt)
{
int flow = 0, x;
int* que = new int[maxn];
int head, tail;
head = tail = 0;
for (int i = 0; i < cnt; i++) d[i] = cnt;
d[e] = 0;
que[tail++] = e;
while (head < tail)
{
x = que[head++];
for (lpEdge i = G[x]; i; i = i->next)
if (i->rev->cap > 0 && d[i->to] >= cnt)
que[tail++] = i->to,
d[i->to] = d[x] + 1;
}
delete[] que;
memset(cur, 0, sizeof(cur));
memset(num, 0, sizeof(num));
for (int i = 0; i < cnt; i++) num[d[i]]++;
x = s;
while (d[s] < cnt)
{
if (x == e)
{
int curFlow = inf;
lpEdge curEdge;
while (x != s)
{
curEdge = fa[x];
curFlow = min(curFlow, curEdge->cap);
x = curEdge->from;
}
x = e;
while (x != s)
{
curEdge = fa[x];
curEdge->cap -= curFlow;
curEdge->rev->cap += curFlow;
x = curEdge->from;
}
flow += curFlow;
x = s;
}
bool needRetreat = true;
for (lpEdge& i = (cur[x] ? cur[x] : cur[x] = G[x]); i; i = i->next)
if (i->cap > 0 && d[x] == d[i->to] + 1)
{
fa[x = i->to] = i;
needRetreat = false;
break;
}
if (needRetreat)
{
int dis = cnt - 1;
for (lpEdge i = G[x]; i; i = i->next)
if (i->cap)
dis = min(dis, d[i->to]);
if (--num[d[x]] == 0) break;
num[d[x] = dis + 1]++;
if (x != s) x = fa[x]->from;
}
}
return flow;
}
int main()
{
lpEdge edge1, edge2;
while (~scanf("%d%d", &m, &n))
{
memset(G, 0, sizeof(G));
for (int i = 0, s, e, c; i < m; i++)
{
scanf("%d%d%d", &s, &e, &c);
edge1 = addEdge(G, s, e, c);
edge2 = addEdge(G, e, s, 0);
edge1->rev = edge2;
edge2->rev = edge1;
}
printf("%d\n", ISAP(1, n, n + 1));
}
return 0;
}

@ -0,0 +1,81 @@
#include <cstdio>
inline int min(int a, int b) { return a < b ? a : b; }
inline int max(int a, int b) { return a > b ? a : b; }
const int maxn = 500010;
int n, m, s, p;
struct Graph
{
int head[maxn], next[maxn], to[maxn], rnk[maxn], ecnt;
bool ispub[maxn];
inline void addEdge(int f, int t)
{
ecnt++;
next[ecnt] = head[f];
head[f] = ecnt;
to[ecnt] = t;
}
}g1, g2;
int low[maxn], dfn[maxn], scc[maxn], scccnt, idx, st[maxn], top, dis[maxn], que[maxn << 2];
bool inq[maxn];
void tarjan(int u)
{
low[u] = dfn[u] = ++idx;
st[top++] = u;
for (int i = g1.head[u]; i; i = g1.next[i])
if (!dfn[g1.to[i]]) tarjan(g1.to[i]), low[u] = min(low[u], low[g1.to[i]]);
else if (!scc[g1.to[i]])
low[u] = min(low[u], dfn[g1.to[i]]);
if (dfn[u] == low[u])
{
scccnt++;
do scc[st[--top]] = scccnt;
while (st[top] != u);
}
}
void spfa()
{
int h, t;
h = t = 0;
que[t++] = s;
dis[s] = g2.rnk[s];
inq[s] = true;
for (int x = 0; h ^ t; inq[que[h++]] = false)
for (int i = g2.head[x = que[h]]; i; i = g2.next[i])
if (dis[g2.to[i]] < g2.rnk[g2.to[i]] + dis[x])
{
dis[g2.to[i]] = g2.rnk[g2.to[i]] + dis[x];
if (!inq[g2.to[i]]) inq[que[t++] = g2.to[i]] = true;
}
}
int main()
{
scanf("%d%d", &n, &m);
for (int i = 1, x, y; i <= m; i++)
{
scanf("%d%d", &x, &y);
g1.addEdge(x, y);
}
for (int i = 1; i <= n; i++) scanf("%d", &g1.rnk[i]);
scanf("%d%d", &s, &p);
for (int i = 0, x; i < p; i++)
scanf("%d", &x), g1.ispub[x] = true;
for (int i = 1; i <= n; i++)
if (!dfn[i]) tarjan(i);
for (int i = 1; i <= n; i++)
{
if (g1.ispub[i]) g2.ispub[scc[i]] = true;
g2.rnk[scc[i]] += g1.rnk[i];
}
for (int i = 1; i <= n; i++)
for (int j = g1.head[i]; j; j = g1.next[j])
if (scc[i] != scc[g1.to[j]])
g2.addEdge(scc[i], scc[g1.to[j]]);
s = scc[s];
spfa();
int ans = 0;
for (int i = 1; i <= scccnt; i++)
if (g2.ispub[i])
ans = max(ans, dis[i]);
printf("%d", ans);
return 0;
}

@ -0,0 +1,37 @@
#include <cstdio>
int fa[105], s[105];
int Find(int x)
{
if (fa[x] != x)
{
int fx = fa[x];
fa[x] = Find(fa[x]);
s[x] += s[fx];
}
return fa[x];
}
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
bool flag = true;
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i <= n; i++) fa[i] = i, s[i] = 0;
for (int i = 0, x, y, z; i < m && flag; i++)
{
scanf("%d%d%d", &x, &y, &z); x--;
int fx = Find(x), fy = Find(y);
if (fx != fy)
{
fa[fx] = fy;
s[fx] = s[y] - s[x] + z;
}
else if (s[x] - s[y] != z) flag = false;
}
puts(flag ? "true" : "false");
}
return 0;
}

@ -0,0 +1,15 @@
#include <cstdio>
inline int abs(int x) { return x >= 0 ? x : -x; }
int t[100010], x[100010], y[100010], f[100010], n, m, ans;
int main()
{
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++) f[i] = 1, scanf("%d%d%d", t + i, x + i, y + i);
for (int i = 0; i < m; i++)
for (int j = 0; j < i; j++)
if (abs(x[i] - x[j]) + abs(y[i] - y[j]) <= abs(t[i] - t[j]) && f[i] < f[j] + 1)
f[i] = f[j] + 1;
for (int i = 0; i < m; i++) if (f[i] > ans) ans = f[i];
printf("%d", ans);
return 0;
}

@ -0,0 +1,14 @@
#include <cstdio>
long long n, k, ans;
int main()
{
scanf("%lld%lld", &n, &k);
long long sz = n < k ? n : k;
for (long long i = 1, last, t; i <= sz; i = last + 1)
{
t = k / i, last = k / t;
if (last >= n) last = n;
ans -= ((last - i + 1) * (i + last) >> 1) * t;
}
printf("%lld", ans + n * k);
}

@ -0,0 +1,27 @@
#include <cstdio>
#include <algorithm>
inline int min(int a, int b) { return a < b ? a : b; }
struct node
{
int pos, col;
bool operator<(const node & R) const { return pos < R.pos || (pos == R.pos&&col < R.col); }
}a[1000010];
int cnt[65];
int main()
{
int n, k, ncnt = 0;
scanf("%d%d", &n, &k);
for (int i = 0, ti; i < k; i++)
for (scanf("%d", &ti); ti; ti--, ncnt++)
a[ncnt].col = i, scanf("%d", &a[ncnt].pos);
std::sort(a, a + ncnt);
int ans = 0x7fffffff, ccnt = 0;
for (int i = 0, j = 0; i < n && j < n;)
{
while (i < n && ccnt < k) ccnt += cnt[a[i++].col]++ == 0;
while (j < n && ccnt == k) ccnt -= --cnt[a[j++].col] == 0;
ans = min(ans, a[i - 1].pos - a[j - 1].pos);
}
printf("%d", ans);
return 0;
}

@ -0,0 +1,61 @@
#include <cstdio>
#include <cmath>
#include <cstring>
struct point
{
int x, y;
}que[1 << 16 | 1];
const int dx[] = { 0, 0, 1, -1 };
const int dy[] = { 1, -1, 0, 0 };
int m, n, T, a[33][33], f[33][33];
char buf[35];
double ans;
bool inq[33][33];
inline double dis(const point &l, const point &r)
{
return sqrt((l.x - r.x) * (l.x - r.x) + (l.y - r.y) * (l.y - r.y));
}
void spfa(const point& s)
{
int h = 0, t = 0;
memset(f, 0x3f, sizeof(f));
memset(inq, 0, sizeof(inq));
que[t++] = s;
f[s.x][s.y] = a[s.x][s.y];
inq[s.x][s.y] = true;
for (point cur, tmp; h != t; h++)
{
cur = que[h];
inq[cur.x][cur.y] = false;
for (int i = 0; i < 4; i++)
{
tmp.x = cur.x + dx[i], tmp.y = cur.y + dy[i];
if (tmp.x >= 0 && tmp.x < m && tmp.y >= 0 && tmp.y < n)
if (f[tmp.x][tmp.y] > f[cur.x][cur.y] + a[tmp.x][tmp.y] &&
f[cur.x][cur.y] + a[tmp.x][tmp.y] <= T)
{
f[tmp.x][tmp.y] = f[cur.x][cur.y] + a[tmp.x][tmp.y];
if (!inq[tmp.x][tmp.y]) que[t++] = tmp, inq[tmp.x][tmp.y] = true;
}
}
}
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (f[i][j] <= T)
ans = fmax(ans, dis(s, { i,j }));
}
int main()
{
scanf("%d%d%d\n", &m, &n, &T);
for (int i = 0; i < n; i++)
{
scanf("%s", buf);
for (int j = 0; j < m; j++)
a[i][j] = buf[j] - '0';
}
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
spfa({ i, j });
printf("%.6lf", ans);
return 0;
}

@ -0,0 +1,22 @@
#include <cstdio>
const int maxn = 100010;
int n, k, p, a[maxn], l[maxn << 1], r[maxn << 1];
int main()
{
scanf("%d%d", &n, &k);
for (int i = 1, x; i <= n; i++)
{
scanf("%d", &x);
if (x < k) a[i] = -1;
if (x > k) a[i] = 1;
if (x == k) p = i;
}
for (int i = 1; i <= n; i++) a[i] += a[i - 1];
for (int i = 0; i < p; i++) l[a[i] + n]++;
for (int i = p; i <= n; i++) r[a[i] + n]++;
int ans = 0;
for (int i = n << 1; i >= 0; i--)
ans += l[i] * r[i];
printf("%d", ans);
return 0;
}

@ -0,0 +1,59 @@
#include <cstdio>
#include <cstring>
int n, k, head[100010], next[100010 << 1], from[100010 << 1], to[100010 << 1], len[100010 << 1],
maxlen, ecnt, fa[100010], mx1[100010], mx2[100010], en;
void addEdge(int f, int t)
{
next[ecnt] = head[f];
head[f] = ecnt;
to[ecnt] = t;
from[ecnt] = f;
len[ecnt] = 1;
ecnt++;
}
int dfs(int u, int fa)
{
int max1 = 0, max2 = 0;
for (int i = head[u]; ~i; i = next[i])
if (to[i] != fa)
{
int tmp = len[i] + dfs(to[i], u);
if (tmp > max1)
{
max2 = max1;
max1 = tmp;
mx2[u] = mx1[u];
mx1[u] = i;
}
else if (tmp > max2)
{
max2 = tmp;
mx2[u] = i;
}
}
if (maxlen < max1 + max2) maxlen = max1 + max2, en = u;
return max1;
}
int main()
{
memset(head, -1, sizeof(head));
scanf("%d%d", &n, &k);
for (int i = 1, x, y; i < n; i++)
{
scanf("%d%d", &x, &y);
addEdge(x, y);
addEdge(y, x);
}
dfs(1, 0);
int ans = ((n - 1) << 1) - (maxlen - 1);
if (k == 2)
{
for (int i = mx1[en]; i; i = mx1[to[i]]) len[i] = len[i ^ 1] = -1;
for (int i = mx2[en]; i; i = mx1[to[i]]) len[i] = len[i ^ 1] = -1;
maxlen = 0;
dfs(1, 0);
ans -= maxlen - 1;
}
printf("%d", ans);
return 0;
}

@ -0,0 +1,22 @@
#include <iostream>
#include <map>
using namespace std;
map<int, int> M;
int main()
{
int n, k;
cin >> n >> k;
for (int i = 0, j, x; i < n; i++)
{
cin >> x;
for (j = 1; j * j < x; j++) if (x % j == 0) M[j]++, M[x / j]++;
if (j * j == x) M[j]++;
}
for (map<int, int>::reverse_iterator rite = M.rbegin(); rite != M.rend(); rite++)
if (rite->second >= k)
{
cout << rite->first;
break;
}
return 0;
}

@ -0,0 +1,20 @@
#include <cstdio>
int n = 1, l;
char s[100001];
int main()
{
s[0] = 'a';
for (int i = 0; i < 12; i++)
{
for (int j = 0; j < n; j++)
s[j + n] = s[j] == 'a' ? 'b' : 'a';
n <<= 1;
}
l = n >> 1;
s[n++] = 'a';
for (int i = 1; i < l; i++) s[n++] = 'a';
s[n++] = 'b';
for (int i = 1; i < l; i++) s[n++] = 'a';
printf("8192 2048\n%s", s);
return 0;
}

@ -0,0 +1,44 @@
#include <cstdio>
#include <queue>
using namespace std;
typedef long long int64;
inline int64 _max(int64 a, int64 b) { return a > b ? a : b; }
struct word
{
int64 w, h;
bool operator<(const word& rhs) const
{
return w > rhs.w || (w == rhs.w && h > rhs.h);
}
};
priority_queue<word> heap;
int main()
{
int n, k;
scanf("%d%d", &n, &k);
for (int i = 0; i < n; i++)
{
int64 x;
scanf("%lld", &x);
heap.push({ x, 0 });
}
int top = (k - 1 - (n - 1) % (k - 1)) % (k - 1);
for (int i = 0; i < top; i++) heap.push({ 0, 0 });
top += n;
int64 ans = 0;
for (; top != 1; top -= k - 1)
{
int64 w, h;
w = h = 0;
for (int i = 0; i < k; i++)
{
w += heap.top().w;
h = _max(h, heap.top().h);
heap.pop();
}
ans += w;
heap.push({ w, h + 1 });
}
printf("%lld\n%lld", ans, heap.top().h);
return 0;
}
Loading…
Cancel
Save