QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#668294#3844. LCS Spanning TreeTJUHuangTaoML 704ms899164kbC++203.1kb2024-10-23 13:23:332024-10-23 13:23:35

Judging History

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

  • [2024-10-23 13:23:35]
  • 评测
  • 测评结果:ML
  • 用时:704ms
  • 内存:899164kb
  • [2024-10-23 13:23:33]
  • 提交

answer

#include <bits/stdc++.h>
// #define int long long
#define inf 0x3f3f3f3f
#define ll long long
#define pii pair<int, int>
#define tii tuple<int, int, int>
#define db double
#define all(a) a.begin(), a.end()
using namespace std;
const int maxn = 4e6 + 100;
const int maxm = 2e6 + 10;
const int mod = 998244353;
struct SAM{
    int tot, fa[maxn], len[maxn], c[maxn][26];
    vector<int> vec[maxn];
    unordered_map<int, bool> mp[maxn];
    SAM(){tot = 1;}
    int id[maxn], tong[maxn];
    void topo() { //基数排序后, 逆序枚举为拓扑序
        for (int i = 1; i <= tot; i++) tong[len[i]]++;
        for (int i = 1; i <= tot; i++) tong[i] += tong[i - 1];
        for (int i = tot; i; i--) id[tong[len[i]]--] = i;
    }
    int extend(char chr, int last, int i){
        int ch = chr - 'a';
        if (c[last][ch]) {
            int p = last, x = c[p][ch];
            if (len[p] + 1 == len[x]) return x;
            else {
                int y = ++tot;
                len[y] = len[p] + 1;
                memcpy(c[y], c[x], sizeof c[y]);
                while (p && c[p][ch] == x)
                    c[p][ch] = y, p = fa[p];
                fa[y] = fa[x], fa[x] = y;
                return y; 
            }
        }
        int z = ++tot, p = last;
        len[z] = len[last] + 1;
        while (p && !c[p][ch])
            c[p][ch] = z, p = fa[p];
        if (!p) fa[z] = 1;
        else {
            int x = c[p][ch];
            if (len[p] + 1 == len[x]) fa[z] = x;
            else {
                int y = ++tot;
                len[y] = len[p] + 1;
                memcpy(c[y], c[x], sizeof c[y]);
                while (p && c[p][ch] == x)
                    c[p][ch] = y, p = fa[p];
                fa[y] = fa[x], fa[z] = fa[x] = y;
            }
        }
        return z;
    }
} sam;
int fa[maxm];
int Find(int x) {return fa[x] == x ? fa[x] : fa[x] = Find(fa[x]);}
void solve() {
    int n; cin >> n;
    for (int i = 1; i <= n; i++) fa[i] = i;
    for (int i = 1; i <= n; i++) {
        string str; cin >> str;
        int lst = 1;
        for (auto ch : str)
            lst = sam.extend(ch, lst, i), sam.vec[lst].push_back(i), sam.mp[lst][i] = 1;
    }
    sam.topo();
    ll ans = 0 ;
    for (int i = sam.tot; i > 1; i--) {
        sam.mp[sam.id[i]].clear();
        auto& vec = sam.vec[sam.id[i]];
        if (!sam.mp[sam.fa[sam.id[i]]].count(vec[0]))
            sam.vec[sam.fa[sam.id[i]]].push_back(vec[0]);
        for (int j = 1; j < vec.size(); j++) {
            if (!sam.mp[sam.fa[sam.id[i]]].count(vec[j]))
                sam.vec[sam.fa[sam.id[i]]].push_back(vec[j]);
            int u = vec[j - 1], v = vec[j];
            u = Find(u), v = Find(v);
            if (u == v) continue;
            fa[u] = v;
            ans += sam.len[sam.id[i]];
        }
        vec.clear();
    }
    cout << ans << "\n";
}
signed main() {
    // freopen("1.in", "r", stdin);
    // freopen("1.out", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int t = 1;
    // cin >> t;
    while (t--) solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 61ms
memory: 316168kb

input:

4
icpc
macau
regional
contest

output:

4

result:

ok single line: '4'

Test #2:

score: 0
Accepted
time: 39ms
memory: 321068kb

input:

3
ababa
babab
aba

output:

7

result:

ok single line: '7'

Test #3:

score: 0
Accepted
time: 704ms
memory: 899164kb

input:

26
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

0

result:

ok single line: '0'

Test #4:

score: 0
Accepted
time: 36ms
memory: 321072kb

input:

7
jia
ran
jin
tian
chi
shen
me

output:

9

result:

ok single line: '9'

Test #5:

score: 0
Accepted
time: 44ms
memory: 321128kb

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: 50ms
memory: 325320kb

input:

100
dblkekaekijliimalcaidjjfaghdmhifkiebieffbddjmflkhagajcfmkccjjadgiijdbdldgbbhgcfdcadbeiabkemiefdccmhdcfilhkfabmfdmigfgigdcibgaeicedfiidgecbhdamiaiefbmbgbjhklbhafmhckklbmmiemkcbfgjihmdjkai
bciiecmbc
cdjailkkbefkbmlekiefdhklcbdccfbgkagflfemjjmkjmcgiibldlmhbcldjajgafmakfbhecgcckkkglklljhmliehidbkicm...

output:

476

result:

ok single line: '476'

Test #7:

score: -100
Memory Limit Exceeded

input:

2000
ecbhcebgbcjgjiihdefajfbbaajfjdedggciaegdiijhijgedbgejhgjjfhabdfhbihdeegcehbcjhgebcjachbdeiefejefhcjdihfcfgeegdahhjhjiiffjjadifiijjbhhjjeffabiaagcjhaachjbiecfeceefddecjchjfibgedfdghgdijdcdahfeddjihbhbbghjjffdcibaggiiadbaajhfcgdbaafbicahjhabfdbeacccfdehebciafaaffdfjdciafbhidbahdccjhjdadcciecfbhac...

output:

17765

result: