【gdsoi2018 day3】水猴

题目大意:

无。

题解:

tmd的看错题了,不然就切了。

用个最大流跑跑不相交路径的条数。

接着用SA的height去对猴子排序,再求出新的height,二分个答案,扫一遍就行了。

Code:

#include
#include
#include
#include
#define fo(i, x, y) for(int i = x; i <= y; i ++)
#define fd(i, x, y) for(int i = x; i >= y; i --)
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
using namespace std;

const int N = 1e5 + 5, INF = 1 << 30;

int n, m, x, y, ar, q;
int final[N], to[N], next[N], r[N], tot = 1;
int cur[N], d[N], co[N], S, T;
struct node {
    int x, y;
} a[N];

void link(int x, int y, int z) {
    next[++ tot] = final[x], to[tot] = y, r[tot] = z, final[x] = tot;
    next[++ tot] = final[y], to[tot] = x, r[tot] = 0, final[y] = tot;
}

int dg(int x, int flow) {
    if(x == T) return flow;
    int use = 0;
    for(int i = cur[x]; i; i = next[i]) {
        int y = to[i];
        if(d[x] == d[y] + 1 && r[i]) {
            int tmp = dg(y, min(flow - use, r[i]));
            r[i] -= tmp; r[i ^ 1] += tmp; use += tmp;
            if(use == flow) return use;
        }
    }
    cur[x] = final[x];
    if(!(-- co[d[x]])) d[S] = T;
    ++ co[++ d[x]];
    return use;
}

int n0, m0; char s[N];
int he[N], rank[N], SA[N], tp[N],tax[N];
void rsort() {
    fo(i, 1, m0) tax[i] = 0;
    fo(i, 1, n0) tax[rank[tp[i]]] ++;
    fo(i, 1, m0) tax[i] += tax[i - 1];
    fd(i, n0, 1) SA[tax[rank[tp[i]]] --] = tp[i];
}
int cmp(int *f, int a, int b, int c) {return f[a] == f[b] && f[a + c] == f[b + c];}
void B_SA() {
    fo(i, 1, n0) rank[i] = s[i], tp[i] = i;
    m0 = 127; rsort();
    for(int w = 1, p = 1; p < n0; m0 = p, w *= 2) {
        p = 0; fo(i, n0 - w + 1, n0) tp[++ p] = i;
        fo(i, 1, n0) if(SA[i] > w) tp[++ p] = SA[i] - w;
        rsort(); swap(tp, rank); rank[SA[1]] = p = 1;
        fo(i, 2, n0) rank[SA[i]] = cmp(tp, SA[i - 1], SA[i], w) ? p : ++ p;
    }
    int j, k = 0;
    for(int i = 1; i <= n0; he[rank[i ++]] = k)
        for(k = k ? k - 1 : 0, j = SA[rank[i] - 1]; s[i + k] == s[j + k]; k ++);
}
int a2[17], f[17][N], h[N];
int mi(int x, int y) {
    int l = log2(y - x + 1);
    return min(f[l][x], f[l][y - a2[l] + 1]);
}
int lcp(node a, node b) {
    int p = rank[a.x], q = rank[b.x];
    if(p > q) swap(p, q);
    int l = (p == q) ? (n0 - SA[p] + 1) : mi(p + 1, q);
    return min(l, min(a.y - a.x + 1, b.y - b.x + 1));
}
int cp(node a, node b) {
    int l = lcp(a, b);
    if(a.y - a.x + 1 == l || b.y - b.x + 1 == l)
        return a.y - a.x + 1 < b.y - b.x + 1;
    return s[a.x + l] < s[b.x + l];
}

int lcp2(node a, node b) {
    int p = rank[a.x], q = rank[b.x];
    if(p > q) swap(p, q);
    int l = (p == q) ? (n0 - SA[p] + 1) : mi(p + 1, q);
    fo(i, p + 1, q) l = min(l, he[i]);
    fo(i, p + 1, q)  if(s[SA[i]] != s[SA[i - 1]])
    return min(l, min(a.y - a.x + 1, b.y - b.x + 1));
}
int ans;

int pd(int md) {
    int s = 1;
    fo(i, 2, q) if(h[i] < md) s ++;
    return s <= ar;
}

int main() {
    freopen("stupid.in", "r", stdin);
    freopen("stupid.out", "w", stdout);
    scanf("%d %d", &n, &m);
    fo(i, 1, m) {
        scanf("%d %d", &x, &y);
        link(x, y, 1);
    }
    S = 1, T = n; co[0] = T;
    for(; d[S] < T;) ar += dg(S, INF);

    scanf("%s", s + 1); n0 = strlen(s + 1);
    B_SA();

    a2[0] = 1; fo(i, 1, 16) a2[i] = a2[i - 1] * 2;
    fo(i, 1, n0) f[0][i] = he[i];
    fo(j, 1, 16) fo(i, 1, n0) f[j][i] = min(f[j - 1][i], f[j - 1][i + a2[j - 1]]);

    scanf("%d", &q);
    if(ar >= q) {printf("inf");return 0;}
    fo(i, 1, q) scanf("%d %d", &a[i].x, &a[i].y);
    sort(a + 1, a + q + 1, cp);
    fo(i, 2, q) h[i] = lcp(a[i - 1], a[i]);
    for(int l = 0, r = n0; l <= r; ) {
        int mm = l + r >> 1;
        if(pd(mm)) ans = mm, l = mm + 1; else r = mm - 1;
    }
    printf("%d\n", ans);
}

你可能感兴趣的:(网络流,Suffix,array)