답안 #102516

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
102516 2019-03-25T14:04:52 Z Bruteforceman Designated Cities (JOI19_designated_cities) C++11
100 / 100
819 ms 54380 KB
#include <bits/stdc++.h>
using namespace std;
int n;
int l[500010], r[500010], a[500010], b[500010];
vector <int> g[500010];
long long sum;

typedef pair <long long, int> pii;
long long ans[500010];
pii opt[500010];
long long cost[500010];
vector <int> ls;
int root;
int Px, Py;

const long long inf = 1e17;

void leaves(int x, int par) {
	bool leaf = true;
	for(auto i : g[x]) {
		int y = l[i] ^ r[i] ^ x;
		if(y - par) {
			leaves(y, x);
			leaf = true;
		}
	}
	if(leaf) {
		ls.push_back(x);
	}
}

bool cmp(int x, int y) {
	return cost[x] > cost[y];
}

void dfs(int x, int par, long long add) {
	ans[1] = max(ans[1], add);

	opt[x] = pii(0, x);
	vector <pii> can;
	for(auto i : g[x]) {
		int y = l[i] ^ r[i] ^ x;
		int val = (y == r[i]) ? a[i] : b[i];
		if(y == par) continue;
		sum += val;
		dfs(y, x, add + val - (a[i] ^ b[i] ^ val));
		pii nw = pii(opt[y].first + val, opt[y].second);
		opt[x] = max(opt[x], nw);
		can.push_back(nw);
	}
	sort(can.begin(), can.end(), greater <pii> ());
	if(can.size() < 2) return ;
	if(ans[2] <= can[0].first + can[1].first + add) {
		ans[2] = can[0].first + can[1].first + add;
		root = x;
		Px = can[0].second;
		Py = can[1].second;
	}
}

void solve(int x, int par, long long add) {
	opt[x] = pii(0, x);
	for(auto i : g[x]) {
		int y = l[i] ^ r[i] ^ x;
		int val = (y == r[i]) ? a[i] : b[i];
		if(y == par) continue;
		sum += val;
		solve(y, x, add + val - (a[i] ^ b[i] ^ val));
		pii nw = pii(opt[y].first + val, opt[y].second);
		opt[x] = max(opt[x], nw);
		cost[opt[y].second] += val;
	}
}



int main() {
    scanf("%d", &n);
    for(int i = 1; i < n; i++) {
        scanf("%d %d %d %d", &l[i], &r[i], &a[i], &b[i]);
        g[l[i]].emplace_back(i);
        g[r[i]].emplace_back(i);
    }
    root = 1;
    while(root < n && g[root].size() == 1) {
    	++root;
    }
    dfs(root, 0, 0);
    if(n == 2) {
    	ans[2] = sum;
    } 
    ans[1] = sum - ans[1];
    ans[2] = sum - ans[2];

    sum = 0;
    solve(root, 0, 0);
    leaves(root, 0);
    
    long long Pcx = cost[Px];
    long long Pcy = cost[Py];
    cost[Px] = inf;
    cost[Py] = inf;
    sort(ls.begin(), ls.end(), cmp);
    cost[Px] = Pcx;
    cost[Py] = Pcy;

    long long tot = 0;
    for(int i = 1; i <= ls.size(); i++) {
    	tot += cost[ls[i - 1]];
    	if(i > 2) {
    		ans[i] = max(ans[i], tot);
    	}
    } 
    for(int i = 3; i <= ls.size(); i++) {
    	ans[i] = sum - ans[i];
    }

    int q;
    scanf("%d", &q);

    while(q--) {
    	int x;
    	scanf("%d", &x);
    	printf("%lld\n", ans[x]);
    }
    return 0;
}

Compilation message

designated_cities.cpp: In function 'int main()':
designated_cities.cpp:108:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 1; i <= ls.size(); i++) {
                    ~~^~~~~~~~~~~~
designated_cities.cpp:114:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 3; i <= ls.size(); i++) {
                    ~~^~~~~~~~~~~~
designated_cities.cpp:78:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
     ~~~~~^~~~~~~~~~
designated_cities.cpp:80:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d %d %d", &l[i], &r[i], &a[i], &b[i]);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
designated_cities.cpp:119:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &q);
     ~~~~~^~~~~~~~~~
designated_cities.cpp:123:11: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
      scanf("%d", &x);
      ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 12160 KB Output is correct
2 Correct 12 ms 12160 KB Output is correct
3 Correct 12 ms 12032 KB Output is correct
4 Correct 16 ms 12160 KB Output is correct
5 Correct 13 ms 12160 KB Output is correct
6 Correct 14 ms 12160 KB Output is correct
7 Correct 15 ms 12160 KB Output is correct
8 Correct 15 ms 12160 KB Output is correct
9 Correct 15 ms 12160 KB Output is correct
10 Correct 14 ms 12032 KB Output is correct
11 Correct 12 ms 12032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 12032 KB Output is correct
2 Correct 517 ms 28844 KB Output is correct
3 Correct 660 ms 50024 KB Output is correct
4 Correct 511 ms 28908 KB Output is correct
5 Correct 477 ms 29828 KB Output is correct
6 Correct 551 ms 31928 KB Output is correct
7 Correct 417 ms 30036 KB Output is correct
8 Correct 645 ms 52184 KB Output is correct
9 Correct 352 ms 30520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 12132 KB Output is correct
2 Correct 639 ms 29036 KB Output is correct
3 Correct 736 ms 53996 KB Output is correct
4 Correct 577 ms 28904 KB Output is correct
5 Correct 548 ms 29960 KB Output is correct
6 Correct 568 ms 32312 KB Output is correct
7 Correct 356 ms 30268 KB Output is correct
8 Correct 616 ms 43164 KB Output is correct
9 Correct 392 ms 30400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 12160 KB Output is correct
2 Correct 12 ms 12160 KB Output is correct
3 Correct 12 ms 12032 KB Output is correct
4 Correct 16 ms 12160 KB Output is correct
5 Correct 13 ms 12160 KB Output is correct
6 Correct 14 ms 12160 KB Output is correct
7 Correct 15 ms 12160 KB Output is correct
8 Correct 15 ms 12160 KB Output is correct
9 Correct 15 ms 12160 KB Output is correct
10 Correct 14 ms 12032 KB Output is correct
11 Correct 12 ms 12032 KB Output is correct
12 Correct 15 ms 12032 KB Output is correct
13 Correct 20 ms 12288 KB Output is correct
14 Correct 16 ms 12444 KB Output is correct
15 Correct 15 ms 12288 KB Output is correct
16 Correct 15 ms 12288 KB Output is correct
17 Correct 14 ms 12288 KB Output is correct
18 Correct 17 ms 12272 KB Output is correct
19 Correct 17 ms 12288 KB Output is correct
20 Correct 20 ms 12288 KB Output is correct
21 Correct 17 ms 12288 KB Output is correct
22 Correct 15 ms 12288 KB Output is correct
23 Correct 16 ms 12288 KB Output is correct
24 Correct 15 ms 12544 KB Output is correct
25 Correct 15 ms 12544 KB Output is correct
26 Correct 14 ms 12416 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 12032 KB Output is correct
2 Correct 517 ms 28844 KB Output is correct
3 Correct 660 ms 50024 KB Output is correct
4 Correct 511 ms 28908 KB Output is correct
5 Correct 477 ms 29828 KB Output is correct
6 Correct 551 ms 31928 KB Output is correct
7 Correct 417 ms 30036 KB Output is correct
8 Correct 645 ms 52184 KB Output is correct
9 Correct 352 ms 30520 KB Output is correct
10 Correct 18 ms 12132 KB Output is correct
11 Correct 639 ms 29036 KB Output is correct
12 Correct 736 ms 53996 KB Output is correct
13 Correct 577 ms 28904 KB Output is correct
14 Correct 548 ms 29960 KB Output is correct
15 Correct 568 ms 32312 KB Output is correct
16 Correct 356 ms 30268 KB Output is correct
17 Correct 616 ms 43164 KB Output is correct
18 Correct 392 ms 30400 KB Output is correct
19 Correct 12 ms 12160 KB Output is correct
20 Correct 496 ms 28904 KB Output is correct
21 Correct 710 ms 54380 KB Output is correct
22 Correct 523 ms 28988 KB Output is correct
23 Correct 547 ms 28908 KB Output is correct
24 Correct 501 ms 28956 KB Output is correct
25 Correct 573 ms 28908 KB Output is correct
26 Correct 578 ms 29132 KB Output is correct
27 Correct 530 ms 28916 KB Output is correct
28 Correct 545 ms 31396 KB Output is correct
29 Correct 569 ms 29232 KB Output is correct
30 Correct 515 ms 29872 KB Output is correct
31 Correct 462 ms 30236 KB Output is correct
32 Correct 622 ms 44200 KB Output is correct
33 Correct 354 ms 30484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 12160 KB Output is correct
2 Correct 12 ms 12160 KB Output is correct
3 Correct 12 ms 12032 KB Output is correct
4 Correct 16 ms 12160 KB Output is correct
5 Correct 13 ms 12160 KB Output is correct
6 Correct 14 ms 12160 KB Output is correct
7 Correct 15 ms 12160 KB Output is correct
8 Correct 15 ms 12160 KB Output is correct
9 Correct 15 ms 12160 KB Output is correct
10 Correct 14 ms 12032 KB Output is correct
11 Correct 12 ms 12032 KB Output is correct
12 Correct 14 ms 12032 KB Output is correct
13 Correct 517 ms 28844 KB Output is correct
14 Correct 660 ms 50024 KB Output is correct
15 Correct 511 ms 28908 KB Output is correct
16 Correct 477 ms 29828 KB Output is correct
17 Correct 551 ms 31928 KB Output is correct
18 Correct 417 ms 30036 KB Output is correct
19 Correct 645 ms 52184 KB Output is correct
20 Correct 352 ms 30520 KB Output is correct
21 Correct 18 ms 12132 KB Output is correct
22 Correct 639 ms 29036 KB Output is correct
23 Correct 736 ms 53996 KB Output is correct
24 Correct 577 ms 28904 KB Output is correct
25 Correct 548 ms 29960 KB Output is correct
26 Correct 568 ms 32312 KB Output is correct
27 Correct 356 ms 30268 KB Output is correct
28 Correct 616 ms 43164 KB Output is correct
29 Correct 392 ms 30400 KB Output is correct
30 Correct 15 ms 12032 KB Output is correct
31 Correct 20 ms 12288 KB Output is correct
32 Correct 16 ms 12444 KB Output is correct
33 Correct 15 ms 12288 KB Output is correct
34 Correct 15 ms 12288 KB Output is correct
35 Correct 14 ms 12288 KB Output is correct
36 Correct 17 ms 12272 KB Output is correct
37 Correct 17 ms 12288 KB Output is correct
38 Correct 20 ms 12288 KB Output is correct
39 Correct 17 ms 12288 KB Output is correct
40 Correct 15 ms 12288 KB Output is correct
41 Correct 16 ms 12288 KB Output is correct
42 Correct 15 ms 12544 KB Output is correct
43 Correct 15 ms 12544 KB Output is correct
44 Correct 14 ms 12416 KB Output is correct
45 Correct 12 ms 12160 KB Output is correct
46 Correct 496 ms 28904 KB Output is correct
47 Correct 710 ms 54380 KB Output is correct
48 Correct 523 ms 28988 KB Output is correct
49 Correct 547 ms 28908 KB Output is correct
50 Correct 501 ms 28956 KB Output is correct
51 Correct 573 ms 28908 KB Output is correct
52 Correct 578 ms 29132 KB Output is correct
53 Correct 530 ms 28916 KB Output is correct
54 Correct 545 ms 31396 KB Output is correct
55 Correct 569 ms 29232 KB Output is correct
56 Correct 515 ms 29872 KB Output is correct
57 Correct 462 ms 30236 KB Output is correct
58 Correct 622 ms 44200 KB Output is correct
59 Correct 354 ms 30484 KB Output is correct
60 Correct 14 ms 12100 KB Output is correct
61 Correct 594 ms 30332 KB Output is correct
62 Correct 819 ms 50672 KB Output is correct
63 Correct 556 ms 30468 KB Output is correct
64 Correct 545 ms 30264 KB Output is correct
65 Correct 545 ms 30060 KB Output is correct
66 Correct 540 ms 30316 KB Output is correct
67 Correct 568 ms 30052 KB Output is correct
68 Correct 572 ms 30528 KB Output is correct
69 Correct 632 ms 32816 KB Output is correct
70 Correct 613 ms 30576 KB Output is correct
71 Correct 509 ms 31508 KB Output is correct
72 Correct 486 ms 32192 KB Output is correct
73 Correct 698 ms 42952 KB Output is correct
74 Correct 438 ms 33176 KB Output is correct