Submission #232852

# Submission time Handle Problem Language Result Execution time Memory
232852 2020-05-18T11:21:22 Z oolimry Designated Cities (JOI19_designated_cities) C++14
7 / 100
311 ms 29688 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int,int> ii;

struct edge { int to, cost, rev; };
vector<edge> adj[200005];
int degree[200005];
int flipDist[200005];
int total = 0;

bool gone[200005];
int ans[200005];

void dfs(int u, int p){
	for(edge e : adj[u]){
		if(e.to == p) continue;
		total += e.cost;
		flipDist[e.to] = flipDist[u] - e.cost + e.rev;
		dfs(e.to, u);
	}
}

ii findParent(int u){
	for(edge e : adj[u]){
		if(!gone[u]) return ii(e.rev, e.to);
	}
}

signed main(){
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	
	int n; cin >> n;
	for(int i = 1;i < n;i++){
		int a, b, c, d; cin >> a >> b >> c >> d;
		adj[a].push_back({b,c,d});
		adj[b].push_back({a,d,c});
		degree[a]++; degree[b]++;
	}
	
	///solve the E = 1 case
	dfs(1,0);
	ans[1] = total + *min_element(flipDist + 1, flipDist + n+1);
	
	///Other cases
	priority_queue<ii, vector<ii>, greater<ii> > leaves;
	for(int i = 1;i <= n;i++){
		if(degree[i] == 1) leaves.push(findParent(i));
	}
	
	while(leaves.size() > 2){
		int u = leaves.top().second; int cost = leaves.top().first;
		leaves.pop();
		
		int p = findParent(u).second;
		degree[p]--;
		
		if(degree[p] == 1){
			leaves.push(ii(findParent(p).first + cost, p));
		}
		else{
			ans[leaves.size()] = ans[leaves.size()+1] + cost;
		}	
	}
	
	int Q; cin >> Q;
	while(Q--){
		int x; cin >> x; cout << ans[x] << "\n";
	}
}

Compilation message

designated_cities.cpp: In function 'ii findParent(long long int)':
designated_cities.cpp:28:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4992 KB Output is correct
2 Incorrect 7 ms 4992 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4992 KB Output is correct
2 Correct 311 ms 23408 KB Output is correct
3 Correct 273 ms 28920 KB Output is correct
4 Correct 279 ms 23408 KB Output is correct
5 Correct 280 ms 24224 KB Output is correct
6 Correct 290 ms 24816 KB Output is correct
7 Correct 260 ms 25120 KB Output is correct
8 Correct 265 ms 29688 KB Output is correct
9 Correct 211 ms 27928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4992 KB Output is correct
2 Incorrect 295 ms 23408 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4992 KB Output is correct
2 Incorrect 7 ms 4992 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4992 KB Output is correct
2 Correct 311 ms 23408 KB Output is correct
3 Correct 273 ms 28920 KB Output is correct
4 Correct 279 ms 23408 KB Output is correct
5 Correct 280 ms 24224 KB Output is correct
6 Correct 290 ms 24816 KB Output is correct
7 Correct 260 ms 25120 KB Output is correct
8 Correct 265 ms 29688 KB Output is correct
9 Correct 211 ms 27928 KB Output is correct
10 Correct 7 ms 4992 KB Output is correct
11 Incorrect 295 ms 23408 KB Output isn't correct
12 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4992 KB Output is correct
2 Incorrect 7 ms 4992 KB Output isn't correct
3 Halted 0 ms 0 KB -