Submission #426895

# Submission time Handle Problem Language Result Execution time Memory
426895 2021-06-14T10:46:06 Z nots0fast Swapping Cities (APIO20_swap) C++17
13 / 100
524 ms 39388 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){
			when[aid1] = min(when[aid1], w);
		}
		if(deg[b] > 2){
			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;
		}
		
		when[aid1] = min(when[aid1], when[aid2]);
		for(auto hd : dsu[aid2]){
			aid[hd] = aid1;
			dsu[aid1].push_back(hd);
			nodeBelongs[hd].pb(mp(aid1, w));
		}
		dsu[aid2].clear();
		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){
		if(seen.find(mp(el[0], el[1])) == seen.end()){
			dsu::join(el[0], el[1], el[2]);
			seen.insert(mp(el[0], el[1]));
		}
	}
}


int getMinimumFuelCapacity(int X, int Y) {
	ll x = X, y = Y;
	if( (dsu::aid[x] != dsu::aid[y]) || (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 5000 KB Output is correct
3 Correct 4 ms 4940 KB Output is correct
4 Correct 5 ms 5068 KB Output is correct
5 Correct 5 ms 5196 KB Output is correct
6 Correct 8 ms 5200 KB Output is correct
7 Correct 5 ms 5196 KB Output is correct
8 Correct 5 ms 5196 KB Output is correct
9 Correct 308 ms 30804 KB Output is correct
10 Correct 399 ms 36272 KB Output is correct
11 Correct 407 ms 35580 KB Output is correct
12 Correct 461 ms 37448 KB Output is correct
13 Correct 272 ms 29704 KB Output is correct
14 Correct 350 ms 30896 KB Output is correct
15 Correct 485 ms 38280 KB Output is correct
16 Correct 475 ms 37276 KB Output is correct
17 Correct 470 ms 39388 KB Output is correct
18 Correct 356 ms 31204 KB Output is correct
19 Correct 111 ms 13204 KB Output is correct
20 Correct 515 ms 38484 KB Output is correct
21 Correct 485 ms 37056 KB Output is correct
22 Correct 524 ms 39268 KB Output is correct
23 Correct 395 ms 31220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 4 ms 5000 KB Output is correct
3 Correct 331 ms 27396 KB Output is correct
4 Correct 353 ms 28568 KB Output is correct
5 Correct 342 ms 27764 KB Output is correct
6 Correct 334 ms 28372 KB Output is correct
7 Correct 349 ms 28148 KB Output is correct
8 Correct 318 ms 27248 KB Output is correct
9 Correct 331 ms 28020 KB Output is correct
10 Correct 337 ms 27136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 4 ms 5000 KB Output is correct
3 Correct 4 ms 4940 KB Output is correct
4 Correct 5 ms 5068 KB Output is correct
5 Correct 5 ms 5196 KB Output is correct
6 Correct 8 ms 5200 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 5 ms 5196 KB Output is correct
11 Correct 6 ms 5196 KB Output is correct
12 Correct 5 ms 5196 KB Output is correct
13 Correct 5 ms 5196 KB Output is correct
14 Correct 5 ms 5196 KB Output is correct
15 Correct 5 ms 5272 KB Output is correct
16 Correct 5 ms 5196 KB Output is correct
17 Correct 5 ms 5196 KB Output is correct
18 Correct 5 ms 5196 KB Output is correct
19 Correct 5 ms 5244 KB Output is correct
20 Correct 6 ms 5268 KB Output is correct
21 Correct 5 ms 5196 KB Output is correct
22 Correct 5 ms 5196 KB Output is correct
23 Correct 5 ms 5196 KB Output is correct
24 Incorrect 6 ms 5324 KB Output isn't correct
25 Halted 0 ms 0 KB -
# 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 5000 KB Output is correct
4 Correct 4 ms 4940 KB Output is correct
5 Correct 5 ms 5068 KB Output is correct
6 Correct 5 ms 5196 KB Output is correct
7 Correct 8 ms 5200 KB Output is correct
8 Correct 5 ms 5196 KB Output is correct
9 Correct 5 ms 5196 KB Output is correct
10 Correct 308 ms 30804 KB Output is correct
11 Correct 399 ms 36272 KB Output is correct
12 Correct 407 ms 35580 KB Output is correct
13 Correct 461 ms 37448 KB Output is correct
14 Correct 272 ms 29704 KB Output is correct
15 Correct 5 ms 5196 KB Output is correct
16 Correct 6 ms 5196 KB Output is correct
17 Correct 5 ms 5196 KB Output is correct
18 Correct 5 ms 5196 KB Output is correct
19 Correct 5 ms 5196 KB Output is correct
20 Correct 5 ms 5272 KB Output is correct
21 Correct 5 ms 5196 KB Output is correct
22 Correct 5 ms 5196 KB Output is correct
23 Correct 5 ms 5196 KB Output is correct
24 Correct 5 ms 5244 KB Output is correct
25 Correct 6 ms 5268 KB Output is correct
26 Correct 5 ms 5196 KB Output is correct
27 Correct 5 ms 5196 KB Output is correct
28 Correct 5 ms 5196 KB Output is correct
29 Incorrect 6 ms 5324 KB Output isn't correct
30 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 4 ms 5000 KB Output is correct
3 Correct 4 ms 4940 KB Output is correct
4 Correct 5 ms 5068 KB Output is correct
5 Correct 5 ms 5196 KB Output is correct
6 Correct 8 ms 5200 KB Output is correct
7 Correct 5 ms 5196 KB Output is correct
8 Correct 5 ms 5196 KB Output is correct
9 Correct 308 ms 30804 KB Output is correct
10 Correct 399 ms 36272 KB Output is correct
11 Correct 407 ms 35580 KB Output is correct
12 Correct 461 ms 37448 KB Output is correct
13 Correct 272 ms 29704 KB Output is correct
14 Correct 350 ms 30896 KB Output is correct
15 Correct 485 ms 38280 KB Output is correct
16 Correct 475 ms 37276 KB Output is correct
17 Correct 470 ms 39388 KB Output is correct
18 Correct 356 ms 31204 KB Output is correct
19 Correct 331 ms 27396 KB Output is correct
20 Correct 353 ms 28568 KB Output is correct
21 Correct 342 ms 27764 KB Output is correct
22 Correct 334 ms 28372 KB Output is correct
23 Correct 349 ms 28148 KB Output is correct
24 Correct 318 ms 27248 KB Output is correct
25 Correct 331 ms 28020 KB Output is correct
26 Correct 337 ms 27136 KB Output is correct
27 Correct 5 ms 5196 KB Output is correct
28 Correct 6 ms 5196 KB Output is correct
29 Correct 5 ms 5196 KB Output is correct
30 Correct 5 ms 5196 KB Output is correct
31 Correct 5 ms 5196 KB Output is correct
32 Correct 5 ms 5272 KB Output is correct
33 Correct 5 ms 5196 KB Output is correct
34 Correct 5 ms 5196 KB Output is correct
35 Correct 5 ms 5196 KB Output is correct
36 Correct 28 ms 9168 KB Output is correct
37 Correct 397 ms 37356 KB Output is correct
38 Correct 413 ms 37148 KB Output is correct
39 Correct 395 ms 36612 KB Output is correct
40 Correct 378 ms 35584 KB Output is correct
41 Correct 380 ms 35048 KB Output is correct
42 Correct 360 ms 32116 KB Output is correct
43 Incorrect 401 ms 37792 KB Output isn't correct
44 Halted 0 ms 0 KB -
# 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 5000 KB Output is correct
4 Correct 4 ms 4940 KB Output is correct
5 Correct 5 ms 5068 KB Output is correct
6 Correct 5 ms 5196 KB Output is correct
7 Correct 8 ms 5200 KB Output is correct
8 Correct 5 ms 5196 KB Output is correct
9 Correct 5 ms 5196 KB Output is correct
10 Correct 308 ms 30804 KB Output is correct
11 Correct 399 ms 36272 KB Output is correct
12 Correct 407 ms 35580 KB Output is correct
13 Correct 461 ms 37448 KB Output is correct
14 Correct 272 ms 29704 KB Output is correct
15 Correct 350 ms 30896 KB Output is correct
16 Correct 485 ms 38280 KB Output is correct
17 Correct 475 ms 37276 KB Output is correct
18 Correct 470 ms 39388 KB Output is correct
19 Correct 356 ms 31204 KB Output is correct
20 Correct 331 ms 27396 KB Output is correct
21 Correct 353 ms 28568 KB Output is correct
22 Correct 342 ms 27764 KB Output is correct
23 Correct 334 ms 28372 KB Output is correct
24 Correct 349 ms 28148 KB Output is correct
25 Correct 318 ms 27248 KB Output is correct
26 Correct 331 ms 28020 KB Output is correct
27 Correct 337 ms 27136 KB Output is correct
28 Correct 5 ms 5196 KB Output is correct
29 Correct 6 ms 5196 KB Output is correct
30 Correct 5 ms 5196 KB Output is correct
31 Correct 5 ms 5196 KB Output is correct
32 Correct 5 ms 5196 KB Output is correct
33 Correct 5 ms 5272 KB Output is correct
34 Correct 5 ms 5196 KB Output is correct
35 Correct 5 ms 5196 KB Output is correct
36 Correct 5 ms 5196 KB Output is correct
37 Correct 28 ms 9168 KB Output is correct
38 Correct 397 ms 37356 KB Output is correct
39 Correct 413 ms 37148 KB Output is correct
40 Correct 395 ms 36612 KB Output is correct
41 Correct 378 ms 35584 KB Output is correct
42 Correct 380 ms 35048 KB Output is correct
43 Correct 360 ms 32116 KB Output is correct
44 Incorrect 401 ms 37792 KB Output isn't correct
45 Halted 0 ms 0 KB -