# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
806780 |
2023-08-04T09:50:58 Z |
ashcomp |
LOSTIKS (INOI20_lostiks) |
C++14 |
|
1404 ms |
518304 KB |
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+10,lg=21;
struct yal{
int u,v,w;
int getad(int fu){
int ret=(u^v^fu);
return ret;
}
};
yal alled[maxn];
vector<int>adj[maxn];
int n,m,visa[maxn],lca[lg][maxn*2],wtf[2*maxn],ghabl[maxn],vis[maxn],high[maxn],timea,para[maxn],s,t;
pair<int,int>stf[maxn];
int dp[(1<<20)][20];
vector<int>allind[(1<<20)];
vector<int>allv;
vector<int>z;
void aval(){
for(int i=1;i<(1<<20);i++)
{
int j=0;
for(;;j++){
if((i>>j)&1){
break;
}
}
allind[i]=allind[i^(1<<j)];
allind[i].push_back(j);
}
int ha=-1;
for(int i=1;i<2*maxn;i++){
if(__builtin_popcount(i)==1){
ha++;
}
wtf[i]=ha;
}
}
void dfs(int u,int par=0){
high[u]=high[par]+1;
z.push_back(u);
stf[u].first=(int)z.size()-1;
for(auto x:adj[u]){
int v=alled[x].getad(u);
if(v!=par){
if(v!=alled[x].v){
swap(alled[x].u,alled[x].v);
}
para[v]=x;
dfs(v,u);
z.push_back(u);
}
}
stf[u].second=(int)z.size()-1;
}
inline void callca(){
for(int i=0;i<lg;i++){
for(int j=0;j<(int)z.size();j++){
if(i==0){
lca[i][j]=z[j];
continue;
}
if(high[lca[i-1][j]]<high[lca[i-1][j+(1<<(i-1))]]){
lca[i][j]=lca[i-1][j];
}
else{
lca[i][j]=lca[i-1][j+(1<<(i-1))];
}
}
}
}
inline int getlca(int u,int v){
int fu=min(stf[u].first,stf[v].first);
int fv=max(stf[u].first,stf[v].first);
u=fu;
v=fv;
int len=v-u+1;
int te=wtf[len];
if(high[lca[te][u]]<high[lca[te][v-(1<<te)+1]]){
return lca[te][u];
}
return lca[te][v-(1<<te)+1];
}
inline int dis(int u,int v){
int uv=getlca(u,v);
int ret=high[u]+high[v]-2*high[uv];
return ret;
}
void solve(int u){
//cout<<u<<endl;
if(vis[u]==1){
return ;
}
if(visa[u]==1){
cout<<-1<<"\n";
exit(0);
}
visa[u]=1;
solve(alled[para[u]].getad(u));
if(alled[para[u]].w!=0){
allv.push_back(para[u]);
solve(alled[para[u]].w);
}
vis[u]=1;
return ;
}
void calghabl(int u,int par=0){
ghabl[u]+=ghabl[par];
for(auto x:adj[u]){
int v=alled[x].getad(u);
if(v!=par){
calghabl(v,u);
}
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
aval();
cin>>n;
cin>>s>>t;
for(int i=0;i<n-1;i++){
cin>>alled[i].u>>alled[i].v>>alled[i].w;
adj[alled[i].u].push_back(i);
adj[alled[i].v].push_back(i);
}
dfs(s);
callca();
vis[s]=1;
solve(t);
m=allv.size();
for(int i=0;i<m;i++){
ghabl[alled[allv[i]].v]+=(1<<i);
}
calghabl(s);
for(int i=1;i<(int)(1<<m);i++){
for(auto x:allind[i]){
int fake=1e9;
if((ghabl[alled[allv[x]].u]&i)!=ghabl[alled[allv[x]].u]||(ghabl[alled[allv[x]].w]&i)!=ghabl[alled[allv[x]].w]){
dp[i][x]=fake;
continue;
}
if(__builtin_popcount(i)==1){
dp[i][x]=dis(alled[allv[x]].u,alled[allv[x]].w)+dis(alled[allv[x]].w,s);
continue;
}
for(auto y:allind[i^(1<<x)]){
fake=min(fake,dis(alled[allv[y]].u,alled[allv[x]].w)+dis(alled[allv[x]].u,alled[allv[x]].w)+dp[(1<<x)^i][y]);
}
dp[i][x]=fake;
}
}
if(m==0){
int res=dis(s,t);
cout<<res<<"\n";
return 0;
}
else{
int res=1e9;
for(auto x:allind[(1<<m)-1]){
res=min(res,dp[(1<<m)-1][x]+dis(alled[allv[x]].u,t));
}
cout<<res<<"\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
139 ms |
142568 KB |
Output is correct |
2 |
Correct |
132 ms |
142604 KB |
Output is correct |
3 |
Correct |
179 ms |
167628 KB |
Output is correct |
4 |
Correct |
187 ms |
167624 KB |
Output is correct |
5 |
Correct |
183 ms |
167660 KB |
Output is correct |
6 |
Correct |
184 ms |
167608 KB |
Output is correct |
7 |
Correct |
179 ms |
167832 KB |
Output is correct |
8 |
Correct |
187 ms |
167796 KB |
Output is correct |
9 |
Correct |
186 ms |
168008 KB |
Output is correct |
10 |
Correct |
180 ms |
167708 KB |
Output is correct |
11 |
Correct |
177 ms |
167740 KB |
Output is correct |
12 |
Correct |
175 ms |
169620 KB |
Output is correct |
13 |
Correct |
191 ms |
169256 KB |
Output is correct |
14 |
Correct |
200 ms |
168892 KB |
Output is correct |
15 |
Correct |
174 ms |
169424 KB |
Output is correct |
16 |
Correct |
175 ms |
170516 KB |
Output is correct |
17 |
Correct |
178 ms |
170552 KB |
Output is correct |
18 |
Correct |
181 ms |
171020 KB |
Output is correct |
19 |
Correct |
185 ms |
175136 KB |
Output is correct |
20 |
Correct |
176 ms |
174944 KB |
Output is correct |
21 |
Correct |
182 ms |
174764 KB |
Output is correct |
22 |
Correct |
134 ms |
142644 KB |
Output is correct |
23 |
Correct |
126 ms |
142700 KB |
Output is correct |
24 |
Correct |
133 ms |
142616 KB |
Output is correct |
25 |
Correct |
139 ms |
142716 KB |
Output is correct |
26 |
Correct |
123 ms |
142624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
127 ms |
142560 KB |
Output is correct |
2 |
Correct |
138 ms |
142560 KB |
Output is correct |
3 |
Correct |
128 ms |
142712 KB |
Output is correct |
4 |
Correct |
121 ms |
142704 KB |
Output is correct |
5 |
Correct |
130 ms |
145112 KB |
Output is correct |
6 |
Correct |
129 ms |
144732 KB |
Output is correct |
7 |
Correct |
130 ms |
144460 KB |
Output is correct |
8 |
Correct |
139 ms |
144524 KB |
Output is correct |
9 |
Correct |
126 ms |
144528 KB |
Output is correct |
10 |
Correct |
196 ms |
185576 KB |
Output is correct |
11 |
Correct |
197 ms |
185548 KB |
Output is correct |
12 |
Correct |
216 ms |
185476 KB |
Output is correct |
13 |
Correct |
199 ms |
185456 KB |
Output is correct |
14 |
Correct |
197 ms |
185552 KB |
Output is correct |
15 |
Correct |
136 ms |
145176 KB |
Output is correct |
16 |
Correct |
140 ms |
144508 KB |
Output is correct |
17 |
Correct |
152 ms |
144512 KB |
Output is correct |
18 |
Correct |
195 ms |
185732 KB |
Output is correct |
19 |
Correct |
200 ms |
185684 KB |
Output is correct |
20 |
Correct |
198 ms |
185676 KB |
Output is correct |
21 |
Correct |
197 ms |
185700 KB |
Output is correct |
22 |
Correct |
194 ms |
185972 KB |
Output is correct |
23 |
Correct |
201 ms |
185968 KB |
Output is correct |
24 |
Correct |
227 ms |
186076 KB |
Output is correct |
25 |
Correct |
127 ms |
142592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
139 ms |
142568 KB |
Output is correct |
2 |
Correct |
132 ms |
142604 KB |
Output is correct |
3 |
Correct |
179 ms |
167628 KB |
Output is correct |
4 |
Correct |
187 ms |
167624 KB |
Output is correct |
5 |
Correct |
183 ms |
167660 KB |
Output is correct |
6 |
Correct |
184 ms |
167608 KB |
Output is correct |
7 |
Correct |
179 ms |
167832 KB |
Output is correct |
8 |
Correct |
187 ms |
167796 KB |
Output is correct |
9 |
Correct |
186 ms |
168008 KB |
Output is correct |
10 |
Correct |
180 ms |
167708 KB |
Output is correct |
11 |
Correct |
177 ms |
167740 KB |
Output is correct |
12 |
Correct |
175 ms |
169620 KB |
Output is correct |
13 |
Correct |
191 ms |
169256 KB |
Output is correct |
14 |
Correct |
200 ms |
168892 KB |
Output is correct |
15 |
Correct |
174 ms |
169424 KB |
Output is correct |
16 |
Correct |
175 ms |
170516 KB |
Output is correct |
17 |
Correct |
178 ms |
170552 KB |
Output is correct |
18 |
Correct |
181 ms |
171020 KB |
Output is correct |
19 |
Correct |
185 ms |
175136 KB |
Output is correct |
20 |
Correct |
176 ms |
174944 KB |
Output is correct |
21 |
Correct |
182 ms |
174764 KB |
Output is correct |
22 |
Correct |
134 ms |
142644 KB |
Output is correct |
23 |
Correct |
126 ms |
142700 KB |
Output is correct |
24 |
Correct |
133 ms |
142616 KB |
Output is correct |
25 |
Correct |
139 ms |
142716 KB |
Output is correct |
26 |
Correct |
123 ms |
142624 KB |
Output is correct |
27 |
Correct |
127 ms |
142560 KB |
Output is correct |
28 |
Correct |
138 ms |
142560 KB |
Output is correct |
29 |
Correct |
128 ms |
142712 KB |
Output is correct |
30 |
Correct |
121 ms |
142704 KB |
Output is correct |
31 |
Correct |
130 ms |
145112 KB |
Output is correct |
32 |
Correct |
129 ms |
144732 KB |
Output is correct |
33 |
Correct |
130 ms |
144460 KB |
Output is correct |
34 |
Correct |
139 ms |
144524 KB |
Output is correct |
35 |
Correct |
126 ms |
144528 KB |
Output is correct |
36 |
Correct |
196 ms |
185576 KB |
Output is correct |
37 |
Correct |
197 ms |
185548 KB |
Output is correct |
38 |
Correct |
216 ms |
185476 KB |
Output is correct |
39 |
Correct |
199 ms |
185456 KB |
Output is correct |
40 |
Correct |
197 ms |
185552 KB |
Output is correct |
41 |
Correct |
136 ms |
145176 KB |
Output is correct |
42 |
Correct |
140 ms |
144508 KB |
Output is correct |
43 |
Correct |
152 ms |
144512 KB |
Output is correct |
44 |
Correct |
195 ms |
185732 KB |
Output is correct |
45 |
Correct |
200 ms |
185684 KB |
Output is correct |
46 |
Correct |
198 ms |
185676 KB |
Output is correct |
47 |
Correct |
197 ms |
185700 KB |
Output is correct |
48 |
Correct |
194 ms |
185972 KB |
Output is correct |
49 |
Correct |
201 ms |
185968 KB |
Output is correct |
50 |
Correct |
227 ms |
186076 KB |
Output is correct |
51 |
Correct |
127 ms |
142592 KB |
Output is correct |
52 |
Correct |
1396 ms |
407508 KB |
Output is correct |
53 |
Correct |
1357 ms |
407668 KB |
Output is correct |
54 |
Correct |
1355 ms |
404996 KB |
Output is correct |
55 |
Correct |
1400 ms |
404808 KB |
Output is correct |
56 |
Correct |
1402 ms |
405820 KB |
Output is correct |
57 |
Correct |
1404 ms |
407284 KB |
Output is correct |
58 |
Correct |
128 ms |
142668 KB |
Output is correct |
59 |
Correct |
129 ms |
142720 KB |
Output is correct |
60 |
Correct |
130 ms |
142612 KB |
Output is correct |
61 |
Correct |
1395 ms |
407624 KB |
Output is correct |
62 |
Correct |
1380 ms |
404504 KB |
Output is correct |
63 |
Correct |
1362 ms |
409628 KB |
Output is correct |
64 |
Correct |
184 ms |
169744 KB |
Output is correct |
65 |
Correct |
193 ms |
171124 KB |
Output is correct |
66 |
Correct |
1385 ms |
407632 KB |
Output is correct |
67 |
Correct |
1355 ms |
407360 KB |
Output is correct |
68 |
Correct |
1032 ms |
403448 KB |
Output is correct |
69 |
Correct |
1066 ms |
403568 KB |
Output is correct |
70 |
Correct |
1024 ms |
402532 KB |
Output is correct |
71 |
Correct |
1063 ms |
402680 KB |
Output is correct |
72 |
Correct |
1049 ms |
402476 KB |
Output is correct |
73 |
Correct |
929 ms |
398204 KB |
Output is correct |
74 |
Correct |
1205 ms |
445952 KB |
Output is correct |
75 |
Correct |
1170 ms |
445656 KB |
Output is correct |
76 |
Correct |
1108 ms |
446592 KB |
Output is correct |
77 |
Correct |
1102 ms |
445364 KB |
Output is correct |
78 |
Correct |
1020 ms |
412212 KB |
Output is correct |
79 |
Correct |
1004 ms |
411812 KB |
Output is correct |
80 |
Correct |
992 ms |
410344 KB |
Output is correct |
81 |
Correct |
887 ms |
422808 KB |
Output is correct |
82 |
Correct |
1066 ms |
469300 KB |
Output is correct |
83 |
Correct |
1181 ms |
469268 KB |
Output is correct |
84 |
Correct |
1132 ms |
480312 KB |
Output is correct |
85 |
Correct |
1242 ms |
518304 KB |
Output is correct |
86 |
Correct |
1201 ms |
517260 KB |
Output is correct |
87 |
Correct |
1214 ms |
517212 KB |
Output is correct |