Submission #112271

# Submission time Handle Problem Language Result Execution time Memory
112271 2019-05-18T08:36:39 Z gs14004 Designated Cities (JOI19_designated_cities) C++17
100 / 100
1774 ms 31008 KB
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200005;
using pi = pair<int, int>;
using lint = long long;

int n, vis[MAXN];
lint ans[MAXN];
vector<pi> gph[MAXN];
lint dpDown[MAXN], dpUp[MAXN], pae[MAXN];

void dfs3(int x, int p){
	for(auto &i : gph[x]){
		if(i.second != p){
			dfs3(i.second, x);
			dpDown[x] += dpDown[i.second] + i.first;
		}
		else{
			pae[x] = i.first;
		}
	}
}

void dfs4(int x, int p){
	for(auto &i : gph[x]){
		if(i.second != p){
			dpUp[i.second] = dpUp[x] + pae[i.second] + dpDown[x] - dpDown[i.second] - i.first;
			dfs4(i.second, x);
		}
	}
}

lint dfs2(int x, int p, vector<lint> &v){
	lint curmax = 0;
	for(auto &i : gph[x]){
		if(i.second != p && !vis[i.second]){
			lint nxt = dfs2(i.second, x, v) + i.first;
			if(curmax < nxt) swap(curmax, nxt);
			if(nxt) v.push_back(nxt);
		}
	}
	return curmax;
}

void solve(int r){
	lint cg = dpDown[r] + dpUp[r];
	vector<pair<lint, int>> tot;
	for(auto &i : gph[r]){
		if(!vis[i.second]){
			vector<lint> v;
			lint topv = dfs2(i.second, r, v);
			v.push_back(topv + i.first);
			for(auto &j : v) tot.emplace_back(j, i.second);
		}
	}
	sort(tot.rbegin(), tot.rend());
	{
		// case 1. includes root in selected vtx
		lint cursum = 0;
		for(int i=0; i<=tot.size(); i++){
			ans[i + 1] = min(ans[i + 1], cg - cursum);
			if(i < tot.size()) cursum += tot[i].first;
		}
	}
	{
		// case 2. two different vertices
		int hitpoint = tot.size();
		for(int i=0; i<tot.size(); i++){
			if(tot[i].second != tot[0].second){
				hitpoint = i;
				break;
			}
		}
		if(hitpoint == tot.size()) return;
		lint cursum = tot[hitpoint].first;
		for(int i=1; i<=hitpoint; i++){
			if(i > 1) ans[i] = min(ans[i], cg - cursum);
			cursum += tot[i-1].first;
		}
		for(int i=hitpoint + 1; i<=tot.size(); i++){
			ans[i] = min(ans[i], cg - cursum);
			if(i < tot.size()) cursum += tot[i].first;
		}
	}
}

vector<int> dfn;
int sz[MAXN], msz[MAXN];

void dfs(int x, int p){
	sz[x] = 1; msz[x] = 0;
	for(auto &i : gph[x]){
		if(!vis[i.second] && i.second != p){
			dfs(i.second, x);
			sz[x] += sz[i.second];
			msz[x] = max(msz[x], sz[i.second]);
		}
	}
	dfn.push_back(x);
}

int get_center(int x){
	dfs(x, -1);
	pi ret(1e9, 1e9);
	for(auto &i : dfn){
		int mx = max(msz[i], (int)dfn.size() - sz[i]);
		ret = min(ret, pi(mx, i));
	}
	dfn.clear();
	return ret.second;
}

int main(){
	scanf("%d",&n);
	for(int i=1; i<n; i++){
		int s, e, x, y; scanf("%d %d %d %d",&s,&e,&x,&y);
		gph[s].emplace_back(x, e);
		gph[e].emplace_back(y, s);
	}
	dfs3(1, -1);
	dfs4(1, -1);
	memset(ans, 0x3f, sizeof(ans));
	queue<int> que;
	que.push(1);
	while(!que.empty()){
		int x = que.front();
		que.pop();
		x = get_center(x);
		solve(x);
		vis[x] = 1;
		for(auto &i : gph[x]){
			if(!vis[i.second]){
				que.push(i.second);
			}
		}
	}
	int q; scanf("%d",&q);
	for(int i=2; i<=n; i++) ans[i] = min(ans[i-1], ans[i]);
	while(q--){
		int x; scanf("%d",&x); printf("%lld\n", ans[x]);
	}
}

Compilation message

designated_cities.cpp: In function 'void solve(int)':
designated_cities.cpp:60:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i=0; i<=tot.size(); i++){
                ~^~~~~~~~~~~~
designated_cities.cpp:62:9: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    if(i < tot.size()) cursum += tot[i].first;
       ~~^~~~~~~~~~~~
designated_cities.cpp:68:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i=0; i<tot.size(); i++){
                ~^~~~~~~~~~~
designated_cities.cpp:74:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if(hitpoint == tot.size()) return;
      ~~~~~~~~~^~~~~~~~~~~~~
designated_cities.cpp:80:28: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i=hitpoint + 1; i<=tot.size(); i++){
                           ~^~~~~~~~~~~~
designated_cities.cpp:82:9: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    if(i < tot.size()) cursum += tot[i].first;
       ~~^~~~~~~~~~~~
designated_cities.cpp: In function 'int main()':
designated_cities.cpp:114:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&n);
  ~~~~~^~~~~~~~~
designated_cities.cpp:116:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int s, e, x, y; scanf("%d %d %d %d",&s,&e,&x,&y);
                   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
designated_cities.cpp:137:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  int q; scanf("%d",&q);
         ~~~~~^~~~~~~~~
designated_cities.cpp:140:15: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int x; scanf("%d",&x); printf("%lld\n", ans[x]);
          ~~~~~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 6656 KB Output is correct
2 Correct 7 ms 6656 KB Output is correct
3 Correct 7 ms 6656 KB Output is correct
4 Correct 6 ms 6656 KB Output is correct
5 Correct 6 ms 6656 KB Output is correct
6 Correct 7 ms 6656 KB Output is correct
7 Correct 7 ms 6656 KB Output is correct
8 Correct 7 ms 6656 KB Output is correct
9 Correct 7 ms 6656 KB Output is correct
10 Correct 7 ms 6656 KB Output is correct
11 Correct 7 ms 6656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 6656 KB Output is correct
2 Correct 1059 ms 24672 KB Output is correct
3 Correct 1571 ms 29904 KB Output is correct
4 Correct 1102 ms 24848 KB Output is correct
5 Correct 552 ms 24144 KB Output is correct
6 Correct 1349 ms 25784 KB Output is correct
7 Correct 411 ms 24168 KB Output is correct
8 Correct 1574 ms 30064 KB Output is correct
9 Correct 265 ms 26980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 6656 KB Output is correct
2 Correct 1163 ms 24800 KB Output is correct
3 Correct 1741 ms 30880 KB Output is correct
4 Correct 1145 ms 24688 KB Output is correct
5 Correct 528 ms 24048 KB Output is correct
6 Correct 1351 ms 26024 KB Output is correct
7 Correct 301 ms 28516 KB Output is correct
8 Correct 1563 ms 28780 KB Output is correct
9 Correct 272 ms 26980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 6656 KB Output is correct
2 Correct 7 ms 6656 KB Output is correct
3 Correct 7 ms 6656 KB Output is correct
4 Correct 6 ms 6656 KB Output is correct
5 Correct 6 ms 6656 KB Output is correct
6 Correct 7 ms 6656 KB Output is correct
7 Correct 7 ms 6656 KB Output is correct
8 Correct 7 ms 6656 KB Output is correct
9 Correct 7 ms 6656 KB Output is correct
10 Correct 7 ms 6656 KB Output is correct
11 Correct 7 ms 6656 KB Output is correct
12 Correct 7 ms 6656 KB Output is correct
13 Correct 10 ms 6912 KB Output is correct
14 Correct 10 ms 6912 KB Output is correct
15 Correct 10 ms 6784 KB Output is correct
16 Correct 28 ms 6904 KB Output is correct
17 Correct 10 ms 6912 KB Output is correct
18 Correct 10 ms 6924 KB Output is correct
19 Correct 11 ms 6912 KB Output is correct
20 Correct 10 ms 6912 KB Output is correct
21 Correct 11 ms 6912 KB Output is correct
22 Correct 10 ms 6912 KB Output is correct
23 Correct 10 ms 6912 KB Output is correct
24 Correct 10 ms 6912 KB Output is correct
25 Correct 10 ms 6912 KB Output is correct
26 Correct 10 ms 6912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 6656 KB Output is correct
2 Correct 1059 ms 24672 KB Output is correct
3 Correct 1571 ms 29904 KB Output is correct
4 Correct 1102 ms 24848 KB Output is correct
5 Correct 552 ms 24144 KB Output is correct
6 Correct 1349 ms 25784 KB Output is correct
7 Correct 411 ms 24168 KB Output is correct
8 Correct 1574 ms 30064 KB Output is correct
9 Correct 265 ms 26980 KB Output is correct
10 Correct 6 ms 6656 KB Output is correct
11 Correct 1163 ms 24800 KB Output is correct
12 Correct 1741 ms 30880 KB Output is correct
13 Correct 1145 ms 24688 KB Output is correct
14 Correct 528 ms 24048 KB Output is correct
15 Correct 1351 ms 26024 KB Output is correct
16 Correct 301 ms 28516 KB Output is correct
17 Correct 1563 ms 28780 KB Output is correct
18 Correct 272 ms 26980 KB Output is correct
19 Correct 7 ms 6656 KB Output is correct
20 Correct 1119 ms 25532 KB Output is correct
21 Correct 1615 ms 31008 KB Output is correct
22 Correct 1119 ms 24756 KB Output is correct
23 Correct 1163 ms 24856 KB Output is correct
24 Correct 1108 ms 24948 KB Output is correct
25 Correct 1078 ms 24892 KB Output is correct
26 Correct 1155 ms 24764 KB Output is correct
27 Correct 568 ms 23916 KB Output is correct
28 Correct 1388 ms 25840 KB Output is correct
29 Correct 1179 ms 24452 KB Output is correct
30 Correct 1012 ms 25676 KB Output is correct
31 Correct 423 ms 26384 KB Output is correct
32 Correct 1581 ms 29020 KB Output is correct
33 Correct 291 ms 26936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 6656 KB Output is correct
2 Correct 7 ms 6656 KB Output is correct
3 Correct 7 ms 6656 KB Output is correct
4 Correct 6 ms 6656 KB Output is correct
5 Correct 6 ms 6656 KB Output is correct
6 Correct 7 ms 6656 KB Output is correct
7 Correct 7 ms 6656 KB Output is correct
8 Correct 7 ms 6656 KB Output is correct
9 Correct 7 ms 6656 KB Output is correct
10 Correct 7 ms 6656 KB Output is correct
11 Correct 7 ms 6656 KB Output is correct
12 Correct 6 ms 6656 KB Output is correct
13 Correct 1059 ms 24672 KB Output is correct
14 Correct 1571 ms 29904 KB Output is correct
15 Correct 1102 ms 24848 KB Output is correct
16 Correct 552 ms 24144 KB Output is correct
17 Correct 1349 ms 25784 KB Output is correct
18 Correct 411 ms 24168 KB Output is correct
19 Correct 1574 ms 30064 KB Output is correct
20 Correct 265 ms 26980 KB Output is correct
21 Correct 6 ms 6656 KB Output is correct
22 Correct 1163 ms 24800 KB Output is correct
23 Correct 1741 ms 30880 KB Output is correct
24 Correct 1145 ms 24688 KB Output is correct
25 Correct 528 ms 24048 KB Output is correct
26 Correct 1351 ms 26024 KB Output is correct
27 Correct 301 ms 28516 KB Output is correct
28 Correct 1563 ms 28780 KB Output is correct
29 Correct 272 ms 26980 KB Output is correct
30 Correct 7 ms 6656 KB Output is correct
31 Correct 10 ms 6912 KB Output is correct
32 Correct 10 ms 6912 KB Output is correct
33 Correct 10 ms 6784 KB Output is correct
34 Correct 28 ms 6904 KB Output is correct
35 Correct 10 ms 6912 KB Output is correct
36 Correct 10 ms 6924 KB Output is correct
37 Correct 11 ms 6912 KB Output is correct
38 Correct 10 ms 6912 KB Output is correct
39 Correct 11 ms 6912 KB Output is correct
40 Correct 10 ms 6912 KB Output is correct
41 Correct 10 ms 6912 KB Output is correct
42 Correct 10 ms 6912 KB Output is correct
43 Correct 10 ms 6912 KB Output is correct
44 Correct 10 ms 6912 KB Output is correct
45 Correct 7 ms 6656 KB Output is correct
46 Correct 1119 ms 25532 KB Output is correct
47 Correct 1615 ms 31008 KB Output is correct
48 Correct 1119 ms 24756 KB Output is correct
49 Correct 1163 ms 24856 KB Output is correct
50 Correct 1108 ms 24948 KB Output is correct
51 Correct 1078 ms 24892 KB Output is correct
52 Correct 1155 ms 24764 KB Output is correct
53 Correct 568 ms 23916 KB Output is correct
54 Correct 1388 ms 25840 KB Output is correct
55 Correct 1179 ms 24452 KB Output is correct
56 Correct 1012 ms 25676 KB Output is correct
57 Correct 423 ms 26384 KB Output is correct
58 Correct 1581 ms 29020 KB Output is correct
59 Correct 291 ms 26936 KB Output is correct
60 Correct 7 ms 6656 KB Output is correct
61 Correct 1071 ms 25204 KB Output is correct
62 Correct 1774 ms 30172 KB Output is correct
63 Correct 1204 ms 26188 KB Output is correct
64 Correct 1121 ms 25856 KB Output is correct
65 Correct 1113 ms 25788 KB Output is correct
66 Correct 1156 ms 25824 KB Output is correct
67 Correct 1118 ms 25760 KB Output is correct
68 Correct 628 ms 24420 KB Output is correct
69 Correct 1473 ms 27208 KB Output is correct
70 Correct 1207 ms 25944 KB Output is correct
71 Correct 1010 ms 26084 KB Output is correct
72 Correct 429 ms 26252 KB Output is correct
73 Correct 1662 ms 29672 KB Output is correct
74 Correct 359 ms 26980 KB Output is correct