Submission #426946

# Submission time Handle Problem Language Result Execution time Memory
426946 2021-06-14T11:05:26 Z nots0fast Swapping Cities (APIO20_swap) C++17
100 / 100
560 ms 34736 KB
#include <bits/stdc++.h>
#include "swap.h"
using namespace std;
#define mp(a,b) make_pair(a,b)
#define ff first
#define setp(a) setprecision(a)<<fixed
#define ss second
#define fori(v) for(ll i=0; i<v; i++)
#define forj(v) for(ll j=0; j<v; j++)
#define fork(v) for(ll k=0; k<v; k++)
#define forl(v) for(ll l=0; l<v; l++)
#define fort(v) for(ll t=0; t<v; t++)
#define forz(v) for(ll z=0; z<v; z++)
#define forx(v) for(ll x=0; x<v; x++)
#define fory(v) for(ll y=0; y<v; y++)
#define ll long long
#define ld long double
#define pb(a) push_back(a)
const ll INF = 0x3f3f3f3f;
const ll inf = 1*pow(10,9) + 100;
ll modulo = pow(10,9) + 7;
#define MAX (int)(pow(10,5) + 10)


namespace dsu{
	int aid[MAX];
	vector<int> dsu[MAX];
	vector<pair<int,int> > nodeBelongs[MAX];	// nodes history of components
	int when[MAX];					// when did a component become 2 tracable
	int deg[MAX];					// degree of nodes
	
	
	void init(int n){
		fori(n){
			aid[i] = i, dsu[i].clear(), dsu[i].push_back(i), 
			when[i] = inf, deg[i] = 0, nodeBelongs[i].pb(mp(i, 0));
		}
	}
	
	bool join(int a,int b, int w){  //	1 - if joined, 0 - if they were already joined 
		deg[a]++;
		deg[b]++;
		
		int aid1 = aid[a], aid2 = aid[b];
		
		if(deg[a] > 2 || deg[b] > 2){
			when[aid1] = min(when[aid1], w);
			when[aid2] = min(when[aid2], w);
		}
		
		if(dsu[aid2].size() > dsu[aid1].size())
			swap(aid1,aid2);
		
		if(aid1==aid2){
			when[aid1] = min(when[aid1], w);
			return 0;
		}
		
		if(when[aid2]!=inf && when[aid1] == inf){
			when[aid1] = w;
		}
		
		for(auto hd : dsu[aid2]){
			aid[hd] = aid1;
			dsu[aid1].push_back(hd);
			nodeBelongs[hd].pb(mp(aid1, w));
		}
		vector<int>().swap(dsu[aid2]);
		return 1;
	}
};


bool srt(vector<ll>& a, vector<ll>& b){
	return a[2] < b[2];
}



void init(int N, int M,	vector<int> U, vector<int> V, vector<int> W) {
	ll n = N, m = M;
	vector<vector<ll> > edges;
	fori(m){
		ll a = U[i], b = V[i], w = W[i];
		if(a > b){
			swap(a, b);
		}
		edges.pb(vector<ll>({a,b,w}));
	}
	sort(edges.begin(), edges.end(), srt);
	
	set<pair<ll,ll> > seen;
	
	dsu::init(n);
	
	for(auto& el : edges){
		dsu::join(el[0], el[1], el[2]);
	}
}


int getMinimumFuelCapacity(int X, int Y) {
	ll x = X, y = Y;
	if(dsu::when[dsu::aid[x]] == inf ){
		return -1;
	}
	
	ll cmp1 = x, cmp2 = y;
	ll pt1 = 0, pt2 = 0;
	
	int ans = 0;
	while( (cmp1 != cmp2) || (dsu::when[cmp1] == inf) ){
		if(pt1 == (ll)dsu::nodeBelongs[x].size() ){
			cmp2 = dsu::nodeBelongs[y][pt2].ff;
			ans = max(ans, dsu::nodeBelongs[y][pt2].ss);
			++pt2;
		}
		else if(pt2 == (ll)dsu::nodeBelongs[y].size()){
			cmp1 =  dsu::nodeBelongs[x][pt1].ff;
			ans = max(ans, dsu::nodeBelongs[x][pt1].ss);
			++pt1;
		}
		else if(dsu::nodeBelongs[x][pt1].ss <= dsu::nodeBelongs[y][pt2].ss){
			cmp1 =  dsu::nodeBelongs[x][pt1].ff;
			ans = max(ans, dsu::nodeBelongs[x][pt1].ss);
			++pt1;
		}
		else{
			cmp2 = dsu::nodeBelongs[y][pt2].ff;
			ans = max(ans, dsu::nodeBelongs[y][pt2].ss);
			++pt2;
		}
	}
	
	ans = max(ans, dsu::when[cmp1]);
	
	return ans;
}

/*

void deal(){
	int n, m;
	cin>>n>>m;
	vector<int> u(m), v(m), w(m);
	fori(m){
		cin>>u[i];
	}
	fori(m){
		cin>>v[i];
	}
	fori(m){
		cin>>w[i];
	}
	
	init(n, m, u, v, w);
	ll q;
	cin>>q;
	forl(q){
		ll ui, vi;
		cin>>ui>>vi;
		cout<<getMinimumFuelCapacity(ui, vi)<<'\n';
	}
}



int main(){
	cin.tie(0);
	ios_base::sync_with_stdio(0);
	deal();
}
*/
















# Verdict Execution time Memory Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 4 ms 5004 KB Output is correct
4 Correct 4 ms 5068 KB Output is correct
5 Correct 5 ms 5196 KB Output is correct
6 Correct 5 ms 5196 KB Output is correct
7 Correct 5 ms 5196 KB Output is correct
8 Correct 5 ms 5196 KB Output is correct
9 Correct 200 ms 21392 KB Output is correct
10 Correct 276 ms 24436 KB Output is correct
11 Correct 258 ms 24240 KB Output is correct
12 Correct 326 ms 25200 KB Output is correct
13 Correct 192 ms 22632 KB Output is correct
14 Correct 226 ms 21352 KB Output is correct
15 Correct 331 ms 26496 KB Output is correct
16 Correct 373 ms 25840 KB Output is correct
17 Correct 343 ms 26992 KB Output is correct
18 Correct 284 ms 24236 KB Output is correct
19 Correct 95 ms 11896 KB Output is correct
20 Correct 395 ms 26328 KB Output is correct
21 Correct 380 ms 25844 KB Output is correct
22 Correct 463 ms 27188 KB Output is correct
23 Correct 359 ms 24248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 239 ms 22620 KB Output is correct
4 Correct 262 ms 23404 KB Output is correct
5 Correct 270 ms 22980 KB Output is correct
6 Correct 252 ms 23360 KB Output is correct
7 Correct 264 ms 23156 KB Output is correct
8 Correct 243 ms 22568 KB Output is correct
9 Correct 299 ms 23200 KB Output is correct
10 Correct 242 ms 22432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 4 ms 5004 KB Output is correct
4 Correct 4 ms 5068 KB Output is correct
5 Correct 5 ms 5196 KB Output is correct
6 Correct 5 ms 5196 KB Output is correct
7 Correct 5 ms 5196 KB Output is correct
8 Correct 5 ms 5196 KB Output is correct
9 Correct 4 ms 4940 KB Output is correct
10 Correct 6 ms 5196 KB Output is correct
11 Correct 5 ms 5196 KB Output is correct
12 Correct 6 ms 5196 KB Output is correct
13 Correct 4 ms 5068 KB Output is correct
14 Correct 5 ms 5136 KB Output is correct
15 Correct 5 ms 5196 KB Output is correct
16 Correct 5 ms 5196 KB Output is correct
17 Correct 4 ms 5196 KB Output is correct
18 Correct 5 ms 5136 KB Output is correct
19 Correct 6 ms 5068 KB Output is correct
20 Correct 5 ms 5196 KB Output is correct
21 Correct 5 ms 5136 KB Output is correct
22 Correct 5 ms 5112 KB Output is correct
23 Correct 4 ms 5068 KB Output is correct
24 Correct 5 ms 5196 KB Output is correct
25 Correct 5 ms 5248 KB Output is correct
26 Correct 5 ms 5196 KB Output is correct
27 Correct 5 ms 5144 KB Output is correct
28 Correct 5 ms 5196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 4 ms 4940 KB Output is correct
4 Correct 4 ms 5004 KB Output is correct
5 Correct 4 ms 5068 KB Output is correct
6 Correct 5 ms 5196 KB Output is correct
7 Correct 5 ms 5196 KB Output is correct
8 Correct 5 ms 5196 KB Output is correct
9 Correct 5 ms 5196 KB Output is correct
10 Correct 200 ms 21392 KB Output is correct
11 Correct 276 ms 24436 KB Output is correct
12 Correct 258 ms 24240 KB Output is correct
13 Correct 326 ms 25200 KB Output is correct
14 Correct 192 ms 22632 KB Output is correct
15 Correct 6 ms 5196 KB Output is correct
16 Correct 5 ms 5196 KB Output is correct
17 Correct 6 ms 5196 KB Output is correct
18 Correct 4 ms 5068 KB Output is correct
19 Correct 5 ms 5136 KB Output is correct
20 Correct 5 ms 5196 KB Output is correct
21 Correct 5 ms 5196 KB Output is correct
22 Correct 4 ms 5196 KB Output is correct
23 Correct 5 ms 5136 KB Output is correct
24 Correct 6 ms 5068 KB Output is correct
25 Correct 5 ms 5196 KB Output is correct
26 Correct 5 ms 5136 KB Output is correct
27 Correct 5 ms 5112 KB Output is correct
28 Correct 4 ms 5068 KB Output is correct
29 Correct 5 ms 5196 KB Output is correct
30 Correct 5 ms 5248 KB Output is correct
31 Correct 5 ms 5196 KB Output is correct
32 Correct 5 ms 5144 KB Output is correct
33 Correct 5 ms 5196 KB Output is correct
34 Correct 22 ms 7620 KB Output is correct
35 Correct 303 ms 25244 KB Output is correct
36 Correct 275 ms 24900 KB Output is correct
37 Correct 310 ms 24592 KB Output is correct
38 Correct 266 ms 24120 KB Output is correct
39 Correct 267 ms 24068 KB Output is correct
40 Correct 220 ms 22156 KB Output is correct
41 Correct 291 ms 25632 KB Output is correct
42 Correct 272 ms 25096 KB Output is correct
43 Correct 176 ms 21844 KB Output is correct
44 Correct 247 ms 23516 KB Output is correct
45 Correct 208 ms 24184 KB Output is correct
46 Correct 271 ms 24560 KB Output is correct
47 Correct 233 ms 22604 KB Output is correct
48 Correct 210 ms 22888 KB Output is correct
49 Correct 115 ms 19384 KB Output is correct
50 Correct 93 ms 15600 KB Output is correct
51 Correct 175 ms 21064 KB Output is correct
52 Correct 326 ms 28888 KB Output is correct
53 Correct 316 ms 28220 KB Output is correct
54 Correct 379 ms 32844 KB Output is correct
55 Correct 173 ms 22080 KB Output is correct
56 Correct 282 ms 27780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 4 ms 5004 KB Output is correct
4 Correct 4 ms 5068 KB Output is correct
5 Correct 5 ms 5196 KB Output is correct
6 Correct 5 ms 5196 KB Output is correct
7 Correct 5 ms 5196 KB Output is correct
8 Correct 5 ms 5196 KB Output is correct
9 Correct 200 ms 21392 KB Output is correct
10 Correct 276 ms 24436 KB Output is correct
11 Correct 258 ms 24240 KB Output is correct
12 Correct 326 ms 25200 KB Output is correct
13 Correct 192 ms 22632 KB Output is correct
14 Correct 226 ms 21352 KB Output is correct
15 Correct 331 ms 26496 KB Output is correct
16 Correct 373 ms 25840 KB Output is correct
17 Correct 343 ms 26992 KB Output is correct
18 Correct 284 ms 24236 KB Output is correct
19 Correct 239 ms 22620 KB Output is correct
20 Correct 262 ms 23404 KB Output is correct
21 Correct 270 ms 22980 KB Output is correct
22 Correct 252 ms 23360 KB Output is correct
23 Correct 264 ms 23156 KB Output is correct
24 Correct 243 ms 22568 KB Output is correct
25 Correct 299 ms 23200 KB Output is correct
26 Correct 242 ms 22432 KB Output is correct
27 Correct 6 ms 5196 KB Output is correct
28 Correct 5 ms 5196 KB Output is correct
29 Correct 6 ms 5196 KB Output is correct
30 Correct 4 ms 5068 KB Output is correct
31 Correct 5 ms 5136 KB Output is correct
32 Correct 5 ms 5196 KB Output is correct
33 Correct 5 ms 5196 KB Output is correct
34 Correct 4 ms 5196 KB Output is correct
35 Correct 5 ms 5136 KB Output is correct
36 Correct 22 ms 7620 KB Output is correct
37 Correct 303 ms 25244 KB Output is correct
38 Correct 275 ms 24900 KB Output is correct
39 Correct 310 ms 24592 KB Output is correct
40 Correct 266 ms 24120 KB Output is correct
41 Correct 267 ms 24068 KB Output is correct
42 Correct 220 ms 22156 KB Output is correct
43 Correct 291 ms 25632 KB Output is correct
44 Correct 272 ms 25096 KB Output is correct
45 Correct 176 ms 21844 KB Output is correct
46 Correct 247 ms 23516 KB Output is correct
47 Correct 28 ms 7860 KB Output is correct
48 Correct 449 ms 26536 KB Output is correct
49 Correct 392 ms 26072 KB Output is correct
50 Correct 410 ms 26108 KB Output is correct
51 Correct 377 ms 25716 KB Output is correct
52 Correct 373 ms 24436 KB Output is correct
53 Correct 241 ms 18932 KB Output is correct
54 Correct 415 ms 26464 KB Output is correct
55 Correct 490 ms 26808 KB Output is correct
56 Correct 279 ms 23796 KB Output is correct
57 Correct 351 ms 24776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 4 ms 4940 KB Output is correct
4 Correct 4 ms 5004 KB Output is correct
5 Correct 4 ms 5068 KB Output is correct
6 Correct 5 ms 5196 KB Output is correct
7 Correct 5 ms 5196 KB Output is correct
8 Correct 5 ms 5196 KB Output is correct
9 Correct 5 ms 5196 KB Output is correct
10 Correct 200 ms 21392 KB Output is correct
11 Correct 276 ms 24436 KB Output is correct
12 Correct 258 ms 24240 KB Output is correct
13 Correct 326 ms 25200 KB Output is correct
14 Correct 192 ms 22632 KB Output is correct
15 Correct 226 ms 21352 KB Output is correct
16 Correct 331 ms 26496 KB Output is correct
17 Correct 373 ms 25840 KB Output is correct
18 Correct 343 ms 26992 KB Output is correct
19 Correct 284 ms 24236 KB Output is correct
20 Correct 239 ms 22620 KB Output is correct
21 Correct 262 ms 23404 KB Output is correct
22 Correct 270 ms 22980 KB Output is correct
23 Correct 252 ms 23360 KB Output is correct
24 Correct 264 ms 23156 KB Output is correct
25 Correct 243 ms 22568 KB Output is correct
26 Correct 299 ms 23200 KB Output is correct
27 Correct 242 ms 22432 KB Output is correct
28 Correct 6 ms 5196 KB Output is correct
29 Correct 5 ms 5196 KB Output is correct
30 Correct 6 ms 5196 KB Output is correct
31 Correct 4 ms 5068 KB Output is correct
32 Correct 5 ms 5136 KB Output is correct
33 Correct 5 ms 5196 KB Output is correct
34 Correct 5 ms 5196 KB Output is correct
35 Correct 4 ms 5196 KB Output is correct
36 Correct 5 ms 5136 KB Output is correct
37 Correct 22 ms 7620 KB Output is correct
38 Correct 303 ms 25244 KB Output is correct
39 Correct 275 ms 24900 KB Output is correct
40 Correct 310 ms 24592 KB Output is correct
41 Correct 266 ms 24120 KB Output is correct
42 Correct 267 ms 24068 KB Output is correct
43 Correct 220 ms 22156 KB Output is correct
44 Correct 291 ms 25632 KB Output is correct
45 Correct 272 ms 25096 KB Output is correct
46 Correct 176 ms 21844 KB Output is correct
47 Correct 247 ms 23516 KB Output is correct
48 Correct 28 ms 7860 KB Output is correct
49 Correct 449 ms 26536 KB Output is correct
50 Correct 392 ms 26072 KB Output is correct
51 Correct 410 ms 26108 KB Output is correct
52 Correct 377 ms 25716 KB Output is correct
53 Correct 373 ms 24436 KB Output is correct
54 Correct 241 ms 18932 KB Output is correct
55 Correct 415 ms 26464 KB Output is correct
56 Correct 490 ms 26808 KB Output is correct
57 Correct 279 ms 23796 KB Output is correct
58 Correct 351 ms 24776 KB Output is correct
59 Correct 95 ms 11896 KB Output is correct
60 Correct 395 ms 26328 KB Output is correct
61 Correct 380 ms 25844 KB Output is correct
62 Correct 463 ms 27188 KB Output is correct
63 Correct 359 ms 24248 KB Output is correct
64 Correct 6 ms 5068 KB Output is correct
65 Correct 5 ms 5196 KB Output is correct
66 Correct 5 ms 5136 KB Output is correct
67 Correct 5 ms 5112 KB Output is correct
68 Correct 4 ms 5068 KB Output is correct
69 Correct 5 ms 5196 KB Output is correct
70 Correct 5 ms 5248 KB Output is correct
71 Correct 5 ms 5196 KB Output is correct
72 Correct 5 ms 5144 KB Output is correct
73 Correct 5 ms 5196 KB Output is correct
74 Correct 208 ms 24184 KB Output is correct
75 Correct 271 ms 24560 KB Output is correct
76 Correct 233 ms 22604 KB Output is correct
77 Correct 210 ms 22888 KB Output is correct
78 Correct 115 ms 19384 KB Output is correct
79 Correct 93 ms 15600 KB Output is correct
80 Correct 175 ms 21064 KB Output is correct
81 Correct 326 ms 28888 KB Output is correct
82 Correct 316 ms 28220 KB Output is correct
83 Correct 379 ms 32844 KB Output is correct
84 Correct 173 ms 22080 KB Output is correct
85 Correct 282 ms 27780 KB Output is correct
86 Correct 81 ms 12532 KB Output is correct
87 Correct 372 ms 25696 KB Output is correct
88 Correct 429 ms 26000 KB Output is correct
89 Correct 323 ms 23408 KB Output is correct
90 Correct 211 ms 21236 KB Output is correct
91 Correct 216 ms 20792 KB Output is correct
92 Correct 257 ms 22260 KB Output is correct
93 Correct 428 ms 29724 KB Output is correct
94 Correct 403 ms 29672 KB Output is correct
95 Correct 560 ms 34736 KB Output is correct
96 Correct 301 ms 23712 KB Output is correct
97 Correct 367 ms 26372 KB Output is correct