QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#594385#3844. LCS Spanning TreeCipherxzcTL 423ms208056kbC++203.2kb2024-09-27 22:49:412024-09-27 22:49:42

Judging History

你现在查看的是最新测评结果

  • [2024-09-27 22:49:42]
  • 评测
  • 测评结果:TL
  • 用时:423ms
  • 内存:208056kb
  • [2024-09-27 22:49:41]
  • 提交

answer

#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int N = 3e6 + 10, inf = 1e9;
struct node {
    int x, y, s;
    bool operator<(const node &a) const { return s < a.s; }
};
int sa[N], c[N], x[N * 2], y[N * 2], h[N], rk[N];
int si[N], fa[N];
int n, m, i, nn;
char a[N];
string ch[N], ss;
priority_queue<node> q;

inline void get_sa() {
    int i, k = 1;
    int sum = 123;
    for (i = 1; i <= m; ++i) c[i] = 0;
    for (i = 1; i <= n; ++i) {
        if (a[i] != '#')
            c[x[i] = a[i]]++;
        else {
            c[x[i] = sum]++;
            sum++;
        }
    }
    for (i = 1; i <= m; ++i) c[i] += c[i - 1];
    for (i = n; i >= 1; --i) sa[c[x[i]]--] = i;
    while (k < n) {
        int t = 0;
        for (i = n - k + 1; i <= n; ++i) y[++t] = i;
        for (i = 1; i <= n; ++i)
            if (sa[i] - k > 0) y[++t] = sa[i] - k;
        for (i = 1; i <= m; ++i) c[i] = 0;
        for (i = 1; i <= n; ++i) c[x[i]]++;
        for (i = 1; i <= m; ++i) c[i] += c[i - 1];
        for (i = n; i >= 1; --i) sa[c[x[y[i]]]--] = y[i], y[i] = x[i];
        x[sa[1]] = 1;
        m = 1;
        for (i = 2; i <= n; ++i)
            if (y[sa[i]] == y[sa[i - 1]] && y[sa[i] + k] == y[sa[i - 1] + k])
                x[sa[i]] = m;
            else
                x[sa[i]] = ++m;
        k <<= 1;
        if (n == m) break;
    }
}
inline void get_height() {
    int i, k = 0;
    for (i = 1; i <= n; ++i) rk[sa[i]] = i;
    for (i = 1; i <= n; ++i) {
        if (k) k--;
        if (rk[i] == 1) continue;
        while (i + k <= n && sa[rk[i] - 1] + k <= n && (a[i + k] == a[sa[rk[i] - 1] + k] && a[i + k] != '#')) k++;
        h[rk[i]] = k;
    }
}
int gf(int x) {
    if (x == fa[x])
        return x;
    else {
        fa[x] = gf(fa[x]);
        return fa[x];
    }
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cin >> nn;
    int la = 0;
    ss = ' ';
    for (int i = 1; i <= nn; ++i) {
        cin >> ch[i];
        si[i] = ch[i].length();

        for (int j = la + 1; j <= la + si[i]; ++j) {
            fa[j] = la + 1;
        }

        ss = ss + ch[i] + '#';
        la = la + si[i] + 1;
    }
    for (int i = 1; i <= la; ++i) a[i] = ss[i];
    n = la;
    m = 122 + nn;
    get_sa();
    get_height();

    // for (int i = 2; i <= n; ++i) {
    //     if (a[sa[i - 1]] == '#') continue;
    //     if (fa[sa[i]] == fa[sa[i - 1]]) l[i] = min(l[i - 1], h[i]);
    // }
    // for (int i = n; i >= 1; --i) {
    //     if (a[sa[i - 1]] == '#') break;
    //     if (fa[sa[i]] == fa[sa[i - 1]]) r[i - 1] = min(r[i], h[i]);
    // }
    for (int i = 2; i <= n; ++i) {
        if (a[sa[i]] == '#') break;
        if (fa[sa[i]] != fa[sa[i - 1]]) q.push({sa[i], sa[i - 1], h[i]});
    }

    ll an = 0;
    for (int i = 1; i < nn; ++i) {
        int x = q.top().x, y = q.top().y;
        while (gf(x) == gf(y)) {
            q.pop();
            x = q.top().x, y = q.top().y;
        }
        x = gf(x);
        y = gf(y);
        an += q.top().s;
        fa[y] = x;
        q.pop();
    }
    cout << an;
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 13ms
memory: 115764kb

input:

4
icpc
macau
regional
contest

output:

4

result:

ok single line: '4'

Test #2:

score: 0
Accepted
time: 7ms
memory: 114368kb

input:

3
ababa
babab
aba

output:

7

result:

ok single line: '7'

Test #3:

score: 0
Accepted
time: 230ms
memory: 176252kb

input:

26
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

0

result:

ok single line: '0'

Test #4:

score: 0
Accepted
time: 7ms
memory: 114928kb

input:

7
jia
ran
jin
tian
chi
shen
me

output:

9

result:

ok single line: '9'

Test #5:

score: 0
Accepted
time: 3ms
memory: 115676kb

input:

10
theysaynothinglastsforever
weareonlyheretoday
loveisnowornever
bringmefaraway
takemetoyourheart
takemetoyoursoul
givemeyourhandandholdme
showmewhatloveis
bemyguidingstar
itiseasytakemetoyourheart

output:

55

result:

ok single line: '55'

Test #6:

score: 0
Accepted
time: 11ms
memory: 115408kb

input:

100
dblkekaekijliimalcaidjjfaghdmhifkiebieffbddjmflkhagajcfmkccjjadgiijdbdldgbbhgcfdcadbeiabkemiefdccmhdcfilhkfabmfdmigfgigdcibgaeicedfiidgecbhdamiaiefbmbgbjhklbhafmhckklbmmiemkcbfgjihmdjkai
bciiecmbc
cdjailkkbefkbmlekiefdhklcbdccfbgkagflfemjjmkjmcgiibldlmhbcldjajgafmakfbhecgcckkkglklljhmliehidbkicm...

output:

476

result:

ok single line: '476'

Test #7:

score: 0
Accepted
time: 423ms
memory: 207312kb

input:

2000
ecbhcebgbcjgjiihdefajfbbaajfjdedggciaegdiijhijgedbgejhgjjfhabdfhbihdeegcehbcjhgebcjachbdeiefejefhcjdihfcfgeegdahhjhjiiffjjadifiijjbhhjjeffabiaagcjhaachjbiecfeceefddecjchjfibgedfdghgdijdcdahfeddjihbhbbghjjffdcibaggiiadbaajhfcgdbaafbicahjhabfdbeacccfdehebciafaaffdfjdciafbhidbahdccjhjdadcciecfbhac...

output:

17765

result:

ok single line: '17765'

Test #8:

score: 0
Accepted
time: 340ms
memory: 208056kb

input:

1413
gjjmlceglbmbibjmmfcfmickcllfekgmicmifdbfgdgbeecgjgalbfkdfljjkclfgkaacdgigblaiaiehkeiccbjamikdgcjfemhhfebicddelklibjafmjhleebkimeblljfembgcgacdlkhjmbijjgacjaajebjfkcllffalheefeaedbmmkicaeecckdmedddbikeieimjmmcfdcgamicfbeimkjfamidjfhejdgiehkjkbdaaaeieldfibkkcgallieiamfehcdggiigkabblgigjgdlmflmafj...

output:

11429

result:

ok single line: '11429'

Test #9:

score: -100
Time Limit Exceeded

input:

2000000
j
o
v
p
h
b
t
s
x
y
u
c
t
n
p
l
b
e
c
v
d
c
p
s
u
m
d
d
i
m
h
t
a
e
j
i
i
c
c
h
d
x
j
w
m
a
j
p
f
l
n
i
q
c
c
k
q
g
q
b
u
z
u
v
d
d
f
n
i
j
v
n
i
e
l
w
h
v
m
m
i
z
w
y
d
z
l
w
h
b
x
b
a
r
y
d
x
f
r
h
p
o
u
x
u
f
u
b
c
i
p
l
j
o
j
o
n
u
k
w
x
h
z
x
z
y
d
y
d
w
h
u
k
b
u
e
i
o
g
n
y
x
y
h
l
j
...

output:


result: