Submission #405897

# Submission time Handle Problem Language Result Execution time Memory
405897 2021-05-17T03:14:47 Z amunduzbaev Swapping Cities (APIO20_swap) C++14
0 / 100
227 ms 27384 KB
#include "swap.h"
#include "bits/stdc++.h"
using namespace std;
#define pb push_back
#define ff first
#define ss second
#define sz(x) (int)x.size()
#define int long long

const int NN = 1e5+5;
int sub, used[NN], found, pos[NN], is[NN];
vector<int> pref[NN];
vector<pair<int, int>> edges[NN];
vector<pair<int, int>> ss;

void dfs(int u, int p = -1){
	if(used[u]){ found = 1; return; } 
	used[u] = 1;
	//cout<<u<<" ";
	for(auto x : edges[u]) { 
		if(x.ff == p) continue;
		ss.pb({u, x.ss});
		dfs(x.ff, u);
	}
}

void init(int32_t N, int32_t M,
	vector<int32_t> U, vector<int32_t> V, vector<int32_t> W) {
	//cout<<"\n";
	for(int i=0;i<M;i++){
		edges[U[i]].pb({V[i], W[i]});
		edges[V[i]].pb({U[i], W[i]});
	}
	int last = 0;
	for(int i=0;i<N;i++){
		if(used[i]) continue;
		ss.clear(), found = 0;
		dfs(i);
		if(found){
			pref[last].pb(0);
			ss.pop_back();
			for(int j=0;j<sz(ss);j++){
				is[ss[j].ff] = last+1;
				pos[ss[j].ff] = j+1;
				pref[last].pb(ss[j].ss);
			} for(int j=1;j<=sz(ss);j++) pref[last][j] += pref[last][j-1];
			last++;
		}
	} 
}

/*

5 5
1 0 1
2 1 2
3 2 3
4 3 4
4 0 5
5
0 1
1 2
2 3
3 4
4 0

*/

int32_t getMinimumFuelCapacity(int32_t x, int32_t y) {
	if(x > y) swap(x, y);
	if(is[x] != is[y] || !is[x]) return -1;
	int l = is[x] - 1;
	int i = pos[x], j = pos[y];
	if(i > j) swap(i, j);
	return max(pref[l][j-1] - pref[l][i-1], pref[l][sz(pref[l])-1] - (pref[l][j-1] - pref[l][i-1]));
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 5 ms 4940 KB Output is correct
3 Correct 4 ms 5000 KB Output is correct
4 Correct 4 ms 5060 KB Output is correct
5 Correct 4 ms 5068 KB Output is correct
6 Correct 4 ms 5068 KB Output is correct
7 Correct 4 ms 5112 KB Output is correct
8 Correct 4 ms 5196 KB Output is correct
9 Correct 85 ms 18960 KB Output is correct
10 Correct 114 ms 22964 KB Output is correct
11 Correct 115 ms 21976 KB Output is correct
12 Correct 97 ms 23312 KB Output is correct
13 Correct 99 ms 25740 KB Output is correct
14 Correct 93 ms 18108 KB Output is correct
15 Correct 172 ms 24316 KB Output is correct
16 Correct 227 ms 22096 KB Output is correct
17 Correct 156 ms 27384 KB Output is correct
18 Correct 175 ms 25468 KB Output is correct
19 Incorrect 81 ms 12356 KB Output isn't correct
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 5 ms 4940 KB Output is correct
3 Incorrect 116 ms 16968 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 5 ms 4940 KB Output is correct
3 Correct 4 ms 5000 KB Output is correct
4 Correct 4 ms 5060 KB Output is correct
5 Correct 4 ms 5068 KB Output is correct
6 Correct 4 ms 5068 KB Output is correct
7 Correct 4 ms 5112 KB Output is correct
8 Correct 4 ms 5196 KB Output is correct
9 Incorrect 4 ms 4940 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 4940 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 5 ms 4940 KB Output is correct
3 Correct 4 ms 5000 KB Output is correct
4 Correct 4 ms 5060 KB Output is correct
5 Correct 4 ms 5068 KB Output is correct
6 Correct 4 ms 5068 KB Output is correct
7 Correct 4 ms 5112 KB Output is correct
8 Correct 4 ms 5196 KB Output is correct
9 Correct 85 ms 18960 KB Output is correct
10 Correct 114 ms 22964 KB Output is correct
11 Correct 115 ms 21976 KB Output is correct
12 Correct 97 ms 23312 KB Output is correct
13 Correct 99 ms 25740 KB Output is correct
14 Correct 93 ms 18108 KB Output is correct
15 Correct 172 ms 24316 KB Output is correct
16 Correct 227 ms 22096 KB Output is correct
17 Correct 156 ms 27384 KB Output is correct
18 Correct 175 ms 25468 KB Output is correct
19 Incorrect 116 ms 16968 KB Output isn't correct
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 4940 KB Output isn't correct