Submission #232851

# Submission time Handle Problem Language Result Execution time Memory
232851 2020-05-18T11:20:09 Z oolimry Designated Cities (JOI19_designated_cities) C++14
7 / 100
305 ms 35576 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(2,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 5120 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 305 ms 29844 KB Output is correct
3 Correct 268 ms 34424 KB Output is correct
4 Correct 284 ms 28400 KB Output is correct
5 Correct 297 ms 30760 KB Output is correct
6 Correct 297 ms 31216 KB Output is correct
7 Correct 250 ms 31524 KB Output is correct
8 Correct 270 ms 35576 KB Output is correct
9 Correct 202 ms 34456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4992 KB Output is correct
2 Incorrect 301 ms 29808 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 5120 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 305 ms 29844 KB Output is correct
3 Correct 268 ms 34424 KB Output is correct
4 Correct 284 ms 28400 KB Output is correct
5 Correct 297 ms 30760 KB Output is correct
6 Correct 297 ms 31216 KB Output is correct
7 Correct 250 ms 31524 KB Output is correct
8 Correct 270 ms 35576 KB Output is correct
9 Correct 202 ms 34456 KB Output is correct
10 Correct 7 ms 4992 KB Output is correct
11 Incorrect 301 ms 29808 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 5120 KB Output isn't correct
3 Halted 0 ms 0 KB -