Submission #557323

# Submission time Handle Problem Language Result Execution time Memory
557323 2022-05-05T06:49:38 Z Koosha_mv Swapping Cities (APIO20_swap) C++14
100 / 100
658 ms 64956 KB
#include <bits/stdc++.h>
using namespace std;
#define dbgv(v) cout<<#v<<" = "; f(i,0,v.size()) cout<<v[i]<<" "; cout<<endl
#define dbga(a,x,y) cout<<#a<<" = "; f(i,x,y) cout<<a[i]<<" "; cout<<endl
#define erorp(x) cout<<#x<<"={"<<x.F<<" , "<<x.S<<"}"<<endl
#define eror(x) cout<<#x<<'='<<(x)<<endl
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define f(i,a,b) for(int i=a;i<b;i++)
#define nb(x) __builtin_popcount(x)
#define all(v) v.begin(),v.end()
#define bit(n,k) (((n)>>(k))&1)
#define Add(x,y) x=(x+y)%mod
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define lst(x) x[x.size()-1]
#define sz(x) int(x.size())
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first
#include "swap.h"

const int N=2e5+99,lg=20,inf=2e9;

int n,m,h[N],res[N],col[N],deg[N],ok[N];
vector<int> vec[N],nok[N];
vector<pair<int,int>> g[N];
pair<int,int> par[lg][N];

void dfs1(int u,int p){
	f(l,1,lg) par[l][u]={par[l-1][par[l-1][u].F].F,max(par[l-1][u].S,par[l-1][par[l-1][u].F].S)};
	for(auto [v,w] : g[u]){
		if(v==p) continue ;
		h[v]=h[u]+1;
		par[0][v]={u,w};
		dfs1(v,u);
	}
}
int dist(int u,int v){
	int ans=0;
	if(h[u]<h[v]) swap(u,v);
	f_(l,lg-1,0){
		if(h[v]<=h[par[l][u].F]){
			maxm(ans,par[l][u].S);
			u=par[l][u].F;
		}
	}
	if(u==v) return ans;
	f_(l,lg-1,0){
		if(par[l][u].F!=par[l][v].F){
			maxm(ans,par[l][u].S);
			maxm(ans,par[l][v].S);
			u=par[l][u].F;
			v=par[l][v].F;
		}
	}
	maxm(ans,par[0][u].S);
	maxm(ans,par[0][v].S);
	return ans;
}
void init(int _n,int _m,std::vector<int> U, std::vector<int> V, std::vector<int> W){
	fill(res,res+N,inf);
	n=_n,m=_m;
	vector<int> p(m);
	iota(all(p),0);
	sort(all(p),[&](int u,int v){ return W[u]<W[v]; });
	f(i,0,n) vec[i].pb(i),nok[i].pb(i),col[i]=i;
	for(auto id : p){
		int u=U[id],v=V[id],w=W[id];
		//cout<<u<<" "<<v<<" "<<w<<endl;
		deg[u]++,deg[v]++;
		if(col[u]==col[v]){
			ok[col[u]]=1;
			//dbgv(nok[col[u]]);
			for(auto x : nok[col[u]]){
				//cout<<x<<" -------- "<<w<<endl;
				res[x]=w;
			}
			nok[col[u]].clear();
			continue ;
		}
		g[u].pb({v,w});
		g[v].pb({u,w});
		if(vec[col[u]].size()<vec[col[v]].size()) swap(u,v);
		ok[col[u]]|=ok[col[v]];
		ok[col[u]]|=(deg[u]>2 || deg[v]>2);
		for(auto x : nok[col[v]]) nok[col[u]].pb(x);
		for(auto x : vec[col[v]]) col[x]=col[u],vec[col[u]].pb(x);
		if(ok[col[u]]){
			for(auto x : nok[col[u]]) res[x]=w;
			nok[col[u]].clear();
		}
	}
	//f(i,0,n) eror(res[i]);
	dfs1(0,0);
}
int getMinimumFuelCapacity(int u, int v) {
	int ans=max(dist(u,v),max(res[u],res[v]));
	if(ans==inf) return -1;
	return ans;
}
/*
int32_t main(){
	int n,m;
	vector<int> U,V,W;
	cin>>n>>m;
	f(i,0,m){
		int x;
		cin>>x;
		U.pb(x);
	}
	f(i,0,m){
		int x;
		cin>>x;
		V.pb(x);
	}
	f(i,0,m){
		int x;
		cin>>x;
		W.pb(x);
	}
	init(n,m,U,V,W);
	int q;
	cin>>q;
	while(q--){
		int u,v;
		cin>>u>>v;
		cout<<getMinimumFuelCapacity(u,v)<<endl;
	}
}
*/
/*
5 6
0 0 1 1 1 2
1 2 2 3 4 3
4 4 1 2 10 3
*/

Compilation message

swap.cpp: In function 'void dfs1(int, int)':
swap.cpp:33:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   33 |  for(auto [v,w] : g[u]){
      |           ^
# Verdict Execution time Memory Grader output
1 Correct 8 ms 15316 KB Output is correct
2 Correct 8 ms 15312 KB Output is correct
3 Correct 9 ms 15316 KB Output is correct
4 Correct 9 ms 15444 KB Output is correct
5 Correct 9 ms 15704 KB Output is correct
6 Correct 9 ms 15576 KB Output is correct
7 Correct 10 ms 15696 KB Output is correct
8 Correct 9 ms 15704 KB Output is correct
9 Correct 225 ms 48704 KB Output is correct
10 Correct 207 ms 57280 KB Output is correct
11 Correct 208 ms 55736 KB Output is correct
12 Correct 252 ms 58596 KB Output is correct
13 Correct 166 ms 55244 KB Output is correct
14 Correct 214 ms 48204 KB Output is correct
15 Correct 541 ms 61220 KB Output is correct
16 Correct 566 ms 58376 KB Output is correct
17 Correct 544 ms 64380 KB Output is correct
18 Correct 510 ms 57768 KB Output is correct
19 Correct 115 ms 27356 KB Output is correct
20 Correct 575 ms 61392 KB Output is correct
21 Correct 613 ms 59652 KB Output is correct
22 Correct 595 ms 63440 KB Output is correct
23 Correct 522 ms 58468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 15316 KB Output is correct
2 Correct 8 ms 15312 KB Output is correct
3 Correct 265 ms 50236 KB Output is correct
4 Correct 279 ms 51680 KB Output is correct
5 Correct 265 ms 51020 KB Output is correct
6 Correct 247 ms 51512 KB Output is correct
7 Correct 248 ms 51476 KB Output is correct
8 Correct 244 ms 50108 KB Output is correct
9 Correct 245 ms 51124 KB Output is correct
10 Correct 245 ms 49864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 15316 KB Output is correct
2 Correct 8 ms 15312 KB Output is correct
3 Correct 9 ms 15316 KB Output is correct
4 Correct 9 ms 15444 KB Output is correct
5 Correct 9 ms 15704 KB Output is correct
6 Correct 9 ms 15576 KB Output is correct
7 Correct 10 ms 15696 KB Output is correct
8 Correct 9 ms 15704 KB Output is correct
9 Correct 9 ms 15312 KB Output is correct
10 Correct 9 ms 15572 KB Output is correct
11 Correct 10 ms 15700 KB Output is correct
12 Correct 11 ms 15580 KB Output is correct
13 Correct 11 ms 15632 KB Output is correct
14 Correct 11 ms 15588 KB Output is correct
15 Correct 10 ms 15700 KB Output is correct
16 Correct 9 ms 15700 KB Output is correct
17 Correct 10 ms 15712 KB Output is correct
18 Correct 10 ms 15600 KB Output is correct
19 Correct 9 ms 15572 KB Output is correct
20 Correct 9 ms 15732 KB Output is correct
21 Correct 9 ms 15584 KB Output is correct
22 Correct 10 ms 15460 KB Output is correct
23 Correct 10 ms 15584 KB Output is correct
24 Correct 9 ms 15620 KB Output is correct
25 Correct 10 ms 15700 KB Output is correct
26 Correct 10 ms 15700 KB Output is correct
27 Correct 9 ms 15660 KB Output is correct
28 Correct 9 ms 15712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 15312 KB Output is correct
2 Correct 8 ms 15316 KB Output is correct
3 Correct 8 ms 15312 KB Output is correct
4 Correct 9 ms 15316 KB Output is correct
5 Correct 9 ms 15444 KB Output is correct
6 Correct 9 ms 15704 KB Output is correct
7 Correct 9 ms 15576 KB Output is correct
8 Correct 10 ms 15696 KB Output is correct
9 Correct 9 ms 15704 KB Output is correct
10 Correct 225 ms 48704 KB Output is correct
11 Correct 207 ms 57280 KB Output is correct
12 Correct 208 ms 55736 KB Output is correct
13 Correct 252 ms 58596 KB Output is correct
14 Correct 166 ms 55244 KB Output is correct
15 Correct 9 ms 15572 KB Output is correct
16 Correct 10 ms 15700 KB Output is correct
17 Correct 11 ms 15580 KB Output is correct
18 Correct 11 ms 15632 KB Output is correct
19 Correct 11 ms 15588 KB Output is correct
20 Correct 10 ms 15700 KB Output is correct
21 Correct 9 ms 15700 KB Output is correct
22 Correct 10 ms 15712 KB Output is correct
23 Correct 10 ms 15600 KB Output is correct
24 Correct 9 ms 15572 KB Output is correct
25 Correct 9 ms 15732 KB Output is correct
26 Correct 9 ms 15584 KB Output is correct
27 Correct 10 ms 15460 KB Output is correct
28 Correct 10 ms 15584 KB Output is correct
29 Correct 9 ms 15620 KB Output is correct
30 Correct 10 ms 15700 KB Output is correct
31 Correct 10 ms 15700 KB Output is correct
32 Correct 9 ms 15660 KB Output is correct
33 Correct 9 ms 15712 KB Output is correct
34 Correct 22 ms 19936 KB Output is correct
35 Correct 234 ms 57312 KB Output is correct
36 Correct 237 ms 54396 KB Output is correct
37 Correct 225 ms 51536 KB Output is correct
38 Correct 245 ms 49728 KB Output is correct
39 Correct 214 ms 48588 KB Output is correct
40 Correct 202 ms 45772 KB Output is correct
41 Correct 241 ms 56344 KB Output is correct
42 Correct 228 ms 57292 KB Output is correct
43 Correct 173 ms 53180 KB Output is correct
44 Correct 226 ms 49484 KB Output is correct
45 Correct 167 ms 44412 KB Output is correct
46 Correct 223 ms 53408 KB Output is correct
47 Correct 247 ms 48736 KB Output is correct
48 Correct 180 ms 47364 KB Output is correct
49 Correct 72 ms 22660 KB Output is correct
50 Correct 62 ms 22980 KB Output is correct
51 Correct 142 ms 39396 KB Output is correct
52 Correct 272 ms 56400 KB Output is correct
53 Correct 265 ms 52872 KB Output is correct
54 Correct 276 ms 60996 KB Output is correct
55 Correct 211 ms 54356 KB Output is correct
56 Correct 267 ms 51572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 15316 KB Output is correct
2 Correct 8 ms 15312 KB Output is correct
3 Correct 9 ms 15316 KB Output is correct
4 Correct 9 ms 15444 KB Output is correct
5 Correct 9 ms 15704 KB Output is correct
6 Correct 9 ms 15576 KB Output is correct
7 Correct 10 ms 15696 KB Output is correct
8 Correct 9 ms 15704 KB Output is correct
9 Correct 225 ms 48704 KB Output is correct
10 Correct 207 ms 57280 KB Output is correct
11 Correct 208 ms 55736 KB Output is correct
12 Correct 252 ms 58596 KB Output is correct
13 Correct 166 ms 55244 KB Output is correct
14 Correct 214 ms 48204 KB Output is correct
15 Correct 541 ms 61220 KB Output is correct
16 Correct 566 ms 58376 KB Output is correct
17 Correct 544 ms 64380 KB Output is correct
18 Correct 510 ms 57768 KB Output is correct
19 Correct 265 ms 50236 KB Output is correct
20 Correct 279 ms 51680 KB Output is correct
21 Correct 265 ms 51020 KB Output is correct
22 Correct 247 ms 51512 KB Output is correct
23 Correct 248 ms 51476 KB Output is correct
24 Correct 244 ms 50108 KB Output is correct
25 Correct 245 ms 51124 KB Output is correct
26 Correct 245 ms 49864 KB Output is correct
27 Correct 9 ms 15572 KB Output is correct
28 Correct 10 ms 15700 KB Output is correct
29 Correct 11 ms 15580 KB Output is correct
30 Correct 11 ms 15632 KB Output is correct
31 Correct 11 ms 15588 KB Output is correct
32 Correct 10 ms 15700 KB Output is correct
33 Correct 9 ms 15700 KB Output is correct
34 Correct 10 ms 15712 KB Output is correct
35 Correct 10 ms 15600 KB Output is correct
36 Correct 22 ms 19936 KB Output is correct
37 Correct 234 ms 57312 KB Output is correct
38 Correct 237 ms 54396 KB Output is correct
39 Correct 225 ms 51536 KB Output is correct
40 Correct 245 ms 49728 KB Output is correct
41 Correct 214 ms 48588 KB Output is correct
42 Correct 202 ms 45772 KB Output is correct
43 Correct 241 ms 56344 KB Output is correct
44 Correct 228 ms 57292 KB Output is correct
45 Correct 173 ms 53180 KB Output is correct
46 Correct 226 ms 49484 KB Output is correct
47 Correct 32 ms 20044 KB Output is correct
48 Correct 645 ms 60504 KB Output is correct
49 Correct 581 ms 57428 KB Output is correct
50 Correct 563 ms 56108 KB Output is correct
51 Correct 541 ms 54616 KB Output is correct
52 Correct 478 ms 51668 KB Output is correct
53 Correct 371 ms 43380 KB Output is correct
54 Correct 658 ms 60528 KB Output is correct
55 Correct 602 ms 61420 KB Output is correct
56 Correct 478 ms 58260 KB Output is correct
57 Correct 471 ms 54448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 15312 KB Output is correct
2 Correct 8 ms 15316 KB Output is correct
3 Correct 8 ms 15312 KB Output is correct
4 Correct 9 ms 15316 KB Output is correct
5 Correct 9 ms 15444 KB Output is correct
6 Correct 9 ms 15704 KB Output is correct
7 Correct 9 ms 15576 KB Output is correct
8 Correct 10 ms 15696 KB Output is correct
9 Correct 9 ms 15704 KB Output is correct
10 Correct 225 ms 48704 KB Output is correct
11 Correct 207 ms 57280 KB Output is correct
12 Correct 208 ms 55736 KB Output is correct
13 Correct 252 ms 58596 KB Output is correct
14 Correct 166 ms 55244 KB Output is correct
15 Correct 214 ms 48204 KB Output is correct
16 Correct 541 ms 61220 KB Output is correct
17 Correct 566 ms 58376 KB Output is correct
18 Correct 544 ms 64380 KB Output is correct
19 Correct 510 ms 57768 KB Output is correct
20 Correct 265 ms 50236 KB Output is correct
21 Correct 279 ms 51680 KB Output is correct
22 Correct 265 ms 51020 KB Output is correct
23 Correct 247 ms 51512 KB Output is correct
24 Correct 248 ms 51476 KB Output is correct
25 Correct 244 ms 50108 KB Output is correct
26 Correct 245 ms 51124 KB Output is correct
27 Correct 245 ms 49864 KB Output is correct
28 Correct 9 ms 15572 KB Output is correct
29 Correct 10 ms 15700 KB Output is correct
30 Correct 11 ms 15580 KB Output is correct
31 Correct 11 ms 15632 KB Output is correct
32 Correct 11 ms 15588 KB Output is correct
33 Correct 10 ms 15700 KB Output is correct
34 Correct 9 ms 15700 KB Output is correct
35 Correct 10 ms 15712 KB Output is correct
36 Correct 10 ms 15600 KB Output is correct
37 Correct 22 ms 19936 KB Output is correct
38 Correct 234 ms 57312 KB Output is correct
39 Correct 237 ms 54396 KB Output is correct
40 Correct 225 ms 51536 KB Output is correct
41 Correct 245 ms 49728 KB Output is correct
42 Correct 214 ms 48588 KB Output is correct
43 Correct 202 ms 45772 KB Output is correct
44 Correct 241 ms 56344 KB Output is correct
45 Correct 228 ms 57292 KB Output is correct
46 Correct 173 ms 53180 KB Output is correct
47 Correct 226 ms 49484 KB Output is correct
48 Correct 32 ms 20044 KB Output is correct
49 Correct 645 ms 60504 KB Output is correct
50 Correct 581 ms 57428 KB Output is correct
51 Correct 563 ms 56108 KB Output is correct
52 Correct 541 ms 54616 KB Output is correct
53 Correct 478 ms 51668 KB Output is correct
54 Correct 371 ms 43380 KB Output is correct
55 Correct 658 ms 60528 KB Output is correct
56 Correct 602 ms 61420 KB Output is correct
57 Correct 478 ms 58260 KB Output is correct
58 Correct 471 ms 54448 KB Output is correct
59 Correct 115 ms 27356 KB Output is correct
60 Correct 575 ms 61392 KB Output is correct
61 Correct 613 ms 59652 KB Output is correct
62 Correct 595 ms 63440 KB Output is correct
63 Correct 522 ms 58468 KB Output is correct
64 Correct 9 ms 15572 KB Output is correct
65 Correct 9 ms 15732 KB Output is correct
66 Correct 9 ms 15584 KB Output is correct
67 Correct 10 ms 15460 KB Output is correct
68 Correct 10 ms 15584 KB Output is correct
69 Correct 9 ms 15620 KB Output is correct
70 Correct 10 ms 15700 KB Output is correct
71 Correct 10 ms 15700 KB Output is correct
72 Correct 9 ms 15660 KB Output is correct
73 Correct 9 ms 15712 KB Output is correct
74 Correct 167 ms 44412 KB Output is correct
75 Correct 223 ms 53408 KB Output is correct
76 Correct 247 ms 48736 KB Output is correct
77 Correct 180 ms 47364 KB Output is correct
78 Correct 72 ms 22660 KB Output is correct
79 Correct 62 ms 22980 KB Output is correct
80 Correct 142 ms 39396 KB Output is correct
81 Correct 272 ms 56400 KB Output is correct
82 Correct 265 ms 52872 KB Output is correct
83 Correct 276 ms 60996 KB Output is correct
84 Correct 211 ms 54356 KB Output is correct
85 Correct 267 ms 51572 KB Output is correct
86 Correct 93 ms 27444 KB Output is correct
87 Correct 625 ms 56892 KB Output is correct
88 Correct 636 ms 57000 KB Output is correct
89 Correct 363 ms 49732 KB Output is correct
90 Correct 178 ms 26728 KB Output is correct
91 Correct 181 ms 29268 KB Output is correct
92 Correct 362 ms 43908 KB Output is correct
93 Correct 634 ms 59884 KB Output is correct
94 Correct 441 ms 56796 KB Output is correct
95 Correct 639 ms 64956 KB Output is correct
96 Correct 523 ms 56160 KB Output is correct
97 Correct 433 ms 54436 KB Output is correct