Submission #954366

# Submission time Handle Problem Language Result Execution time Memory
954366 2024-03-27T17:13:09 Z amirhoseinfar1385 Swapping Cities (APIO20_swap) C++17
100 / 100
294 ms 48080 KB
#include "swap.h"
#include<bits/stdc++.h>
using namespace std;
struct yal{
	int u,v,w,vas;
	bool operator <(yal b){
		return w<b.w;
	}
	int getad(int fu){
		return (fu^u^v);
	}
};
const int maxn=100000+10,lg=17;
int n,m,wlca[lg][maxn],lca[lg][maxn],inf=1e9+5,mainres[maxn],timea;
vector<yal>alle;
vector<int>adjt[maxn];
pair<int,int>stf[maxn];

bool zird(int u,int v){
	return stf[v].first<=stf[u].first&&stf[v].second>=stf[u].second;
}

struct dsu{
	int par[maxn],val[maxn];
	vector<int>allv[maxn];
	dsu(){
		for(int i=0;i<maxn;i++){
			par[i]=i;
			val[i]=inf;
			allv[i].push_back(i);
		}
	};
	int root(int u){
		if(u==par[u]){
			return u;
		}
		return par[u]=root(par[u]);
	}
	int con(int u,int v,int w){
		int rootu=root(u),rootv=root(v);
		if(rootu!=rootv){
			par[rootv]=rootu;
			if((int)allv[rootu].size()<(int)allv[rootv].size()){
				swap(allv[rootu],allv[rootv]);
			}
			for(auto x:allv[rootv]){
				allv[rootu].push_back(x);
			}
			if(val[rootu]<inf||val[rootv]<inf){
				val[rootu]=w;
			}
			if(val[rootu]<inf){
				for(auto x:allv[rootu]){
					mainres[x]=min(mainres[x],val[rootu]);
				}
				allv[rootu].clear();
			}
			return 1;
		}else{
			return 0;
		}
	}
	void tagh(int u,int w){
		u=root(u);
		for(auto x:allv[u]){
			mainres[x]=min(mainres[x],w);
		}
		allv[u].clear();
		val[u]=min(w,val[u]);
	}
}ds;
int av=inf;

void dfs(int u=1,int par=-1){
	timea++;
	stf[u].first=timea;
	for(auto x:adjt[u]){
		int v=alle[x].getad(u);
		if(v==par){
			continue;
		}
		wlca[0][v]=alle[x].w;
		lca[0][v]=u;
		dfs(v,u);
	}
	stf[u].second=timea;
}

void callca(){
	for(int i=1;i<lg;i++){
		for(int j=1;j<=n;j++){
			lca[i][j]=lca[i-1][lca[i-1][j]];
			wlca[i][j]=max(wlca[i-1][j],wlca[i-1][lca[i-1][j]]);
		}
	}
}

int getlca(int u,int v){
	if(zird(u,v)){
		return v;
	}
	if(zird(v,u)){
		return u;
	}
	for(int i=lg-1;i>=0;i--){
		if(lca[i][u]!=0&&zird(v,lca[i][u])==0){
			u=lca[i][u];
		}
	}
	return lca[0][u];
}

int getwlca(int u,int v){
	if(u==v){
		return 0;
	}
	int ret=0;
	for(int i=lg-1;i>=0;i--){
		if(lca[i][u]!=0&&zird(v,lca[i][u])==0){
			ret=max(ret,wlca[i][u]);
			u=lca[i][u];
		}
	}
	ret=max(ret,wlca[0][u]);
	return ret;
}

void init(int N, int M,std::vector<int> U, std::vector<int> V, std::vector<int> W) {
	n=N;
	m=M;
	alle.resize(m);
	for(int i=0;i<maxn;i++){
		mainres[i]=inf;
	}
	for(int i=0;i<m;i++){
		U[i]++;
		V[i]++;
		alle[i].u=U[i];
		alle[i].v=V[i];
		alle[i].w=W[i];
	}
	sort(alle.begin(),alle.end());
	for(int i=0;i<m;i++){
		if(ds.con(alle[i].u,alle[i].v,alle[i].w)){
			alle[i].vas=1;
			adjt[alle[i].u].push_back(i);
			adjt[alle[i].v].push_back(i);
			if((int)adjt[alle[i].u].size()>2||(int)adjt[alle[i].v].size()>2){
				ds.tagh(alle[i].u,alle[i].w);
			}
		}else{
			ds.tagh(alle[i].u,alle[i].w);
		}
	}
	dfs();
	callca();
}
int mx;
void dfsres(int u,int v){
	int uv=getlca(u,v);
	mx=max(getwlca(u,uv),getwlca(v,uv));
}

int getMinimumFuelCapacity(int X, int Y) {
	X++;
	Y++;
  mx=inf;
  dfsres(X,Y);
  long long ret=max(mx,min(mainres[X],mainres[Y]));
  if(ret>=inf){
  	return -1;
  }
  return ret;
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 22264 KB Output is correct
2 Correct 8 ms 22108 KB Output is correct
3 Correct 7 ms 22108 KB Output is correct
4 Correct 8 ms 22108 KB Output is correct
5 Correct 7 ms 22108 KB Output is correct
6 Correct 7 ms 22108 KB Output is correct
7 Correct 8 ms 22108 KB Output is correct
8 Correct 8 ms 22616 KB Output is correct
9 Correct 67 ms 34352 KB Output is correct
10 Correct 81 ms 37884 KB Output is correct
11 Correct 82 ms 37488 KB Output is correct
12 Correct 85 ms 38272 KB Output is correct
13 Correct 68 ms 37192 KB Output is correct
14 Correct 78 ms 34000 KB Output is correct
15 Correct 232 ms 39716 KB Output is correct
16 Correct 238 ms 38084 KB Output is correct
17 Correct 229 ms 41780 KB Output is correct
18 Correct 233 ms 38064 KB Output is correct
19 Correct 94 ms 28240 KB Output is correct
20 Correct 246 ms 40264 KB Output is correct
21 Correct 251 ms 39436 KB Output is correct
22 Correct 245 ms 41268 KB Output is correct
23 Correct 220 ms 39032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 22264 KB Output is correct
2 Correct 8 ms 22108 KB Output is correct
3 Correct 119 ms 33392 KB Output is correct
4 Correct 116 ms 37404 KB Output is correct
5 Correct 122 ms 37604 KB Output is correct
6 Correct 124 ms 37440 KB Output is correct
7 Correct 123 ms 37668 KB Output is correct
8 Correct 114 ms 37124 KB Output is correct
9 Correct 117 ms 37452 KB Output is correct
10 Correct 118 ms 37112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 22264 KB Output is correct
2 Correct 8 ms 22108 KB Output is correct
3 Correct 7 ms 22108 KB Output is correct
4 Correct 8 ms 22108 KB Output is correct
5 Correct 7 ms 22108 KB Output is correct
6 Correct 7 ms 22108 KB Output is correct
7 Correct 8 ms 22108 KB Output is correct
8 Correct 8 ms 22616 KB Output is correct
9 Correct 6 ms 22108 KB Output is correct
10 Correct 7 ms 22108 KB Output is correct
11 Correct 7 ms 22300 KB Output is correct
12 Correct 7 ms 22108 KB Output is correct
13 Correct 7 ms 22152 KB Output is correct
14 Correct 9 ms 22360 KB Output is correct
15 Correct 7 ms 22108 KB Output is correct
16 Correct 10 ms 22104 KB Output is correct
17 Correct 8 ms 22108 KB Output is correct
18 Correct 7 ms 22108 KB Output is correct
19 Correct 7 ms 22104 KB Output is correct
20 Correct 7 ms 22108 KB Output is correct
21 Correct 7 ms 22108 KB Output is correct
22 Correct 8 ms 22108 KB Output is correct
23 Correct 7 ms 22108 KB Output is correct
24 Correct 8 ms 22108 KB Output is correct
25 Correct 8 ms 22104 KB Output is correct
26 Correct 8 ms 22364 KB Output is correct
27 Correct 7 ms 22104 KB Output is correct
28 Correct 7 ms 22108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 22108 KB Output is correct
2 Correct 8 ms 22264 KB Output is correct
3 Correct 8 ms 22108 KB Output is correct
4 Correct 7 ms 22108 KB Output is correct
5 Correct 8 ms 22108 KB Output is correct
6 Correct 7 ms 22108 KB Output is correct
7 Correct 7 ms 22108 KB Output is correct
8 Correct 8 ms 22108 KB Output is correct
9 Correct 8 ms 22616 KB Output is correct
10 Correct 67 ms 34352 KB Output is correct
11 Correct 81 ms 37884 KB Output is correct
12 Correct 82 ms 37488 KB Output is correct
13 Correct 85 ms 38272 KB Output is correct
14 Correct 68 ms 37192 KB Output is correct
15 Correct 7 ms 22108 KB Output is correct
16 Correct 7 ms 22300 KB Output is correct
17 Correct 7 ms 22108 KB Output is correct
18 Correct 7 ms 22152 KB Output is correct
19 Correct 9 ms 22360 KB Output is correct
20 Correct 7 ms 22108 KB Output is correct
21 Correct 10 ms 22104 KB Output is correct
22 Correct 8 ms 22108 KB Output is correct
23 Correct 7 ms 22108 KB Output is correct
24 Correct 7 ms 22104 KB Output is correct
25 Correct 7 ms 22108 KB Output is correct
26 Correct 7 ms 22108 KB Output is correct
27 Correct 8 ms 22108 KB Output is correct
28 Correct 7 ms 22108 KB Output is correct
29 Correct 8 ms 22108 KB Output is correct
30 Correct 8 ms 22104 KB Output is correct
31 Correct 8 ms 22364 KB Output is correct
32 Correct 7 ms 22104 KB Output is correct
33 Correct 7 ms 22108 KB Output is correct
34 Correct 14 ms 23128 KB Output is correct
35 Correct 87 ms 37292 KB Output is correct
36 Correct 81 ms 34896 KB Output is correct
37 Correct 80 ms 32848 KB Output is correct
38 Correct 73 ms 31568 KB Output is correct
39 Correct 72 ms 30724 KB Output is correct
40 Correct 64 ms 29776 KB Output is correct
41 Correct 87 ms 36236 KB Output is correct
42 Correct 89 ms 37444 KB Output is correct
43 Correct 65 ms 35152 KB Output is correct
44 Correct 75 ms 30544 KB Output is correct
45 Correct 82 ms 31312 KB Output is correct
46 Correct 116 ms 34144 KB Output is correct
47 Correct 83 ms 31316 KB Output is correct
48 Correct 78 ms 31056 KB Output is correct
49 Correct 51 ms 28656 KB Output is correct
50 Correct 44 ms 27148 KB Output is correct
51 Correct 67 ms 29852 KB Output is correct
52 Correct 102 ms 36780 KB Output is correct
53 Correct 100 ms 33616 KB Output is correct
54 Correct 119 ms 40340 KB Output is correct
55 Correct 68 ms 36432 KB Output is correct
56 Correct 99 ms 33360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 22264 KB Output is correct
2 Correct 8 ms 22108 KB Output is correct
3 Correct 7 ms 22108 KB Output is correct
4 Correct 8 ms 22108 KB Output is correct
5 Correct 7 ms 22108 KB Output is correct
6 Correct 7 ms 22108 KB Output is correct
7 Correct 8 ms 22108 KB Output is correct
8 Correct 8 ms 22616 KB Output is correct
9 Correct 67 ms 34352 KB Output is correct
10 Correct 81 ms 37884 KB Output is correct
11 Correct 82 ms 37488 KB Output is correct
12 Correct 85 ms 38272 KB Output is correct
13 Correct 68 ms 37192 KB Output is correct
14 Correct 78 ms 34000 KB Output is correct
15 Correct 232 ms 39716 KB Output is correct
16 Correct 238 ms 38084 KB Output is correct
17 Correct 229 ms 41780 KB Output is correct
18 Correct 233 ms 38064 KB Output is correct
19 Correct 119 ms 33392 KB Output is correct
20 Correct 116 ms 37404 KB Output is correct
21 Correct 122 ms 37604 KB Output is correct
22 Correct 124 ms 37440 KB Output is correct
23 Correct 123 ms 37668 KB Output is correct
24 Correct 114 ms 37124 KB Output is correct
25 Correct 117 ms 37452 KB Output is correct
26 Correct 118 ms 37112 KB Output is correct
27 Correct 7 ms 22108 KB Output is correct
28 Correct 7 ms 22300 KB Output is correct
29 Correct 7 ms 22108 KB Output is correct
30 Correct 7 ms 22152 KB Output is correct
31 Correct 9 ms 22360 KB Output is correct
32 Correct 7 ms 22108 KB Output is correct
33 Correct 10 ms 22104 KB Output is correct
34 Correct 8 ms 22108 KB Output is correct
35 Correct 7 ms 22108 KB Output is correct
36 Correct 14 ms 23128 KB Output is correct
37 Correct 87 ms 37292 KB Output is correct
38 Correct 81 ms 34896 KB Output is correct
39 Correct 80 ms 32848 KB Output is correct
40 Correct 73 ms 31568 KB Output is correct
41 Correct 72 ms 30724 KB Output is correct
42 Correct 64 ms 29776 KB Output is correct
43 Correct 87 ms 36236 KB Output is correct
44 Correct 89 ms 37444 KB Output is correct
45 Correct 65 ms 35152 KB Output is correct
46 Correct 75 ms 30544 KB Output is correct
47 Correct 23 ms 23888 KB Output is correct
48 Correct 233 ms 42748 KB Output is correct
49 Correct 248 ms 40740 KB Output is correct
50 Correct 233 ms 39420 KB Output is correct
51 Correct 237 ms 38420 KB Output is correct
52 Correct 203 ms 37124 KB Output is correct
53 Correct 173 ms 35144 KB Output is correct
54 Correct 250 ms 43452 KB Output is correct
55 Correct 268 ms 43948 KB Output is correct
56 Correct 237 ms 42432 KB Output is correct
57 Correct 204 ms 38328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 22108 KB Output is correct
2 Correct 8 ms 22264 KB Output is correct
3 Correct 8 ms 22108 KB Output is correct
4 Correct 7 ms 22108 KB Output is correct
5 Correct 8 ms 22108 KB Output is correct
6 Correct 7 ms 22108 KB Output is correct
7 Correct 7 ms 22108 KB Output is correct
8 Correct 8 ms 22108 KB Output is correct
9 Correct 8 ms 22616 KB Output is correct
10 Correct 67 ms 34352 KB Output is correct
11 Correct 81 ms 37884 KB Output is correct
12 Correct 82 ms 37488 KB Output is correct
13 Correct 85 ms 38272 KB Output is correct
14 Correct 68 ms 37192 KB Output is correct
15 Correct 78 ms 34000 KB Output is correct
16 Correct 232 ms 39716 KB Output is correct
17 Correct 238 ms 38084 KB Output is correct
18 Correct 229 ms 41780 KB Output is correct
19 Correct 233 ms 38064 KB Output is correct
20 Correct 119 ms 33392 KB Output is correct
21 Correct 116 ms 37404 KB Output is correct
22 Correct 122 ms 37604 KB Output is correct
23 Correct 124 ms 37440 KB Output is correct
24 Correct 123 ms 37668 KB Output is correct
25 Correct 114 ms 37124 KB Output is correct
26 Correct 117 ms 37452 KB Output is correct
27 Correct 118 ms 37112 KB Output is correct
28 Correct 7 ms 22108 KB Output is correct
29 Correct 7 ms 22300 KB Output is correct
30 Correct 7 ms 22108 KB Output is correct
31 Correct 7 ms 22152 KB Output is correct
32 Correct 9 ms 22360 KB Output is correct
33 Correct 7 ms 22108 KB Output is correct
34 Correct 10 ms 22104 KB Output is correct
35 Correct 8 ms 22108 KB Output is correct
36 Correct 7 ms 22108 KB Output is correct
37 Correct 14 ms 23128 KB Output is correct
38 Correct 87 ms 37292 KB Output is correct
39 Correct 81 ms 34896 KB Output is correct
40 Correct 80 ms 32848 KB Output is correct
41 Correct 73 ms 31568 KB Output is correct
42 Correct 72 ms 30724 KB Output is correct
43 Correct 64 ms 29776 KB Output is correct
44 Correct 87 ms 36236 KB Output is correct
45 Correct 89 ms 37444 KB Output is correct
46 Correct 65 ms 35152 KB Output is correct
47 Correct 75 ms 30544 KB Output is correct
48 Correct 23 ms 23888 KB Output is correct
49 Correct 233 ms 42748 KB Output is correct
50 Correct 248 ms 40740 KB Output is correct
51 Correct 233 ms 39420 KB Output is correct
52 Correct 237 ms 38420 KB Output is correct
53 Correct 203 ms 37124 KB Output is correct
54 Correct 173 ms 35144 KB Output is correct
55 Correct 250 ms 43452 KB Output is correct
56 Correct 268 ms 43948 KB Output is correct
57 Correct 237 ms 42432 KB Output is correct
58 Correct 204 ms 38328 KB Output is correct
59 Correct 94 ms 28240 KB Output is correct
60 Correct 246 ms 40264 KB Output is correct
61 Correct 251 ms 39436 KB Output is correct
62 Correct 245 ms 41268 KB Output is correct
63 Correct 220 ms 39032 KB Output is correct
64 Correct 7 ms 22104 KB Output is correct
65 Correct 7 ms 22108 KB Output is correct
66 Correct 7 ms 22108 KB Output is correct
67 Correct 8 ms 22108 KB Output is correct
68 Correct 7 ms 22108 KB Output is correct
69 Correct 8 ms 22108 KB Output is correct
70 Correct 8 ms 22104 KB Output is correct
71 Correct 8 ms 22364 KB Output is correct
72 Correct 7 ms 22104 KB Output is correct
73 Correct 7 ms 22108 KB Output is correct
74 Correct 82 ms 31312 KB Output is correct
75 Correct 116 ms 34144 KB Output is correct
76 Correct 83 ms 31316 KB Output is correct
77 Correct 78 ms 31056 KB Output is correct
78 Correct 51 ms 28656 KB Output is correct
79 Correct 44 ms 27148 KB Output is correct
80 Correct 67 ms 29852 KB Output is correct
81 Correct 102 ms 36780 KB Output is correct
82 Correct 100 ms 33616 KB Output is correct
83 Correct 119 ms 40340 KB Output is correct
84 Correct 68 ms 36432 KB Output is correct
85 Correct 99 ms 33360 KB Output is correct
86 Correct 61 ms 27552 KB Output is correct
87 Correct 266 ms 40264 KB Output is correct
88 Correct 257 ms 40252 KB Output is correct
89 Correct 180 ms 37236 KB Output is correct
90 Correct 131 ms 35504 KB Output is correct
91 Correct 140 ms 35784 KB Output is correct
92 Correct 170 ms 37552 KB Output is correct
93 Correct 281 ms 44708 KB Output is correct
94 Correct 216 ms 42048 KB Output is correct
95 Correct 294 ms 48080 KB Output is correct
96 Correct 222 ms 40892 KB Output is correct
97 Correct 230 ms 40028 KB Output is correct