답안 #783220

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
783220 2023-07-14T18:11:01 Z amirhoseinfar1385 LOSTIKS (INOI20_lostiks) C++14
100 / 100
1475 ms 518200 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;
}
 
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))];
			}
		}
	}
}
 
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];
}
 
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";
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 129 ms 142644 KB Output is correct
2 Correct 126 ms 142664 KB Output is correct
3 Correct 181 ms 167220 KB Output is correct
4 Correct 182 ms 167304 KB Output is correct
5 Correct 176 ms 167240 KB Output is correct
6 Correct 187 ms 167224 KB Output is correct
7 Correct 177 ms 167440 KB Output is correct
8 Correct 178 ms 167432 KB Output is correct
9 Correct 187 ms 167568 KB Output is correct
10 Correct 179 ms 167368 KB Output is correct
11 Correct 179 ms 167360 KB Output is correct
12 Correct 174 ms 169336 KB Output is correct
13 Correct 174 ms 168828 KB Output is correct
14 Correct 178 ms 168496 KB Output is correct
15 Correct 185 ms 169052 KB Output is correct
16 Correct 177 ms 171508 KB Output is correct
17 Correct 176 ms 171548 KB Output is correct
18 Correct 181 ms 172060 KB Output is correct
19 Correct 183 ms 176040 KB Output is correct
20 Correct 182 ms 175952 KB Output is correct
21 Correct 180 ms 175680 KB Output is correct
22 Correct 131 ms 142712 KB Output is correct
23 Correct 129 ms 142640 KB Output is correct
24 Correct 132 ms 142696 KB Output is correct
25 Correct 129 ms 142680 KB Output is correct
26 Correct 126 ms 142684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 127 ms 142652 KB Output is correct
2 Correct 128 ms 142668 KB Output is correct
3 Correct 129 ms 142624 KB Output is correct
4 Correct 128 ms 142784 KB Output is correct
5 Correct 133 ms 144972 KB Output is correct
6 Correct 133 ms 144760 KB Output is correct
7 Correct 129 ms 144416 KB Output is correct
8 Correct 128 ms 144452 KB Output is correct
9 Correct 131 ms 144376 KB Output is correct
10 Correct 205 ms 185476 KB Output is correct
11 Correct 207 ms 185408 KB Output is correct
12 Correct 206 ms 185484 KB Output is correct
13 Correct 207 ms 185420 KB Output is correct
14 Correct 210 ms 185492 KB Output is correct
15 Correct 137 ms 145100 KB Output is correct
16 Correct 129 ms 144484 KB Output is correct
17 Correct 130 ms 144464 KB Output is correct
18 Correct 204 ms 185664 KB Output is correct
19 Correct 204 ms 185604 KB Output is correct
20 Correct 249 ms 185580 KB Output is correct
21 Correct 207 ms 185624 KB Output is correct
22 Correct 211 ms 185984 KB Output is correct
23 Correct 207 ms 185904 KB Output is correct
24 Correct 211 ms 186056 KB Output is correct
25 Correct 126 ms 142632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 129 ms 142644 KB Output is correct
2 Correct 126 ms 142664 KB Output is correct
3 Correct 181 ms 167220 KB Output is correct
4 Correct 182 ms 167304 KB Output is correct
5 Correct 176 ms 167240 KB Output is correct
6 Correct 187 ms 167224 KB Output is correct
7 Correct 177 ms 167440 KB Output is correct
8 Correct 178 ms 167432 KB Output is correct
9 Correct 187 ms 167568 KB Output is correct
10 Correct 179 ms 167368 KB Output is correct
11 Correct 179 ms 167360 KB Output is correct
12 Correct 174 ms 169336 KB Output is correct
13 Correct 174 ms 168828 KB Output is correct
14 Correct 178 ms 168496 KB Output is correct
15 Correct 185 ms 169052 KB Output is correct
16 Correct 177 ms 171508 KB Output is correct
17 Correct 176 ms 171548 KB Output is correct
18 Correct 181 ms 172060 KB Output is correct
19 Correct 183 ms 176040 KB Output is correct
20 Correct 182 ms 175952 KB Output is correct
21 Correct 180 ms 175680 KB Output is correct
22 Correct 131 ms 142712 KB Output is correct
23 Correct 129 ms 142640 KB Output is correct
24 Correct 132 ms 142696 KB Output is correct
25 Correct 129 ms 142680 KB Output is correct
26 Correct 126 ms 142684 KB Output is correct
27 Correct 127 ms 142652 KB Output is correct
28 Correct 128 ms 142668 KB Output is correct
29 Correct 129 ms 142624 KB Output is correct
30 Correct 128 ms 142784 KB Output is correct
31 Correct 133 ms 144972 KB Output is correct
32 Correct 133 ms 144760 KB Output is correct
33 Correct 129 ms 144416 KB Output is correct
34 Correct 128 ms 144452 KB Output is correct
35 Correct 131 ms 144376 KB Output is correct
36 Correct 205 ms 185476 KB Output is correct
37 Correct 207 ms 185408 KB Output is correct
38 Correct 206 ms 185484 KB Output is correct
39 Correct 207 ms 185420 KB Output is correct
40 Correct 210 ms 185492 KB Output is correct
41 Correct 137 ms 145100 KB Output is correct
42 Correct 129 ms 144484 KB Output is correct
43 Correct 130 ms 144464 KB Output is correct
44 Correct 204 ms 185664 KB Output is correct
45 Correct 204 ms 185604 KB Output is correct
46 Correct 249 ms 185580 KB Output is correct
47 Correct 207 ms 185624 KB Output is correct
48 Correct 211 ms 185984 KB Output is correct
49 Correct 207 ms 185904 KB Output is correct
50 Correct 211 ms 186056 KB Output is correct
51 Correct 126 ms 142632 KB Output is correct
52 Correct 1436 ms 407496 KB Output is correct
53 Correct 1426 ms 407640 KB Output is correct
54 Correct 1389 ms 404920 KB Output is correct
55 Correct 1413 ms 404784 KB Output is correct
56 Correct 1383 ms 405820 KB Output is correct
57 Correct 1414 ms 407368 KB Output is correct
58 Correct 132 ms 142664 KB Output is correct
59 Correct 130 ms 142632 KB Output is correct
60 Correct 126 ms 142720 KB Output is correct
61 Correct 1366 ms 407640 KB Output is correct
62 Correct 1425 ms 404580 KB Output is correct
63 Correct 1351 ms 409576 KB Output is correct
64 Correct 181 ms 169832 KB Output is correct
65 Correct 246 ms 171092 KB Output is correct
66 Correct 1409 ms 407720 KB Output is correct
67 Correct 1475 ms 407372 KB Output is correct
68 Correct 1059 ms 403336 KB Output is correct
69 Correct 1075 ms 403564 KB Output is correct
70 Correct 1055 ms 402516 KB Output is correct
71 Correct 1022 ms 402688 KB Output is correct
72 Correct 1024 ms 402540 KB Output is correct
73 Correct 909 ms 398252 KB Output is correct
74 Correct 1127 ms 445980 KB Output is correct
75 Correct 1149 ms 445664 KB Output is correct
76 Correct 1139 ms 446540 KB Output is correct
77 Correct 1111 ms 445368 KB Output is correct
78 Correct 989 ms 412196 KB Output is correct
79 Correct 1020 ms 411740 KB Output is correct
80 Correct 1008 ms 410376 KB Output is correct
81 Correct 928 ms 422828 KB Output is correct
82 Correct 1131 ms 469288 KB Output is correct
83 Correct 1130 ms 469164 KB Output is correct
84 Correct 1254 ms 480324 KB Output is correct
85 Correct 1196 ms 518200 KB Output is correct
86 Correct 1218 ms 517096 KB Output is correct
87 Correct 1201 ms 517248 KB Output is correct