답안 #319947

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
319947 2020-11-06T21:55:05 Z Blagojce 자매 도시 (APIO20_swap) C++11
30 / 100
619 ms 48216 KB
#include <bits/stdc++.h> 
#define fr(i, n, m) for(int i = (n); i < (m); i ++)
#define rfr(i, n, m) for(int i = (n); i >= (m); i --)
#define pb push_back
#define st first
#define nd second
#define pq priority_queue
#define all(x) begin(x), end(x)
#include <time.h>
#include <cmath>
#include <deque>

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const int i_inf = 1e9+1;
const ll inf = 2e18;
const ll mod = 998244353;
const ld eps = 1e-13;
const ld pi  = 3.14159265359;
const int mxn = 1e5+5;
mt19937 _rand(time(NULL));

//#include "swap.h"

vector<pii> g[mxn];
int n, m;
int sparse[mxn][20];
int mxw[mxn][20];

int depth[mxn];

int _c[mxn];
int dp[mxn];
void dfs(int u, int p, int val, int val2){
	sparse[u][0] = p;
	fr(i, 1, 20) sparse[u][i] = sparse[sparse[u][i-1]][i-1];
	
	mxw[u][0] = val2;
	fr(i, 1, 20) mxw[u][i] = max(mxw[u][i-1], mxw[sparse[u][i-1]][i-1]);
	
	int mi1 = i_inf, c1 = -1;
	int mi2 = i_inf;
	
	for(auto e : g[u]){
		if(e.st == p) continue;
		if(e.nd < mi1){
			mi2 = mi1;
			mi1 = e.nd;
			c1 = e.st;
		}
		else if(e.nd < mi2){
			mi2 = e.nd;
		}
	}
	dp[u] = _c[u];
	for(auto e : g[u]){
		if(e.st == p) continue;
		depth[e.st] = depth[u] + 1;
		if(e.st != c1){
			dfs(e.st, u, mi1, e.nd);
		}
		else{
			dfs(e.st, u, mi2, e.nd);
		}
		dp[u] = min(dp[u], max(dp[e.st], e.nd));
	}
}
int dpup[mxn];
void dfs2(int u, int p, int carry){
	carry = min(carry, _c[u]);
	dpup[u] = carry;
	int id = p;
	int min1 = carry;
	int min2 = i_inf;
	for(auto e : g[u]){
		if(e.st == p) continue;
		int cand1 = max(dp[e.st], e.nd);
		if(cand1 < min1){
			min2 = min1;
			id = e.st;
			min1 = cand1;
		}else if(cand1 < min2){
			min2 = cand1;
		}
	}
	for(auto e : g[u]){
		if(e.st == p) continue;
		if(e.st == id){
			dfs2(e.st, u, max(e.nd, min2));
		}
		else{
			dfs2(e.st, u, max(e.nd, min1));
		}
	}
	

}

int lca(int a, int b){
	int mind = min(depth[a], depth[b]);
	for(int i = 19; i >= 0; i --){
		if(depth[a] - (1<<i) >= mind) a = sparse[a][i];
		if(depth[b] - (1<<i) >= mind) b = sparse[b][i];
	}
	if(a == b) return a;
	
	for(int i = 19; i >= 0; i --){
		if(sparse[a][i] != sparse[b][i]){
			a = sparse[a][i];
			b = sparse[b][i];
		}
	}
	return sparse[a][0];
}
int cost(int u, int p){
	int ret = 0;
	for(int i = 19; i >= 0; i --){
		if(depth[u] - (1<<i) >= depth[p]){
			ret = max(ret, mxw[u][i]);
			u = sparse[u][i];
		}
	}
	return ret;
}

int link[mxn];
int sz[mxn];
int findx(int x){
	if(x == link[x]) return x;
	link[x] = findx(link[x]);
	return link[x];
}
void unite(int a, int b, int wi){
	int u = a, v = b;
	
	a = findx(a);
	b = findx(b);
	if(a == b) return;
	if(sz[a] < sz[b]) swap(a, b);
	link[b] = a;
	sz[a] += sz[b];
	g[u].pb({v, wi});
	g[v].pb({u, wi});
	
}
vector<int> G[mxn];
void init(int N, int M, vector<int> U, vector<int> V, vector<int> W){
	n = N, m = M;
	vector<pair<int, pii> > S;
	fr(i, 0, m){
		S.pb({W[i], {U[i], V[i]}});
	}
	sort(all(S));
	fr(i, 0, n){
		link[i] = i;
		sz[i] = 1;
	}
	for(auto edge : S){
		int u = edge.nd.st;
		int v = edge.nd.nd;
		unite(u, v, edge.st);
	}
	fr(i, 0, m){
		G[U[i]].pb(W[i]);
		G[V[i]].pb(W[i]);
	}
	
	fr(i, 0, n){
		sort(all(G[i]));
	}
	fr(i, 0, n){
		_c[i] = i_inf;
		if((int)G[i].size() >= 3) _c[i] = G[i][2];
	}
	
	dfs(0, 0, i_inf, i_inf);
	dfs2(0, 0, i_inf);
	
	
}
int line = 0;
int getMinimumFuelCapacity(int X, int Y){
	int k = lca(X, Y);
	
	if(X == k){
		int c = max(min(dp[k], dpup[k]), cost(Y, k));
		if(c == i_inf) c = -1;
		return c;
	}
	else if(Y == k){
		int c = max(min(dp[k], dpup[k]), cost(X, k));
		if(c == i_inf) c = -1;
		return c;
		
	}
	else{
		int c = max(min(dp[k], dpup[k]), max(cost(X, k), cost(Y, k)));
		if(c == i_inf) c = -1;
		return c;
	}
}/*
int main(){
	freopen("inp.txt", "r",stdin);
	//freopen("out.txt", "w", stdout);
	cin >> n >> m;
	vector<int> U(m);
	vector<int> V(m);
	vector<int> W(m);

	fr(i, 0, m){
		cin >> U[i] >> V[i] >> W[i];
	}
	
	init(n, m, U, V, W);
	int cc;
	cin >> cc;
	while(cc--){
		int i, j;
		cin >> i >> j;
		++line;
		int ans = getMinimumFuelCapacity(i, j);
		if(line == 1200) cout<<ans<<endl;
	}
	
	


}*/

# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 4 ms 5228 KB Output is correct
5 Correct 5 ms 5356 KB Output is correct
6 Correct 5 ms 5356 KB Output is correct
7 Correct 5 ms 5356 KB Output is correct
8 Correct 5 ms 5484 KB Output is correct
9 Correct 190 ms 33116 KB Output is correct
10 Correct 230 ms 40924 KB Output is correct
11 Correct 230 ms 39756 KB Output is correct
12 Correct 260 ms 42092 KB Output is correct
13 Correct 231 ms 44120 KB Output is correct
14 Correct 226 ms 32856 KB Output is correct
15 Correct 591 ms 44628 KB Output is correct
16 Correct 612 ms 41688 KB Output is correct
17 Correct 577 ms 48216 KB Output is correct
18 Correct 543 ms 46296 KB Output is correct
19 Incorrect 135 ms 15080 KB Output isn't correct
20 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 282 ms 38176 KB Output is correct
4 Correct 297 ms 39604 KB Output is correct
5 Correct 307 ms 39044 KB Output is correct
6 Correct 285 ms 39340 KB Output is correct
7 Correct 302 ms 39512 KB Output is correct
8 Correct 290 ms 38072 KB Output is correct
9 Correct 296 ms 38972 KB Output is correct
10 Correct 303 ms 37968 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 5100 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 5100 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 4 ms 5228 KB Output is correct
5 Correct 5 ms 5356 KB Output is correct
6 Correct 5 ms 5356 KB Output is correct
7 Correct 5 ms 5356 KB Output is correct
8 Correct 5 ms 5484 KB Output is correct
9 Correct 190 ms 33116 KB Output is correct
10 Correct 230 ms 40924 KB Output is correct
11 Correct 230 ms 39756 KB Output is correct
12 Correct 260 ms 42092 KB Output is correct
13 Correct 231 ms 44120 KB Output is correct
14 Correct 226 ms 32856 KB Output is correct
15 Correct 591 ms 44628 KB Output is correct
16 Correct 612 ms 41688 KB Output is correct
17 Correct 577 ms 48216 KB Output is correct
18 Correct 543 ms 46296 KB Output is correct
19 Correct 282 ms 38176 KB Output is correct
20 Correct 297 ms 39604 KB Output is correct
21 Correct 307 ms 39044 KB Output is correct
22 Correct 285 ms 39340 KB Output is correct
23 Correct 302 ms 39512 KB Output is correct
24 Correct 290 ms 38072 KB Output is correct
25 Correct 296 ms 38972 KB Output is correct
26 Correct 303 ms 37968 KB Output is correct
27 Correct 5 ms 5356 KB Output is correct
28 Correct 5 ms 5356 KB Output is correct
29 Correct 5 ms 5356 KB Output is correct
30 Correct 5 ms 5356 KB Output is correct
31 Correct 6 ms 5356 KB Output is correct
32 Correct 5 ms 5356 KB Output is correct
33 Correct 5 ms 5356 KB Output is correct
34 Correct 5 ms 5484 KB Output is correct
35 Correct 5 ms 5484 KB Output is correct
36 Correct 20 ms 9252 KB Output is correct
37 Correct 240 ms 41304 KB Output is correct
38 Correct 258 ms 38104 KB Output is correct
39 Correct 232 ms 35800 KB Output is correct
40 Correct 209 ms 34904 KB Output is correct
41 Correct 212 ms 34264 KB Output is correct
42 Correct 186 ms 31960 KB Output is correct
43 Correct 275 ms 39276 KB Output is correct
44 Correct 245 ms 40408 KB Output is correct
45 Correct 223 ms 41816 KB Output is correct
46 Correct 201 ms 35544 KB Output is correct
47 Correct 33 ms 9236 KB Output is correct
48 Correct 616 ms 42200 KB Output is correct
49 Correct 587 ms 40152 KB Output is correct
50 Correct 541 ms 38872 KB Output is correct
51 Correct 524 ms 38264 KB Output is correct
52 Correct 492 ms 36152 KB Output is correct
53 Correct 328 ms 29888 KB Output is correct
54 Correct 619 ms 41636 KB Output is correct
55 Correct 601 ms 42712 KB Output is correct
56 Correct 545 ms 45656 KB Output is correct
57 Correct 444 ms 38692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 5100 KB Output isn't correct