#include "split.h"
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#include<bits/stdc++.h>
#define rc(x) return cout<<x<<endl,0
#define pb push_back
#define mkp make_pair
#define in insert
#define er erase
#define fd find
#define fr first
#define sc second
typedef long long ll;
typedef long double ld;
const ll INF=0x3f3f3f3f3f3f3f3f;
const ll llinf=(1LL<<62);
const int inf=(1<<30);
const int nmax=1e5+50;
const int mod=1e9+7;
using namespace std;
int n,i,j,x,y,bl,nd,p[nmax],pr[nmax],sub[nmax],b[5],viz[nmax],mx,m;
vector<int>g[nmax],rs,dg[nmax],t[nmax];
pair<int,int>a[5],pa;
set<pair<int,int> >s;
void siz(int x,int p)
{
pr[x]=p;
sub[x]=1;
for(int i=0;i<t[x].size();i++)
{
int y=t[x][i];
if(y==p)continue;
siz(y,x);
sub[x]+=sub[y];
}
}
void dfs(int x,int pp,int c)
{
p[x]=c;
dg[c].pb(x);
for(int i=0;i<t[x].size();i++)
{
int y=t[x][i];
if(y==pp)continue;
dfs(y,x,c);
}
}
int fnd(int x)
{
if(p[x]==x)return x;
return p[x]=fnd(p[x]);
}
void uni(int x,int y)
{
x=fnd(x),y=fnd(y);
if(sub[x]<sub[y])swap(x,y);
while(!dg[y].empty())
{
p[dg[y].back()]=x;
dg[x].pb(dg[y].back());
dg[y].pop_back();
}
if(s.fd(mkp(sub[x],x))!=s.end())s.er(s.fd(mkp(sub[x],x)));
sub[x]+=sub[y];
s.in(mkp(sub[x],x));
}
void bas(int x,int y)
{
if(fnd(x)!=fnd(y))
{
t[x].pb(y);
t[y].pb(x);
x=fnd(x),y=fnd(y);
if(sub[x]<sub[y])swap(x,y);
p[y]=x;
sub[x]+=sub[y];
}
}
void frs(int x,int p)
{
if(!a[1].fr)return;
a[1].fr--;
rs[x-1]=a[1].sc;
viz[x]=1;
for(int i=0;i<g[x].size();i++)
{
int y=g[x][i];
if(fnd(y)!=p || viz[y])continue;
frs(y,p);
}
}
void col(int x)
{
if(!a[2].fr)return;
a[2].fr--;
rs[x-1]=a[2].sc;
viz[x]=1;
for(int i=0;i<g[x].size();i++)
{
int y=g[x][i];
if(viz[y])continue;
col(y);
}
}
vector<int> find_split(int N,int A,int B,int C,vector<int>P,vector<int>Q)
{
n=N,b[1]=A,b[2]=B,b[3]=C,m=(int)P.size();
for(i=1;i<=3;i++)a[i]=mkp(b[i],i);
sort(a+1,a+4);
for(i=1;i<=n;i++)
{
p[i]=i;
sub[i]=1;
}
for(i=0;i<m;i++)
{
P[i]++,Q[i]++;
bas(P[i],Q[i]);
g[P[i]].pb(Q[i]);
g[Q[i]].pb(P[i]);
}
for(i=0;i<n;i++)rs.pb(0);
siz(1,0);
for(i=1;i<=n;i++)
{
mx=n-sub[i];
for(j=0;j<t[i].size();j++)
{
x=t[i][j];
if(x==pr[i])continue;
mx=max(mx,sub[x]);
}
if(mx<=n/2)
{
nd=i;
break;
}
}
siz(nd,0);
p[nd]=nd;
for(i=0;i<t[nd].size();i++)
{
x=t[nd][i];
s.in(mkp(sub[x],x));
dfs(x,nd,x);
}
while((int)s.size()>2)
{
if(s.rbegin()->fr>=a[1].fr)break;
pa=(*s.begin());
s.er(s.begin());
bl=0;
for(i=0;i<dg[pa.sc].size();i++)
{
x=dg[pa.sc][i];
for(j=0;j<g[x].size();j++)
{
y=g[x][j];
if(y==nd || fnd(y)==pa.sc)continue;
uni(x,y);
bl=1;
break;
}
if(bl)break;
}
}
if(s.empty())return rs;
x=s.rbegin()->sc;
if(sub[x]<a[1].fr)return rs;
frs(x,x);
col(nd);
for(i=0;i<n;i++)if(!rs[i])rs[i]=a[3].sc;
return rs;
}
Compilation message
split.cpp:3:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
#pragma GCC optimization("O3")
split.cpp:4:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
#pragma GCC optimization("unroll-loops")
split.cpp: In function 'void siz(int, int)':
split.cpp:30:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<t[x].size();i++)
~^~~~~~~~~~~~
split.cpp: In function 'void dfs(int, int, int)':
split.cpp:42:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<t[x].size();i++)
~^~~~~~~~~~~~
split.cpp: In function 'void frs(int, int)':
split.cpp:86:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[x].size();i++)
~^~~~~~~~~~~~
split.cpp: In function 'void col(int)':
split.cpp:99:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[x].size();i++)
~^~~~~~~~~~~~
split.cpp: In function 'std::vector<int> find_split(int, int, int, int, std::vector<int>, std::vector<int>)':
split.cpp:128:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(j=0;j<t[i].size();j++)
~^~~~~~~~~~~~
split.cpp:142:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0;i<t[nd].size();i++)
~^~~~~~~~~~~~~
split.cpp:154:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0;i<dg[pa.sc].size();i++)
~^~~~~~~~~~~~~~~~~
split.cpp:157:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(j=0;j<g[x].size();j++)
~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
7416 KB |
ok, correct split |
2 |
Correct |
8 ms |
7416 KB |
ok, correct split |
3 |
Correct |
8 ms |
7416 KB |
ok, correct split |
4 |
Correct |
8 ms |
7416 KB |
ok, correct split |
5 |
Correct |
8 ms |
7416 KB |
ok, correct split |
6 |
Correct |
8 ms |
7416 KB |
ok, correct split |
7 |
Correct |
161 ms |
24176 KB |
ok, correct split |
8 |
Correct |
143 ms |
21872 KB |
ok, correct split |
9 |
Correct |
211 ms |
21772 KB |
ok, correct split |
10 |
Correct |
160 ms |
23856 KB |
ok, correct split |
11 |
Correct |
253 ms |
23124 KB |
ok, correct split |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
7416 KB |
ok, correct split |
2 |
Correct |
8 ms |
7416 KB |
ok, correct split |
3 |
Correct |
10 ms |
7416 KB |
ok, correct split |
4 |
Correct |
175 ms |
18984 KB |
ok, correct split |
5 |
Correct |
189 ms |
18416 KB |
ok, correct split |
6 |
Correct |
168 ms |
23792 KB |
ok, correct split |
7 |
Correct |
240 ms |
22100 KB |
ok, correct split |
8 |
Correct |
264 ms |
20508 KB |
ok, correct split |
9 |
Correct |
163 ms |
18288 KB |
ok, correct split |
10 |
Correct |
186 ms |
26216 KB |
ok, correct split |
11 |
Correct |
181 ms |
26164 KB |
ok, correct split |
12 |
Correct |
192 ms |
26216 KB |
ok, correct split |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
7416 KB |
ok, correct split |
2 |
Correct |
140 ms |
18416 KB |
ok, correct split |
3 |
Correct |
69 ms |
11772 KB |
ok, correct split |
4 |
Correct |
8 ms |
7416 KB |
ok, correct split |
5 |
Correct |
259 ms |
20208 KB |
ok, correct split |
6 |
Correct |
536 ms |
20080 KB |
ok, correct split |
7 |
Correct |
280 ms |
20092 KB |
ok, correct split |
8 |
Correct |
168 ms |
21104 KB |
ok, correct split |
9 |
Correct |
165 ms |
20080 KB |
ok, correct split |
10 |
Correct |
48 ms |
10996 KB |
ok, no valid answer |
11 |
Correct |
67 ms |
12792 KB |
ok, no valid answer |
12 |
Correct |
168 ms |
22640 KB |
ok, no valid answer |
13 |
Correct |
134 ms |
18724 KB |
ok, no valid answer |
14 |
Correct |
183 ms |
25828 KB |
ok, no valid answer |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
7416 KB |
ok, correct split |
2 |
Correct |
8 ms |
7416 KB |
ok, no valid answer |
3 |
Correct |
8 ms |
7416 KB |
ok, correct split |
4 |
Correct |
8 ms |
7416 KB |
ok, correct split |
5 |
Correct |
8 ms |
7416 KB |
ok, correct split |
6 |
Correct |
8 ms |
7416 KB |
ok, correct split |
7 |
Correct |
8 ms |
7416 KB |
ok, correct split |
8 |
Correct |
8 ms |
7416 KB |
ok, correct split |
9 |
Correct |
11 ms |
7672 KB |
ok, correct split |
10 |
Correct |
11 ms |
7672 KB |
ok, correct split |
11 |
Correct |
8 ms |
7416 KB |
ok, correct split |
12 |
Correct |
12 ms |
7800 KB |
ok, correct split |
13 |
Correct |
8 ms |
7420 KB |
ok, correct split |
14 |
Correct |
8 ms |
7416 KB |
ok, correct split |
15 |
Correct |
8 ms |
7420 KB |
ok, correct split |
16 |
Correct |
10 ms |
7416 KB |
ok, correct split |
17 |
Correct |
10 ms |
7416 KB |
ok, correct split |
18 |
Correct |
10 ms |
7416 KB |
ok, correct split |
19 |
Correct |
10 ms |
7416 KB |
ok, correct split |
20 |
Correct |
9 ms |
7544 KB |
ok, correct split |
21 |
Correct |
11 ms |
7800 KB |
ok, correct split |
22 |
Correct |
11 ms |
7800 KB |
ok, correct split |
23 |
Correct |
10 ms |
7800 KB |
ok, correct split |
24 |
Correct |
24 ms |
7672 KB |
ok, correct split |
25 |
Correct |
10 ms |
7672 KB |
ok, correct split |
26 |
Correct |
11 ms |
7928 KB |
ok, correct split |
27 |
Correct |
11 ms |
7928 KB |
ok, correct split |
28 |
Correct |
20 ms |
7800 KB |
ok, correct split |
29 |
Correct |
8 ms |
7416 KB |
ok, correct split |
30 |
Correct |
11 ms |
7800 KB |
ok, correct split |
31 |
Correct |
9 ms |
7544 KB |
ok, correct split |
32 |
Correct |
10 ms |
7416 KB |
ok, correct split |
33 |
Correct |
10 ms |
7416 KB |
ok, correct split |
34 |
Correct |
9 ms |
7800 KB |
ok, correct split |
35 |
Correct |
11 ms |
7672 KB |
ok, correct split |
36 |
Correct |
15 ms |
7672 KB |
ok, correct split |
37 |
Correct |
15 ms |
7928 KB |
ok, correct split |
38 |
Correct |
16 ms |
7928 KB |
ok, correct split |
39 |
Correct |
12 ms |
7928 KB |
ok, correct split |
40 |
Correct |
12 ms |
7932 KB |
ok, correct split |
41 |
Correct |
10 ms |
7672 KB |
ok, correct split |
42 |
Correct |
10 ms |
7544 KB |
ok, correct split |
43 |
Correct |
12 ms |
7800 KB |
ok, correct split |
44 |
Correct |
11 ms |
7928 KB |
ok, correct split |
45 |
Correct |
11 ms |
7800 KB |
ok, correct split |
46 |
Correct |
10 ms |
7800 KB |
ok, correct split |
47 |
Correct |
10 ms |
7800 KB |
ok, no valid answer |
48 |
Correct |
11 ms |
7672 KB |
ok, correct split |
49 |
Correct |
11 ms |
7876 KB |
ok, correct split |
50 |
Correct |
8 ms |
7416 KB |
ok, no valid answer |
51 |
Correct |
8 ms |
7416 KB |
ok, no valid answer |
52 |
Correct |
10 ms |
7672 KB |
ok, no valid answer |
53 |
Correct |
11 ms |
7816 KB |
ok, no valid answer |
54 |
Correct |
10 ms |
7800 KB |
ok, no valid answer |
55 |
Correct |
11 ms |
7800 KB |
ok, no valid answer |
56 |
Correct |
11 ms |
7800 KB |
ok, no valid answer |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
7416 KB |
ok, correct split |
2 |
Correct |
8 ms |
7416 KB |
ok, correct split |
3 |
Correct |
8 ms |
7416 KB |
ok, correct split |
4 |
Correct |
8 ms |
7416 KB |
ok, correct split |
5 |
Correct |
8 ms |
7416 KB |
ok, correct split |
6 |
Correct |
8 ms |
7416 KB |
ok, correct split |
7 |
Correct |
161 ms |
24176 KB |
ok, correct split |
8 |
Correct |
143 ms |
21872 KB |
ok, correct split |
9 |
Correct |
211 ms |
21772 KB |
ok, correct split |
10 |
Correct |
160 ms |
23856 KB |
ok, correct split |
11 |
Correct |
253 ms |
23124 KB |
ok, correct split |
12 |
Correct |
9 ms |
7416 KB |
ok, correct split |
13 |
Correct |
8 ms |
7416 KB |
ok, correct split |
14 |
Correct |
10 ms |
7416 KB |
ok, correct split |
15 |
Correct |
175 ms |
18984 KB |
ok, correct split |
16 |
Correct |
189 ms |
18416 KB |
ok, correct split |
17 |
Correct |
168 ms |
23792 KB |
ok, correct split |
18 |
Correct |
240 ms |
22100 KB |
ok, correct split |
19 |
Correct |
264 ms |
20508 KB |
ok, correct split |
20 |
Correct |
163 ms |
18288 KB |
ok, correct split |
21 |
Correct |
186 ms |
26216 KB |
ok, correct split |
22 |
Correct |
181 ms |
26164 KB |
ok, correct split |
23 |
Correct |
192 ms |
26216 KB |
ok, correct split |
24 |
Correct |
8 ms |
7416 KB |
ok, correct split |
25 |
Correct |
140 ms |
18416 KB |
ok, correct split |
26 |
Correct |
69 ms |
11772 KB |
ok, correct split |
27 |
Correct |
8 ms |
7416 KB |
ok, correct split |
28 |
Correct |
259 ms |
20208 KB |
ok, correct split |
29 |
Correct |
536 ms |
20080 KB |
ok, correct split |
30 |
Correct |
280 ms |
20092 KB |
ok, correct split |
31 |
Correct |
168 ms |
21104 KB |
ok, correct split |
32 |
Correct |
165 ms |
20080 KB |
ok, correct split |
33 |
Correct |
48 ms |
10996 KB |
ok, no valid answer |
34 |
Correct |
67 ms |
12792 KB |
ok, no valid answer |
35 |
Correct |
168 ms |
22640 KB |
ok, no valid answer |
36 |
Correct |
134 ms |
18724 KB |
ok, no valid answer |
37 |
Correct |
183 ms |
25828 KB |
ok, no valid answer |
38 |
Correct |
8 ms |
7416 KB |
ok, correct split |
39 |
Correct |
8 ms |
7416 KB |
ok, no valid answer |
40 |
Correct |
8 ms |
7416 KB |
ok, correct split |
41 |
Correct |
8 ms |
7416 KB |
ok, correct split |
42 |
Correct |
8 ms |
7416 KB |
ok, correct split |
43 |
Correct |
8 ms |
7416 KB |
ok, correct split |
44 |
Correct |
8 ms |
7416 KB |
ok, correct split |
45 |
Correct |
8 ms |
7416 KB |
ok, correct split |
46 |
Correct |
11 ms |
7672 KB |
ok, correct split |
47 |
Correct |
11 ms |
7672 KB |
ok, correct split |
48 |
Correct |
8 ms |
7416 KB |
ok, correct split |
49 |
Correct |
12 ms |
7800 KB |
ok, correct split |
50 |
Correct |
8 ms |
7420 KB |
ok, correct split |
51 |
Correct |
8 ms |
7416 KB |
ok, correct split |
52 |
Correct |
8 ms |
7420 KB |
ok, correct split |
53 |
Correct |
10 ms |
7416 KB |
ok, correct split |
54 |
Correct |
10 ms |
7416 KB |
ok, correct split |
55 |
Correct |
10 ms |
7416 KB |
ok, correct split |
56 |
Correct |
10 ms |
7416 KB |
ok, correct split |
57 |
Correct |
9 ms |
7544 KB |
ok, correct split |
58 |
Correct |
11 ms |
7800 KB |
ok, correct split |
59 |
Correct |
11 ms |
7800 KB |
ok, correct split |
60 |
Correct |
10 ms |
7800 KB |
ok, correct split |
61 |
Correct |
24 ms |
7672 KB |
ok, correct split |
62 |
Correct |
10 ms |
7672 KB |
ok, correct split |
63 |
Correct |
11 ms |
7928 KB |
ok, correct split |
64 |
Correct |
11 ms |
7928 KB |
ok, correct split |
65 |
Correct |
20 ms |
7800 KB |
ok, correct split |
66 |
Correct |
8 ms |
7416 KB |
ok, correct split |
67 |
Correct |
11 ms |
7800 KB |
ok, correct split |
68 |
Correct |
9 ms |
7544 KB |
ok, correct split |
69 |
Correct |
10 ms |
7416 KB |
ok, correct split |
70 |
Correct |
10 ms |
7416 KB |
ok, correct split |
71 |
Correct |
9 ms |
7800 KB |
ok, correct split |
72 |
Correct |
11 ms |
7672 KB |
ok, correct split |
73 |
Correct |
15 ms |
7672 KB |
ok, correct split |
74 |
Correct |
15 ms |
7928 KB |
ok, correct split |
75 |
Correct |
16 ms |
7928 KB |
ok, correct split |
76 |
Correct |
12 ms |
7928 KB |
ok, correct split |
77 |
Correct |
12 ms |
7932 KB |
ok, correct split |
78 |
Correct |
10 ms |
7672 KB |
ok, correct split |
79 |
Correct |
10 ms |
7544 KB |
ok, correct split |
80 |
Correct |
12 ms |
7800 KB |
ok, correct split |
81 |
Correct |
11 ms |
7928 KB |
ok, correct split |
82 |
Correct |
11 ms |
7800 KB |
ok, correct split |
83 |
Correct |
10 ms |
7800 KB |
ok, correct split |
84 |
Correct |
10 ms |
7800 KB |
ok, no valid answer |
85 |
Correct |
11 ms |
7672 KB |
ok, correct split |
86 |
Correct |
11 ms |
7876 KB |
ok, correct split |
87 |
Correct |
8 ms |
7416 KB |
ok, no valid answer |
88 |
Correct |
8 ms |
7416 KB |
ok, no valid answer |
89 |
Correct |
10 ms |
7672 KB |
ok, no valid answer |
90 |
Correct |
11 ms |
7816 KB |
ok, no valid answer |
91 |
Correct |
10 ms |
7800 KB |
ok, no valid answer |
92 |
Correct |
11 ms |
7800 KB |
ok, no valid answer |
93 |
Correct |
11 ms |
7800 KB |
ok, no valid answer |
94 |
Correct |
163 ms |
20696 KB |
ok, correct split |
95 |
Correct |
218 ms |
23920 KB |
ok, correct split |
96 |
Correct |
204 ms |
23004 KB |
ok, correct split |
97 |
Correct |
52 ms |
12152 KB |
ok, correct split |
98 |
Correct |
54 ms |
12284 KB |
ok, correct split |
99 |
Correct |
74 ms |
14072 KB |
ok, correct split |
100 |
Correct |
213 ms |
23128 KB |
ok, correct split |
101 |
Correct |
183 ms |
21940 KB |
ok, correct split |
102 |
Correct |
220 ms |
25924 KB |
ok, correct split |
103 |
Correct |
225 ms |
25936 KB |
ok, correct split |
104 |
Correct |
161 ms |
24264 KB |
ok, correct split |
105 |
Correct |
82 ms |
14968 KB |
ok, correct split |
106 |
Correct |
216 ms |
26720 KB |
ok, correct split |
107 |
Correct |
146 ms |
19440 KB |
ok, correct split |
108 |
Correct |
227 ms |
19440 KB |
ok, correct split |
109 |
Correct |
232 ms |
22516 KB |
ok, correct split |
110 |
Correct |
298 ms |
26860 KB |
ok, correct split |
111 |
Correct |
315 ms |
27884 KB |
ok, correct split |
112 |
Correct |
292 ms |
27368 KB |
ok, correct split |
113 |
Correct |
285 ms |
28140 KB |
ok, correct split |
114 |
Correct |
28 ms |
9592 KB |
ok, correct split |
115 |
Correct |
28 ms |
9464 KB |
ok, correct split |
116 |
Correct |
221 ms |
25832 KB |
ok, correct split |
117 |
Correct |
281 ms |
25448 KB |
ok, correct split |
118 |
Correct |
152 ms |
19540 KB |
ok, correct split |
119 |
Correct |
162 ms |
19456 KB |
ok, correct split |
120 |
Correct |
151 ms |
21872 KB |
ok, correct split |
121 |
Correct |
137 ms |
19064 KB |
ok, no valid answer |
122 |
Correct |
147 ms |
20680 KB |
ok, no valid answer |
123 |
Correct |
260 ms |
22544 KB |
ok, no valid answer |
124 |
Correct |
217 ms |
22644 KB |
ok, no valid answer |
125 |
Correct |
333 ms |
26344 KB |
ok, no valid answer |
126 |
Correct |
181 ms |
25036 KB |
ok, no valid answer |
127 |
Correct |
250 ms |
25576 KB |
ok, no valid answer |