Submission #110555

# Submission time Handle Problem Language Result Execution time Memory
110555 2019-05-11T07:01:55 Z AngusRitossa Designated Cities (JOI19_designated_cities) C++14
100 / 100
1854 ms 94968 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
vector<pair<int, pair<int, int> > > adj[200010];
int n;
int dgoingup[200010], dgoingdown[200010], done[200010];
int goingup(int a)
{
	if (dgoingup[a]) return dgoingup[a]-1;
	dgoingup[a] = 1;
	for (auto b : adj[a])
	{
		if (!dgoingup[b.first]) dgoingup[a] += goingup(b.first), dgoingup[a] += b.second.second;
	}
	return dgoingup[a]-1;
}
int goingdown(int a)
{
	if (dgoingdown[a]) return dgoingdown[a]-1;
	dgoingdown[a] = 1;
	for (auto b : adj[a])
	{
		if (!dgoingdown[b.first]) dgoingdown[a] += goingdown(b.first), dgoingdown[a] += b.second.first;
	}
	return dgoingdown[a]-1;
}
int oneans = 1e15;
void findoneans(int a, int above)
{
	done[a] = 1;
	int am = 0;
	for (auto b : adj[a])
	{
		if (done[b.first]) continue;
		am += goingup(b.first)+b.second.second;
	}
	oneans = min(oneans, am+above);
	for (auto b : adj[a])
	{
		if (done[b.first]) continue;
		findoneans(b.first, above+am-goingup(b.first)-b.second.second + b.second.first);
	}
}
int jump[200010][20], root, seen[200010], cost[200010], hei[200010], cost2[200010];
int roots[200010], revroots[200010], upto;
void dfs(int a, int am, int am2)
{
	seen[a] = 1;
	cost[a] = am;
	cost2[a] = am2;
	//printf("%d, %d\n", a, cost[a]);
	for (auto b : adj[a])
	{
		if (seen[b.first]) continue;
		jump[b.first][0] = a;
		hei[b.first] = hei[a]+1;
		dfs(b.first, am+b.second.second, am2+b.second.first);
	}
	if (adj[a].size() == 1)
	{
		roots[a] = upto;
		revroots[upto++] = a;
	}
}
int ans[200010];
int lca(int a, int b)
{
	for (int i = 19; i >= 0; i--)
	{
		if (hei[jump[a][i]] >= hei[b]) a = jump[a][i];
		if (hei[jump[b][i]] >= hei[a]) b = jump[b][i];
	}
	if (a == b) return a;
	for (int i = 19; i >= 0; i--)
	{
		if (jump[a][i] != jump[b][i]) a = jump[a][i], b = jump[b][i];
	}
	assert(a != b && jump[a][0] == jump[b][0]);
	return jump[a][0];
}
set<int> in;
int amof[200010];
priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > pq;
void calc(set<int>::iterator it)
{
	auto nex = next(it);
	int l;
	int extra = 0;
	int pre = -1;
	if (it != in.begin()) pre = *prev(it);
	if (nex == in.end())
	{
		int l1 = lca(revroots[*it], revroots[pre]);
		int l2 = lca(revroots[*in.begin()], revroots[pre]);
		if (hei[l1] < hei[l2]) extra = cost2[l2]-cost2[l1];
		l = l1;
	}
	else if (pre == -1)
	{
		int l2 = lca(revroots[*it], revroots[*nex]);
		int l1 = lca(revroots[*nex], revroots[*in.rbegin()]);
		if (hei[l1] > hei[l2]) extra = cost2[l1]-cost2[l2];
		l = l2;
	}
	else
	{
		int l1 = lca(revroots[*it], revroots[pre]);
		int l2 = lca(revroots[*it], revroots[*nex]);
		if (hei[l1] > hei[l2]) l = l1;
		else l = l2;
	}
	int am = cost[revroots[*it]] - cost[l] + extra;
	amof[*it] = am;
	pq.emplace(am, *it);
}
#undef int
int main()
{
	#define int long long
	scanf("%lld", &n);
	for (int i = 1; i < n; i++)
	{
		int a, b, c, d;
		scanf("%lld%lld%lld%lld", &a, &b, &c, &d);
		a--;
		b--;
		adj[a].push_back({ b, { d, c }});
		adj[b].push_back({ a, { c, d }});
	}
	goingup(0), goingdown(0), findoneans(0, 0);
	ans[1] = oneans;
	for (int i = 0; i < n; i++)
	{
		if (adj[i].size() != 1) 
		{
			root = i;
			break;
		}
	}
	hei[root] = 1;
	dfs(root, 0, 0);
	jump[root][0] = root;
	for (int j = 1; j < 20; j++)
	{
		for (int i = 0; i < n; i++)
		{
			jump[i][j] = jump[jump[i][j-1]][j-1];
		}
	}
	for (int i = 0; i < upto; i++) in.insert(i);
	for (int i = upto-1; i > 1; i--)
	{
		// want to remove something from in
		if (i == upto-1)
		{
			for (auto it = in.begin(); it != in.end(); ++it)
			{
				calc(it);
			}
		}
		while (pq.size() && amof[pq.top().second] != pq.top().first) pq.pop();
		pair<int, int> mn = pq.top();
		assert(mn.second != -1);
	//	printf("%lld (%lld) %lld\n\n", mn.second, revroots[mn.second], mn.first);
		in.erase(mn.second);
		auto it = in.lower_bound(mn.second);
		if (it != in.end())
		{
			calc(it);
		}
		if (it != in.begin())
		{
			--it;
			calc(it);
		}
		calc(in.begin());
		calc(prev(in.end()));
		amof[mn.second] = -1;
		ans[i] = ans[i+1]+mn.first;
	}
	int q;
	scanf("%lld", &q);
	for (int i = 0; i < q; i++)
	{
		int a;
		scanf("%lld", &a);
		printf("%lld\n", ans[a]);
	}
}

Compilation message

designated_cities.cpp: In function 'int main()':
designated_cities.cpp:120:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%lld", &n);
  ~~~~~^~~~~~~~~~~~
designated_cities.cpp:124:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld%lld%lld%lld", &a, &b, &c, &d);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
designated_cities.cpp:182:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%lld", &q);
  ~~~~~^~~~~~~~~~~~
designated_cities.cpp:186:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld", &a);
   ~~~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 6 ms 5120 KB Output is correct
3 Correct 7 ms 5120 KB Output is correct
4 Correct 6 ms 5120 KB Output is correct
5 Correct 6 ms 5120 KB Output is correct
6 Correct 6 ms 5120 KB Output is correct
7 Correct 6 ms 5120 KB Output is correct
8 Correct 6 ms 5120 KB Output is correct
9 Correct 7 ms 5120 KB Output is correct
10 Correct 8 ms 5120 KB Output is correct
11 Correct 6 ms 5120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5120 KB Output is correct
2 Correct 1387 ms 71504 KB Output is correct
3 Correct 757 ms 73288 KB Output is correct
4 Correct 1097 ms 71760 KB Output is correct
5 Correct 1399 ms 72864 KB Output is correct
6 Correct 1489 ms 73688 KB Output is correct
7 Correct 1532 ms 79448 KB Output is correct
8 Correct 855 ms 76748 KB Output is correct
9 Correct 1854 ms 85028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5120 KB Output is correct
2 Correct 1464 ms 71532 KB Output is correct
3 Correct 748 ms 75432 KB Output is correct
4 Correct 1071 ms 71936 KB Output is correct
5 Correct 1395 ms 72864 KB Output is correct
6 Correct 1626 ms 73844 KB Output is correct
7 Correct 1803 ms 83960 KB Output is correct
8 Correct 1339 ms 76696 KB Output is correct
9 Correct 1789 ms 85796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 6 ms 5120 KB Output is correct
3 Correct 7 ms 5120 KB Output is correct
4 Correct 6 ms 5120 KB Output is correct
5 Correct 6 ms 5120 KB Output is correct
6 Correct 6 ms 5120 KB Output is correct
7 Correct 6 ms 5120 KB Output is correct
8 Correct 6 ms 5120 KB Output is correct
9 Correct 7 ms 5120 KB Output is correct
10 Correct 8 ms 5120 KB Output is correct
11 Correct 6 ms 5120 KB Output is correct
12 Correct 7 ms 5120 KB Output is correct
13 Correct 13 ms 5760 KB Output is correct
14 Correct 11 ms 5760 KB Output is correct
15 Correct 13 ms 5888 KB Output is correct
16 Correct 13 ms 5760 KB Output is correct
17 Correct 12 ms 5760 KB Output is correct
18 Correct 13 ms 5760 KB Output is correct
19 Correct 12 ms 6016 KB Output is correct
20 Correct 12 ms 5888 KB Output is correct
21 Correct 11 ms 5888 KB Output is correct
22 Correct 12 ms 5760 KB Output is correct
23 Correct 18 ms 5888 KB Output is correct
24 Correct 15 ms 6016 KB Output is correct
25 Correct 10 ms 5888 KB Output is correct
26 Correct 15 ms 5888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5120 KB Output is correct
2 Correct 1387 ms 71504 KB Output is correct
3 Correct 757 ms 73288 KB Output is correct
4 Correct 1097 ms 71760 KB Output is correct
5 Correct 1399 ms 72864 KB Output is correct
6 Correct 1489 ms 73688 KB Output is correct
7 Correct 1532 ms 79448 KB Output is correct
8 Correct 855 ms 76748 KB Output is correct
9 Correct 1854 ms 85028 KB Output is correct
10 Correct 6 ms 5120 KB Output is correct
11 Correct 1464 ms 71532 KB Output is correct
12 Correct 748 ms 75432 KB Output is correct
13 Correct 1071 ms 71936 KB Output is correct
14 Correct 1395 ms 72864 KB Output is correct
15 Correct 1626 ms 73844 KB Output is correct
16 Correct 1803 ms 83960 KB Output is correct
17 Correct 1339 ms 76696 KB Output is correct
18 Correct 1789 ms 85796 KB Output is correct
19 Correct 7 ms 5120 KB Output is correct
20 Correct 1480 ms 71592 KB Output is correct
21 Correct 692 ms 75512 KB Output is correct
22 Correct 1067 ms 71784 KB Output is correct
23 Correct 1307 ms 71988 KB Output is correct
24 Correct 1308 ms 77112 KB Output is correct
25 Correct 1374 ms 78216 KB Output is correct
26 Correct 1316 ms 77032 KB Output is correct
27 Correct 1292 ms 78852 KB Output is correct
28 Correct 1491 ms 79884 KB Output is correct
29 Correct 1226 ms 77796 KB Output is correct
30 Correct 1395 ms 78752 KB Output is correct
31 Correct 1642 ms 86556 KB Output is correct
32 Correct 1219 ms 83172 KB Output is correct
33 Correct 1714 ms 92228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 6 ms 5120 KB Output is correct
3 Correct 7 ms 5120 KB Output is correct
4 Correct 6 ms 5120 KB Output is correct
5 Correct 6 ms 5120 KB Output is correct
6 Correct 6 ms 5120 KB Output is correct
7 Correct 6 ms 5120 KB Output is correct
8 Correct 6 ms 5120 KB Output is correct
9 Correct 7 ms 5120 KB Output is correct
10 Correct 8 ms 5120 KB Output is correct
11 Correct 6 ms 5120 KB Output is correct
12 Correct 6 ms 5120 KB Output is correct
13 Correct 1387 ms 71504 KB Output is correct
14 Correct 757 ms 73288 KB Output is correct
15 Correct 1097 ms 71760 KB Output is correct
16 Correct 1399 ms 72864 KB Output is correct
17 Correct 1489 ms 73688 KB Output is correct
18 Correct 1532 ms 79448 KB Output is correct
19 Correct 855 ms 76748 KB Output is correct
20 Correct 1854 ms 85028 KB Output is correct
21 Correct 6 ms 5120 KB Output is correct
22 Correct 1464 ms 71532 KB Output is correct
23 Correct 748 ms 75432 KB Output is correct
24 Correct 1071 ms 71936 KB Output is correct
25 Correct 1395 ms 72864 KB Output is correct
26 Correct 1626 ms 73844 KB Output is correct
27 Correct 1803 ms 83960 KB Output is correct
28 Correct 1339 ms 76696 KB Output is correct
29 Correct 1789 ms 85796 KB Output is correct
30 Correct 7 ms 5120 KB Output is correct
31 Correct 13 ms 5760 KB Output is correct
32 Correct 11 ms 5760 KB Output is correct
33 Correct 13 ms 5888 KB Output is correct
34 Correct 13 ms 5760 KB Output is correct
35 Correct 12 ms 5760 KB Output is correct
36 Correct 13 ms 5760 KB Output is correct
37 Correct 12 ms 6016 KB Output is correct
38 Correct 12 ms 5888 KB Output is correct
39 Correct 11 ms 5888 KB Output is correct
40 Correct 12 ms 5760 KB Output is correct
41 Correct 18 ms 5888 KB Output is correct
42 Correct 15 ms 6016 KB Output is correct
43 Correct 10 ms 5888 KB Output is correct
44 Correct 15 ms 5888 KB Output is correct
45 Correct 7 ms 5120 KB Output is correct
46 Correct 1480 ms 71592 KB Output is correct
47 Correct 692 ms 75512 KB Output is correct
48 Correct 1067 ms 71784 KB Output is correct
49 Correct 1307 ms 71988 KB Output is correct
50 Correct 1308 ms 77112 KB Output is correct
51 Correct 1374 ms 78216 KB Output is correct
52 Correct 1316 ms 77032 KB Output is correct
53 Correct 1292 ms 78852 KB Output is correct
54 Correct 1491 ms 79884 KB Output is correct
55 Correct 1226 ms 77796 KB Output is correct
56 Correct 1395 ms 78752 KB Output is correct
57 Correct 1642 ms 86556 KB Output is correct
58 Correct 1219 ms 83172 KB Output is correct
59 Correct 1714 ms 92228 KB Output is correct
60 Correct 8 ms 5120 KB Output is correct
61 Correct 1399 ms 79284 KB Output is correct
62 Correct 730 ms 81608 KB Output is correct
63 Correct 1032 ms 78260 KB Output is correct
64 Correct 1315 ms 79804 KB Output is correct
65 Correct 1256 ms 78436 KB Output is correct
66 Correct 1358 ms 79768 KB Output is correct
67 Correct 1350 ms 78592 KB Output is correct
68 Correct 1536 ms 80848 KB Output is correct
69 Correct 1397 ms 81824 KB Output is correct
70 Correct 1241 ms 78556 KB Output is correct
71 Correct 1404 ms 80668 KB Output is correct
72 Correct 1615 ms 85660 KB Output is correct
73 Correct 1391 ms 84504 KB Output is correct
74 Correct 1771 ms 94968 KB Output is correct