# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
999423 |
2024-06-15T13:01:03 Z |
vjudge1 |
LOSTIKS (INOI20_lostiks) |
C++17 |
|
2000 ms |
181084 KB |
#include <bits/stdc++.h>
#define int ll
#define MAX 1000001
#define INF INT_MAX
#define MOD 1000000007
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ins insert
#define ff first
#define ss second
#define all(a) a.begin(),a.end()
#define lb(a,b) lower_bound(all(a),b)
#define ub(a,b) upper_bound(all(a),b)
#define sortv(a) sort(all(a))
#define outputar(a,b){\
for(int i=0;i<b;i++){\
cout << a[i] << " ";\
}\
cout << endl;\
}
#define outputvec(a){\
for(auto x:a){\
cout << (int)x << " ";\
}\
cout << endl;\
}
#define reset(a,n,v){\
for(int i=0;i<n;i++){\
a[i]=v;\
}\
}
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef tuple<ll,ll,ll> tll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef double db;
typedef long double ldb;
inline void USACO(string filename){
freopen((filename+".in").c_str(),"r",stdin);
freopen((filename+".out").c_str(),"w",stdout);
}
int n,q,t=1,m,k,x,y,z,x2,y2,z2,a[MAX],b[MAX],d[MAX];
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//string s[MAX],str[MAX];
//int e[1001][1001];
string s1,s2,s3;
const int mod = 998244353;
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
vector<vector<int>> g;
int st[MAX][20],dep[MAX],pre[MAX],cnt=0;
int lca(int x,int y){
if(dep[x]<dep[y]){
swap(x,y);
}
int h=dep[x]-dep[y];
for(int i=0;i<20;i++){
if((h&(1<<i))){
x=st[x][i];
}
}
if(x==y){
return x;
}
for(int i=19;i>=0;i--){
if(st[x][i]!=st[y][i]){
x=st[x][i];
y=st[y][i];
}
}
return pre[x];
}
vector<array<int,3>> c;
bool check(int x,int y,int z){
bool ok=true;
for(int i=0;i<cnt;i++){
if(!(x&(1<<i))){
int h1,h2,h3;
h1=lca(c[i][0],y);
h2=lca(c[i][0],z);
h3=lca(y,z);
if((h1==c[i][0] && dep[h1]>dep[h3]) || (h2==c[i][0] && dep[h2]>dep[h3])){
ok=false;
}
}
}
return ok;
}
void dfs(int v,int prev){
st[v][0]=prev;
pre[v]=prev;
for(auto x:g[v]){
if(x==prev){
continue;
}
dep[x]=dep[v]+1;
dfs(x,v);
}
}
int dist(int x,int y){
return dep[x]+dep[y]-2*dep[lca(x,y)];
}
int dis[21][21];
int dp[1<<20][21];
void solve(){
cin >> n;
g.clear();
g.resize(n+1);
cin >> m >> k;
for(int i=0;i<n-1;i++){
cin >> x >> y >> z;
g[x].pb(y);
g[y].pb(x);
a[i]=z;
if(a[i]!=0){
c.pb({x,y,i});
cnt++;
}
}
dfs(m,-1);
for(int i=1;i<20;i++){
for(int j=1;j<=n;j++){
if(st[j][i-1]!=-1){
st[j][i]=st[st[j][i-1]][i-1];
}
}
}
for(int i=0;i<cnt;i++){
int n1,n2;
n1=c[0][0];
n2=c[0][1];
if(dep[n1]<dep[n2]){
swap(n1,n2);
}
c.pb({n1,n2,c[0][2]});
c.erase(c.begin());
}
int h=(1<<cnt);
for(int i=0;i<h;i++){
for(int j=0;j<=cnt;j++){
dp[i][j]=INF;
}
}
dp[0][cnt]=0;
int res=INF;
for(int i=0;i<cnt;i++){
b[i]=dist(a[c[i][2]],c[i][1]);
}
for(int i=0;i<=cnt;i++){
for(int j=0;j<cnt;j++){
int num;
if(i==cnt){
num=m;
}
else{
num=c[i][1];
}
dis[i][j]=dist(num,a[c[j][2]]);
}
}
for(int i=0;i<h;i++){
for(int j=0;j<=cnt;j++){
if(dp[i][j]==INF){
continue;
}
int num;
if(j==cnt){
num=m;
}
else{
num=c[j][1];
}
for(int z=0;z<cnt;z++){
if(!(i&(1<<z)) && check(i,num,a[c[z][2]]) && check(i,a[c[z][2]],c[z][1])){
dp[i+(1<<z)][z]=min(dp[i+(1<<z)][z],dp[i][j]+dis[j][z]+b[z]);
}
}
if(check(i,num,k)){
res=min(res,dp[i][j]+dist(num,k));
}
}
}
if(res==INF){
res=-1;
}
cout << res << "\n";
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//freopen("input13.txt","r",stdin);
//cin >> t;
ll cnt1=1;
while(t--){
solve();
cnt1++;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10584 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
40 ms |
37760 KB |
Output is correct |
4 |
Correct |
40 ms |
37972 KB |
Output is correct |
5 |
Correct |
41 ms |
37968 KB |
Output is correct |
6 |
Correct |
38 ms |
37460 KB |
Output is correct |
7 |
Correct |
36 ms |
37976 KB |
Output is correct |
8 |
Correct |
41 ms |
37712 KB |
Output is correct |
9 |
Correct |
37 ms |
37712 KB |
Output is correct |
10 |
Correct |
37 ms |
37636 KB |
Output is correct |
11 |
Correct |
41 ms |
37488 KB |
Output is correct |
12 |
Correct |
42 ms |
38480 KB |
Output is correct |
13 |
Correct |
44 ms |
38228 KB |
Output is correct |
14 |
Correct |
34 ms |
38152 KB |
Output is correct |
15 |
Correct |
36 ms |
38736 KB |
Output is correct |
16 |
Correct |
37 ms |
38840 KB |
Output is correct |
17 |
Correct |
36 ms |
38992 KB |
Output is correct |
18 |
Correct |
38 ms |
39252 KB |
Output is correct |
19 |
Correct |
43 ms |
41676 KB |
Output is correct |
20 |
Correct |
38 ms |
41760 KB |
Output is correct |
21 |
Correct |
39 ms |
41344 KB |
Output is correct |
22 |
Correct |
1 ms |
10588 KB |
Output is correct |
23 |
Correct |
1 ms |
10588 KB |
Output is correct |
24 |
Correct |
1 ms |
10588 KB |
Output is correct |
25 |
Correct |
1 ms |
10588 KB |
Output is correct |
26 |
Correct |
1 ms |
10584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Correct |
105 ms |
99500 KB |
Output is correct |
6 |
Correct |
52 ms |
99420 KB |
Output is correct |
7 |
Correct |
247 ms |
99516 KB |
Output is correct |
8 |
Correct |
244 ms |
99416 KB |
Output is correct |
9 |
Correct |
186 ms |
99460 KB |
Output is correct |
10 |
Correct |
32 ms |
99420 KB |
Output is correct |
11 |
Correct |
32 ms |
99420 KB |
Output is correct |
12 |
Correct |
31 ms |
99420 KB |
Output is correct |
13 |
Correct |
33 ms |
99420 KB |
Output is correct |
14 |
Correct |
33 ms |
99420 KB |
Output is correct |
15 |
Correct |
75 ms |
99440 KB |
Output is correct |
16 |
Correct |
88 ms |
99420 KB |
Output is correct |
17 |
Correct |
45 ms |
99420 KB |
Output is correct |
18 |
Correct |
36 ms |
99672 KB |
Output is correct |
19 |
Correct |
32 ms |
99676 KB |
Output is correct |
20 |
Correct |
38 ms |
99672 KB |
Output is correct |
21 |
Correct |
32 ms |
99672 KB |
Output is correct |
22 |
Correct |
32 ms |
99672 KB |
Output is correct |
23 |
Correct |
32 ms |
99844 KB |
Output is correct |
24 |
Correct |
33 ms |
99672 KB |
Output is correct |
25 |
Execution timed out |
2050 ms |
181084 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10584 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
40 ms |
37760 KB |
Output is correct |
4 |
Correct |
40 ms |
37972 KB |
Output is correct |
5 |
Correct |
41 ms |
37968 KB |
Output is correct |
6 |
Correct |
38 ms |
37460 KB |
Output is correct |
7 |
Correct |
36 ms |
37976 KB |
Output is correct |
8 |
Correct |
41 ms |
37712 KB |
Output is correct |
9 |
Correct |
37 ms |
37712 KB |
Output is correct |
10 |
Correct |
37 ms |
37636 KB |
Output is correct |
11 |
Correct |
41 ms |
37488 KB |
Output is correct |
12 |
Correct |
42 ms |
38480 KB |
Output is correct |
13 |
Correct |
44 ms |
38228 KB |
Output is correct |
14 |
Correct |
34 ms |
38152 KB |
Output is correct |
15 |
Correct |
36 ms |
38736 KB |
Output is correct |
16 |
Correct |
37 ms |
38840 KB |
Output is correct |
17 |
Correct |
36 ms |
38992 KB |
Output is correct |
18 |
Correct |
38 ms |
39252 KB |
Output is correct |
19 |
Correct |
43 ms |
41676 KB |
Output is correct |
20 |
Correct |
38 ms |
41760 KB |
Output is correct |
21 |
Correct |
39 ms |
41344 KB |
Output is correct |
22 |
Correct |
1 ms |
10588 KB |
Output is correct |
23 |
Correct |
1 ms |
10588 KB |
Output is correct |
24 |
Correct |
1 ms |
10588 KB |
Output is correct |
25 |
Correct |
1 ms |
10588 KB |
Output is correct |
26 |
Correct |
1 ms |
10584 KB |
Output is correct |
27 |
Correct |
1 ms |
10588 KB |
Output is correct |
28 |
Correct |
2 ms |
10588 KB |
Output is correct |
29 |
Correct |
2 ms |
10844 KB |
Output is correct |
30 |
Correct |
2 ms |
10844 KB |
Output is correct |
31 |
Correct |
105 ms |
99500 KB |
Output is correct |
32 |
Correct |
52 ms |
99420 KB |
Output is correct |
33 |
Correct |
247 ms |
99516 KB |
Output is correct |
34 |
Correct |
244 ms |
99416 KB |
Output is correct |
35 |
Correct |
186 ms |
99460 KB |
Output is correct |
36 |
Correct |
32 ms |
99420 KB |
Output is correct |
37 |
Correct |
32 ms |
99420 KB |
Output is correct |
38 |
Correct |
31 ms |
99420 KB |
Output is correct |
39 |
Correct |
33 ms |
99420 KB |
Output is correct |
40 |
Correct |
33 ms |
99420 KB |
Output is correct |
41 |
Correct |
75 ms |
99440 KB |
Output is correct |
42 |
Correct |
88 ms |
99420 KB |
Output is correct |
43 |
Correct |
45 ms |
99420 KB |
Output is correct |
44 |
Correct |
36 ms |
99672 KB |
Output is correct |
45 |
Correct |
32 ms |
99676 KB |
Output is correct |
46 |
Correct |
38 ms |
99672 KB |
Output is correct |
47 |
Correct |
32 ms |
99672 KB |
Output is correct |
48 |
Correct |
32 ms |
99672 KB |
Output is correct |
49 |
Correct |
32 ms |
99844 KB |
Output is correct |
50 |
Correct |
33 ms |
99672 KB |
Output is correct |
51 |
Execution timed out |
2050 ms |
181084 KB |
Time limit exceeded |
52 |
Halted |
0 ms |
0 KB |
- |