CSES - APIO 2014 - Results
Submission details
Task:Beads and wires
Sender:Olli
Submission time:2019-03-24 17:39:21 +0200
Language:C++
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
#30
#40
Test results
testverdicttimegroup
#1ACCEPTED0.02 s1, 2, 3, 4details
#2ACCEPTED0.02 s1, 2, 3, 4details
#3ACCEPTED0.03 s1, 2, 3, 4details
#4ACCEPTED0.02 s1, 2, 3, 4details
#5ACCEPTED0.03 s1, 2, 3, 4details
#6ACCEPTED0.03 s1, 2, 3, 4details
#70.01 s1, 2, 3, 4details
#8ACCEPTED0.02 s1, 2, 3, 4details
#9ACCEPTED0.02 s1, 2, 3, 4details
#10ACCEPTED0.02 s1, 2, 3, 4details
#11ACCEPTED0.02 s1, 2, 3, 4details
#12ACCEPTED0.03 s1, 2, 3, 4details
#130.03 s2, 3, 4details
#140.02 s2, 3, 4details
#150.03 s2, 3, 4details
#160.03 s2, 3, 4details
#170.02 s2, 3, 4details
#180.02 s2, 3, 4details
#190.02 s2, 3, 4details
#200.02 s2, 3, 4details
#210.04 s2, 3, 4details
#220.03 s2, 3, 4details
#230.03 s3, 4details
#240.03 s3, 4details
#250.03 s3, 4details
#260.04 s3, 4details
#270.04 s3, 4details
#280.05 s3, 4details
#290.04 s3, 4details
#300.04 s3, 4details
#310.04 s3, 4details
#320.10 s4details
#330.10 s4details
#340.10 s4details
#350.37 s4details
#360.38 s4details
#370.38 s4details
#380.35 s4details
#390.34 s4details
#400.35 s4details
#410.38 s4details

Compiler report

input/code.cpp: In function 'void dfs(int)':
input/code.cpp:29:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int j = 0; j < g[i].size(); ++j) {
                 ~~^~~~~~~~~~~~~
input/code.cpp:48:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int j = 0; j < g[i].size(); ++j) {
                 ~~^~~~~~~~~~~~~
input/code.cpp:58:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int j = 0; j < g[i].size(); ++j) {
                 ~~^~~~~~~~~~~~~
input/code.cpp:87:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int j = 0; j < g[i].size(); ++j) {
                  ~~^~~~~~~~~~~~~
input/code.cpp:101:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int j = 0; j < g[i].size(); ++j) {
                  ~~^~~~~~~~~~~~~
input/code.cpp:126:21: warning: comparison be...

Code

#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <unordered_map>
#include <math.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int N = 2e5 + 5;
const ll INF = 1e18;
vector<pll> g[N];

int p[N];
bool z[N];

ll maxMissOne[N];
ll noUse[N];
ll mayUse[N];
ll indParent[N];

void dfs(int i) {
	if(z[i]) return;
	z[i] = true;
	for(int j = 0; j < g[i].size(); ++j) {
		int k = g[i][j].first;
		if(p[k] == 0) {
			p[k] = i;
			dfs(k);
		} else {
			indParent[i] = j;
		}
	}
	if(g[i].size() == 1 && i != 1) {
		noUse[i] = 0;
		mayUse[i] = 0;
		maxMissOne[i] = -INF;
		return;
	}

	//Calculate noUse
	int maxInd = -1;
	ll maxProf = -INF;
	for(int j = 0; j < g[i].size(); ++j) {
		int k = g[i][j].first;
		ll prof = mayUse[k] - noUse[k];
		if(prof >= maxProf) {
			maxInd = j;
			maxProf = prof;
		}
	}

	ll noUs = 0;
	for(int j = 0; j < g[i].size(); ++j) {
		int k = g[i][j].first;
		if(p[i] == k) continue;
		ll cand1 = mayUse[k];
		ll cand2 = maxMissOne[k] + g[i][j].second;
		if(j == maxInd) {
			noUs += max(cand1, cand2);
		} else {
			cand1 = noUse[k];
			noUs += max(cand1, cand2);
		}
	}
	noUse[i] = noUs;
	//Calculate mayUse
	mayUse[i] = noUs;
	/*
	We may give two kinds of rewards: neighbor and mayUse
	Case 1. Overlap
		The remaining neighbor can be easily determined (the one suffering least from not getting maxMissOne[k] + g[i, k])
	Case 2. No overlap
		The neighboring nodes can be easily determined (the ones ...)		
	*/
	ll child = g[i].size() - 1; if(i == 1) child+=1;
	if(child >= 2) {

		//Case 1.
		vector<pll> suffer;
		vector<ll> term;
		ll sum = 0;
		for(int j = 0; j < g[i].size(); ++j) {
			int k = g[i][j].first;
			if(k == p[i]) {
				term.push_back(-INF);
				continue;
			}
			ll suff = max(maxMissOne[k] + g[i][j].second, noUse[k]) - (noUse[k] + g[i][j].second);
			suffer.push_back({suff, j});
			ll te = max(noUse[k], maxMissOne[k] + g[i][j].second);
			sum += te;
			term.push_back(te);
		}
		sort(suffer.begin(), suffer.end());

		for(int j = 0; j < g[i].size(); ++j) {
			int k = g[i][j].first;
			if(k == p[i]) continue;
			//Give overlap to node k

			ll ans = sum;
			ans -= term[j];
			ans += g[i][j].second + mayUse[k];
			ll fir = suffer[0].second;
			if(g[i][fir].first == k) {
				fir = suffer[1].second;
			}
			ans -= term[fir];
			ll nodeFir = g[i][fir].first;
			ans += g[nodeFir][indParent[nodeFir]].second + noUse[nodeFir];
			if(i == 1) {
//				cout << k << " " << ans << " .\n";
//				cout << sum << " " << term[j] << " " << g[i][j].second + mayUse[k] << "\n";
//				cout << fir << " " << nodeFir << " " << term[fir] << " " << g[nodeFir][indParent[nodeFir]].second + noUse[nodeFir] << "\n\n";
			}
			mayUse[i] = max(mayUse[i], ans);
		}
		//Case 2.
		if(child >= 3) {

			for(int j = 0; j < g[i].size(); ++j) {
				int k = g[i][j].first;
				if(k == p[i]) continue;
				//Give mayUse to k - "must" use
				ll ans = sum;
				ans -= term[j];
				ans += mayUse[k];
				ll fir = suffer[0].second;
				if(g[i][fir].first == k) {
					fir = suffer[1].second;
				}
				ll sec = suffer[1].second;
				if(sec == fir || g[i][sec].first == k) {
					sec = suffer[2].second;
				}

				ans -= term[fir];
				ans -= term[sec];
				fir = g[i][fir].first;
				sec = g[i][sec].first;
				ans += g[fir][indParent[fir]].second + noUse[fir];
				ans += g[sec][indParent[sec]].second + noUse[sec];
				mayUse[i] = max(mayUse[i], ans);
			}
		}

	}

	//Calculate maxMissOne

	ll sumChildren = 0;
	for(int j = 0; j < g[i].size(); ++j) {
		int k = g[i][j].first;
		if(p[i] == k) continue;
		sumChildren += max(mayUse[k], maxMissOne[k] + g[i][j].second);
	}

	for(int j = 0; j < g[i].size(); ++j) {
		int k = g[i][j].first;
		if(p[i] == k) continue;
		ll cand = sumChildren - max(mayUse[k], maxMissOne[k] + g[i][j].second) + g[i][j].second + mayUse[k];
		maxMissOne[i] = max(maxMissOne[i], cand);
	}
}

int main() {
	int n;
	cin >> n;
	for(int i = 1; i< n; ++i) {
		int a, b, c;
		cin >> a >> b >> c;
		g[a].push_back(pii{b, c});
		g[b].push_back(pii{a, c});
	}

	p[1] = -1;
	dfs(1);

	for(int i = 1; i <= n; ++i) {
//		cout << i << " " << noUse[i] << " " << mayUse[i] << " " << maxMissOne[i] << "\n";
	}


	cout << mayUse[1] << "\n";
}

Test details

Test 1

Group: 1, 2, 3, 4

Verdict: ACCEPTED

input
5
1 2 10
1 3 40
1 4 15
1 5 20

correct output
60

user output
60

Test 2

Group: 1, 2, 3, 4

Verdict: ACCEPTED

input
10
4 10 2
1 2 21
1 3 13
6 7 1
...

correct output
140

user output
140

Test 3

Group: 1, 2, 3, 4

Verdict: ACCEPTED

input
10
4 10 5
1 10 7
3 10 10
3 9 10
...

correct output
61

user output
61

Test 4

Group: 1, 2, 3, 4

Verdict: ACCEPTED

input
10
7 10 1
2 7 3
6 7 5
1 9 5
...

correct output
30

user output
30

Test 5

Group: 1, 2, 3, 4

Verdict: ACCEPTED

input
10
3 10 6
3 7 6
8 9 3
1 5 1
...

correct output
44

user output
44

Test 6

Group: 1, 2, 3, 4

Verdict: ACCEPTED

input
10
5 6 9
2 3 5
1 10 8
4 5 9
...

correct output
62

user output
62

Test 7

Group: 1, 2, 3, 4

Verdict:

input
10
2 3 7
6 9 7
2 10 6
4 9 2
...

correct output
41

user output
47

Test 8

Group: 1, 2, 3, 4

Verdict: ACCEPTED

input
10
7 9 1
3 5 9
1 6 3
5 9 3
...

correct output
53

user output
53

Test 9

Group: 1, 2, 3, 4

Verdict: ACCEPTED

input
10
3 6 9
2 5 5
2 4 1
4 9 2
...

correct output
43

user output
43

Test 10

Group: 1, 2, 3, 4

Verdict: ACCEPTED

input
10
2 3 4
4 8 9
1 8 2
2 4 6
...

correct output
39

user output
39

Test 11

Group: 1, 2, 3, 4

Verdict: ACCEPTED

input
10
1 8 4
2 3 7
3 10 4
2 4 7
...

correct output
48

user output
48

Test 12

Group: 1, 2, 3, 4

Verdict: ACCEPTED

input
10
8 9 7
2 3 8
2 6 5
2 9 1
...

correct output
35

user output
35

Test 13

Group: 2, 3, 4

Verdict:

input
100
48 93 4
12 87 1
27 72 10
3 43 2
...

correct output
441

user output
469

Test 14

Group: 2, 3, 4

Verdict:

input
100
38 97 8
26 29 9
62 73 7
13 62 8
...

correct output
498

user output
521

Test 15

Group: 2, 3, 4

Verdict:

input
100
13 54 5
7 94 5
9 39 7
52 53 8
...

correct output
460

user output
486

Test 16

Group: 2, 3, 4

Verdict:

input
200
147 182 33
101 186 14
7 66 10
73 180 33
...

correct output
4537

user output
4845

Test 17

Group: 2, 3, 4

Verdict:

input
200
33 93 17
63 114 24
19 93 42
151 168 9
...

correct output
4605

user output
4887

Test 18

Group: 2, 3, 4

Verdict:

input
200
25 94 45
143 166 19
24 103 16
133 200 43
...

correct output
4695

user output
5032

Test 19

Group: 2, 3, 4

Verdict:

input
200
74 191 24
74 171 19
2 74 50
27 74 42
...

correct output
546

user output
585

Test 20

Group: 2, 3, 4

Verdict:

input
200
98 200 12
12 87 47
19 98 31
9 87 14
...

correct output
233

user output
292

Test 21

Group: 2, 3, 4

Verdict:

input
200
47 130 8
25 47 15
47 172 33
6 47 45
...

correct output
1202

user output
1216

Test 22

Group: 2, 3, 4

Verdict:

input
200
56 174 43
28 56 22
26 112 21
56 119 44
...

correct output
3349

user output
3414

Test 23

Group: 3, 4

Verdict:

input
5000
2198 4992 964
2521 2711 961
3408 4585 975
2746 3304 974
...

correct output
3848169

user output
4140711

Test 24

Group: 3, 4

Verdict:

input
5000
1926 2920 933
565 1093 993
1894 4373 930
1713 3978 916
...

correct output
3789162

user output
4136641

Test 25

Group: 3, 4

Verdict:

input
5000
2488 4286 905
898 3460 995
3342 4660 963
38 1300 971
...

correct output
3818444

user output
4169616

Test 26

Group: 3, 4

Verdict:

input
10000
2751 6467 4918
3158 3563 4945
3261 6833 4929
2955 6313 4923
...

correct output
40189502

user output
43370076

Test 27

Group: 3, 4

Verdict:

input
10000
2180 7493 4923
5745 9205 4949
446 7909 4938
2787 8203 4921
...

correct output
40070133

user output
43015333

Test 28

Group: 3, 4

Verdict:

input
10000
3748 4584 4926
3748 7419 4990
1194 3748 4924
3748 6181 4996
...

correct output
3991669

user output
4348086

Test 29

Group: 3, 4

Verdict:

input
10000
1070 4104 4988
2982 3086 4963
7216 8547 4971
1070 7973 4941
...

correct output
29901

user output
29988

Test 30

Group: 3, 4

Verdict:

input
10000
7902 8933 4901
3764 6085 4995
1621 3537 4934
4050 8356 4996
...

correct output
8971361

user output
8991368

Test 31

Group: 3, 4

Verdict:

input
10000
3309 7441 4960
5499 9949 4978
339 5089 4928
4076 7951 4916
...

correct output
29562255

user output
30892182

Test 32

Group: 4

Verdict:

input
50000
22758 25880 990
917 25140 901
10537 30620 913
10368 14209 993
...

correct output
38479584

user output
41605132

Test 33

Group: 4

Verdict:

input
50000
3238 13619 998
16363 38824 982
27886 39526 947
11705 35966 934
...

correct output
38503542

user output
41616527

Test 34

Group: 4

Verdict:

input
50000
35799 44598 957
11590 25577 939
4784 35538 999
5004 9994 968
...

correct output
38465202

user output
41595384

Test 35

Group: 4

Verdict:

input
200000
30736 178178 9996
90079 171554 9980
25124 79152 9901
54905 96160 9925
...

correct output
1605459774

user output
1736090653

Test 36

Group: 4

Verdict:

input
200000
108342 138357 9984
110960 113525 9938
41108 45029 9990
55734 141188 9963
...

correct output
1607393503

user output
1734714023

Test 37

Group: 4

Verdict:

input
200000
126722 130360 9943
89278 168087 9902
119299 167497 9901
53594 131583 9919
...

correct output
1606037984

user output
1733164534

Test 38

Group: 4

Verdict:

input
200000
164755 181587 9909
108623 181587 9979
322 181587 9974
181138 181587 9946
...

correct output
160485772

user output
172594227

Test 39

Group: 4

Verdict:

input
200000
27257 170181 9998
27257 46336 9911
64710 109131 9958
47607 164375 9999
...

correct output
59979

user output
60000

Test 40

Group: 4

Verdict:

input
200000
100383 177661 9902
29890 102857 9905
4153 102857 9990
42390 177661 9901
...

correct output
360716635

user output
361220469

Test 41

Group: 4

Verdict:

input
200000
122137 172111 9908
53871 122137 9978
85117 168845 9993
3821 9227 9906
...

correct output
1183642659

user output
1235231262