QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#236063#3844. LCS Spanning Treeushg8877RE 794ms564808kbC++202.2kb2023-11-03 16:00:222023-11-03 16:00:23

Judging History

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

  • [2023-11-03 16:00:23]
  • 评测
  • 测评结果:RE
  • 用时:794ms
  • 内存:564808kb
  • [2023-11-03 16:00:22]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MP make_pair
const int MAXN=2e6+5;
bool Mbe;
struct machine{
	int fa,len,nxt[26];
}sam[MAXN<<1];int tot=1;// GSA
vector<int> son[MAXN<<1];int p[MAXN<<1],rep[MAXN<<1];
vector<int> vec[MAXN]; 
bool Med;
void add(string s,int id){
	int p=1;
	for(int i=0;i<s.length();i++){
		int ch=s[i]-'a';
		if(!sam[p].nxt[ch]) sam[p].nxt[ch]=++tot;
		p=sam[p].nxt[ch];
		vec[p].push_back(id);
	}
	return;
}
int insert(int lst,int ch){
	int cur=sam[lst].nxt[ch],p=sam[lst].fa;
	sam[cur].len=sam[lst].len+1; 
	for(;p&&!sam[p].nxt[ch];p=sam[p].fa) sam[p].nxt[ch]=cur;
	int q=sam[p].nxt[ch];
	if(!p) sam[cur].fa=1;
	else if(sam[q].len==sam[p].len+1) sam[cur].fa=q;
	else{
		int clone=++tot;
		sam[clone].fa=sam[q].fa;sam[clone].len=sam[p].len+1;
		for(int i=0;i<26;i++)
			if(sam[sam[q].nxt[i]].len)// 只转已到达的点
				sam[clone].nxt[i]=sam[q].nxt[i];
		for(;p&&sam[p].nxt[ch]==q;p=sam[p].fa) sam[p].nxt[ch]=clone;
		sam[q].fa=sam[cur].fa=clone;
	}
	return cur;
}
void BFS(){
	queue<pair<int,int> > q;// node, chart
	for(int i=0;i<26;i++)
		if(sam[1].nxt[i])
			q.push(MP(1,i)); 
	while(!q.empty()){
		auto k=q.front();q.pop();
		int p=insert(k.first,k.second);
		for(int i=0;i<26;i++)
			if(sam[p].nxt[i])
				q.push(MP(p,i));
	}
	return;
}
int n;string s[MAXN];
int fa[MAXN];
inline int find(int x){
	while(x^fa[x]) x=fa[x]=fa[fa[x]];
	return x;
}
int main(){
	ios::sync_with_stdio(false);
	cerr<<"Memory: "<<(&Mbe-&Med)*1.0/1024/1024<<endl;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>s[i];
		add(s[i],i);
		fa[i]=i;
	}
	BFS();
	int ans=0;
	for(int i=2;i<=tot;i++) son[sam[i].fa].push_back(i);
	for(int i=1;i<=tot;i++) p[i]=i;
	sort(p+1,p+tot+1,[&](int x,int y){return sam[x].len>sam[y].len;});
	for(int i=1;i<=tot;i++){
		for(int &j:vec[p[i]]) j=find(j);
		for(int j:son[p[i]]) if(!vec[j].empty())
			vec[p[i]].push_back(find(vec[j].front()));
		sort(vec[p[i]].begin(),vec[p[i]].end());
		vec[p[i]].erase(unique(vec[p[i]].begin(),vec[p[i]].end()),vec[p[i]].end());
		ans+=sam[p[i]].len*(vec[p[i]].size()-1);
		for(int j:vec[p[i]]) fa[find(j)]=find(vec[p[i]].front());
	}
	cout<<ans;
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 32ms
memory: 212852kb

input:

4
icpc
macau
regional
contest

output:

4

result:

ok single line: '4'

Test #2:

score: 0
Accepted
time: 20ms
memory: 212756kb

input:

3
ababa
babab
aba

output:

7

result:

ok single line: '7'

Test #3:

score: 0
Accepted
time: 794ms
memory: 564808kb

input:

26
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

0

result:

ok single line: '0'

Test #4:

score: 0
Accepted
time: 19ms
memory: 212908kb

input:

7
jia
ran
jin
tian
chi
shen
me

output:

9

result:

ok single line: '9'

Test #5:

score: 0
Accepted
time: 15ms
memory: 214800kb

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: 19ms
memory: 213848kb

input:

100
dblkekaekijliimalcaidjjfaghdmhifkiebieffbddjmflkhagajcfmkccjjadgiijdbdldgbbhgcfdcadbeiabkemiefdccmhdcfilhkfabmfdmigfgigdcibgaeicedfiidgecbhdamiaiefbmbgbjhklbhafmhckklbmmiemkcbfgjihmdjkai
bciiecmbc
cdjailkkbefkbmlekiefdhklcbdccfbgkagflfemjjmkjmcgiibldlmhbcldjajgafmakfbhecgcckkkglklljhmliehidbkicm...

output:

476

result:

ok single line: '476'

Test #7:

score: -100
Runtime Error

input:

2000
ecbhcebgbcjgjiihdefajfbbaajfjdedggciaegdiijhijgedbgejhgjjfhabdfhbihdeegcehbcjhgebcjachbdeiefejefhcjdihfcfgeegdahhjhjiiffjjadifiijjbhhjjeffabiaagcjhaachjbiecfeceefddecjchjfibgedfdghgdijdcdahfeddjihbhbbghjjffdcibaggiiadbaajhfcgdbaafbicahjhabfdbeacccfdehebciafaaffdfjdciafbhidbahdccjhjdadcciecfbhac...

output:


result: