#include "september.h"
#include<iostream>
#include <vector>
#include<cassert>
#include<set>
#define pb push_back
using namespace std;
const int mxn=1e5,inf=1e9;
int pa[mxn+10],group[mxn+10],comp,who[mxn+10],mnpos[mxn+10],group2[mxn+10],group3[mxn+10];
vector<int>adj[mxn+10];
int find(int u){return (pa[u]==u)?u:pa[u]=find(pa[u]);}
void merg(int a,int b){
a=find(a),b=find(b);
if(a==0||b==0)assert(0);
if(a==b)return;
comp--;
pa[b]=a;
group[a]=max(group[a],group[b]);//keep max del min
}
void dfs(int cur,int p){for(auto i:adj[cur])group3[i]=min(group2[i],group3[cur]),dfs(i,cur);}
set<int>st;
int solve(int n,int m,vector<int>up,vector<vector<int>>S){
for(int i=0;i<n;i++)pa[i]=i,group[i]=inf,group2[i]=inf,adj[i].clear();
for(int i=1;i<n;i++)adj[up[i]].pb(i);
comp=n-1;
st.clear();
for(int i=0;i<n;i++)st.insert(i);
for(int i=0;i<n-1;i++){
int x=S[0][i];
group[x]=i;
who[i]=x;
}
for(int i=0;i<n;i++)group2[i]=group[find(i)],group3[i]=group2[i];
dfs(0,-1);
for(int i=0;i<n-1;i++){
int cur=S[0][i],mn=group3[cur];
int g=group2[(S[0][i])];
cur=mn;
auto it=st.lower_bound(mn);
while(it!=st.end()&&(*it)<g)merg(who[(*it)],who[g]),it=st.erase(it);
}
for(int j=1;j<m;j++){
int what=-1;
for(int i=0;i<n-1;i++){
int x=group[find(S[j][i])];
auto it=st.lower_bound(x);
while(it!=st.end()&&(*it)<what)merg(who[(*it)],who[what]),it=st.erase(it);
what=max(what,x);
}
for(int i=0;i<n;i++)group2[i]=group[find(i)],group3[i]=group2[i];
dfs(0,-1);
for(int i=0;i<n-1;i++){
int cur=S[j][i],mn=group3[cur];
int g=group2[(S[j][i])];
cur=g;
auto it=st.lower_bound(mn);
while(it!=st.end()&&(*it)<g)merg(who[(*it)],who[g]),it=st.erase(it);
}
}
if(comp<=0)assert(0);
return comp;
}
/*
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4952 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4952 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4696 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
4700 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
4700 KB |
Output is correct |
14 |
Correct |
1 ms |
4700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4952 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4696 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
2 ms |
4700 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
4700 KB |
Output is correct |
14 |
Correct |
2 ms |
4700 KB |
Output is correct |
15 |
Correct |
1 ms |
4700 KB |
Output is correct |
16 |
Correct |
1 ms |
4700 KB |
Output is correct |
17 |
Correct |
1 ms |
4700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
2 ms |
4696 KB |
Output is correct |
5 |
Correct |
2 ms |
4700 KB |
Output is correct |
6 |
Correct |
2 ms |
4700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4952 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4696 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
4700 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
4700 KB |
Output is correct |
14 |
Correct |
1 ms |
4700 KB |
Output is correct |
15 |
Correct |
1 ms |
4700 KB |
Output is correct |
16 |
Correct |
1 ms |
4700 KB |
Output is correct |
17 |
Correct |
1 ms |
4700 KB |
Output is correct |
18 |
Correct |
2 ms |
4700 KB |
Output is correct |
19 |
Correct |
1 ms |
4700 KB |
Output is correct |
20 |
Correct |
1 ms |
4700 KB |
Output is correct |
21 |
Correct |
2 ms |
4700 KB |
Output is correct |
22 |
Correct |
1 ms |
4700 KB |
Output is correct |
23 |
Correct |
1 ms |
4700 KB |
Output is correct |
24 |
Correct |
1 ms |
4700 KB |
Output is correct |
25 |
Correct |
2 ms |
4696 KB |
Output is correct |
26 |
Correct |
2 ms |
4700 KB |
Output is correct |
27 |
Correct |
2 ms |
4700 KB |
Output is correct |
28 |
Correct |
2 ms |
4856 KB |
Output is correct |
29 |
Correct |
2 ms |
4700 KB |
Output is correct |
30 |
Correct |
2 ms |
4700 KB |
Output is correct |
31 |
Correct |
2 ms |
4700 KB |
Output is correct |
32 |
Correct |
2 ms |
4860 KB |
Output is correct |
33 |
Correct |
2 ms |
4700 KB |
Output is correct |
34 |
Correct |
3 ms |
4700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
209 ms |
20812 KB |
Output is correct |
5 |
Correct |
205 ms |
21028 KB |
Output is correct |
6 |
Correct |
192 ms |
20764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4952 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4696 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
2 ms |
4700 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
4700 KB |
Output is correct |
14 |
Correct |
2 ms |
4700 KB |
Output is correct |
15 |
Correct |
1 ms |
4700 KB |
Output is correct |
16 |
Correct |
1 ms |
4700 KB |
Output is correct |
17 |
Correct |
1 ms |
4700 KB |
Output is correct |
18 |
Correct |
209 ms |
20812 KB |
Output is correct |
19 |
Correct |
205 ms |
21028 KB |
Output is correct |
20 |
Correct |
192 ms |
20764 KB |
Output is correct |
21 |
Correct |
217 ms |
14000 KB |
Output is correct |
22 |
Correct |
201 ms |
13992 KB |
Output is correct |
23 |
Correct |
204 ms |
15272 KB |
Output is correct |
24 |
Correct |
202 ms |
15272 KB |
Output is correct |
25 |
Correct |
217 ms |
14096 KB |
Output is correct |
26 |
Correct |
210 ms |
14000 KB |
Output is correct |
27 |
Correct |
210 ms |
15272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
2 ms |
4696 KB |
Output is correct |
5 |
Correct |
2 ms |
4700 KB |
Output is correct |
6 |
Correct |
2 ms |
4700 KB |
Output is correct |
7 |
Correct |
209 ms |
20812 KB |
Output is correct |
8 |
Correct |
205 ms |
21028 KB |
Output is correct |
9 |
Correct |
192 ms |
20764 KB |
Output is correct |
10 |
Correct |
149 ms |
23752 KB |
Output is correct |
11 |
Correct |
142 ms |
23880 KB |
Output is correct |
12 |
Correct |
160 ms |
23920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4952 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4696 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
4700 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
4700 KB |
Output is correct |
14 |
Correct |
1 ms |
4700 KB |
Output is correct |
15 |
Correct |
1 ms |
4700 KB |
Output is correct |
16 |
Correct |
1 ms |
4700 KB |
Output is correct |
17 |
Correct |
1 ms |
4700 KB |
Output is correct |
18 |
Correct |
2 ms |
4700 KB |
Output is correct |
19 |
Correct |
1 ms |
4700 KB |
Output is correct |
20 |
Correct |
1 ms |
4700 KB |
Output is correct |
21 |
Correct |
2 ms |
4700 KB |
Output is correct |
22 |
Correct |
1 ms |
4700 KB |
Output is correct |
23 |
Correct |
1 ms |
4700 KB |
Output is correct |
24 |
Correct |
1 ms |
4700 KB |
Output is correct |
25 |
Correct |
2 ms |
4696 KB |
Output is correct |
26 |
Correct |
2 ms |
4700 KB |
Output is correct |
27 |
Correct |
2 ms |
4700 KB |
Output is correct |
28 |
Correct |
2 ms |
4856 KB |
Output is correct |
29 |
Correct |
2 ms |
4700 KB |
Output is correct |
30 |
Correct |
2 ms |
4700 KB |
Output is correct |
31 |
Correct |
2 ms |
4700 KB |
Output is correct |
32 |
Correct |
2 ms |
4860 KB |
Output is correct |
33 |
Correct |
2 ms |
4700 KB |
Output is correct |
34 |
Correct |
3 ms |
4700 KB |
Output is correct |
35 |
Correct |
209 ms |
20812 KB |
Output is correct |
36 |
Correct |
205 ms |
21028 KB |
Output is correct |
37 |
Correct |
192 ms |
20764 KB |
Output is correct |
38 |
Correct |
217 ms |
14000 KB |
Output is correct |
39 |
Correct |
201 ms |
13992 KB |
Output is correct |
40 |
Correct |
204 ms |
15272 KB |
Output is correct |
41 |
Correct |
202 ms |
15272 KB |
Output is correct |
42 |
Correct |
217 ms |
14096 KB |
Output is correct |
43 |
Correct |
210 ms |
14000 KB |
Output is correct |
44 |
Correct |
210 ms |
15272 KB |
Output is correct |
45 |
Correct |
149 ms |
23752 KB |
Output is correct |
46 |
Correct |
142 ms |
23880 KB |
Output is correct |
47 |
Correct |
160 ms |
23920 KB |
Output is correct |
48 |
Correct |
144 ms |
16076 KB |
Output is correct |
49 |
Correct |
154 ms |
16076 KB |
Output is correct |
50 |
Correct |
147 ms |
17100 KB |
Output is correct |
51 |
Correct |
145 ms |
17100 KB |
Output is correct |
52 |
Correct |
154 ms |
16076 KB |
Output is correct |
53 |
Correct |
165 ms |
16080 KB |
Output is correct |
54 |
Correct |
149 ms |
17100 KB |
Output is correct |