Submission #248931

# Submission time Handle Problem Language Result Execution time Memory
248931 2020-07-13T18:24:33 Z patrikpavic2 Beads and wires (APIO14_beads) C++17
57 / 100
479 ms 131072 KB
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <unordered_map>
#include <ext/pb_ds/assoc_container.hpp>
 
#define X first
#define Y second
#define PB push_back
 
using namespace std;
using namespace __gnu_pbds;
 
typedef pair < int, int > pii;
typedef long long ll;
 
const int N = 2e5 + 500;
const int INF = 2e9;
 
vector < pii > v[N];
gp_hash_table < int, ll > dp[3];
gp_hash_table < int, int > vr;
ll zbr[N];
vector < pair < ll, int > > kand[N];
int n, jesam[N];
 
 
 
ll f(int x, int fl, int lst){
	if(dp[fl][x * (N - 37) + lst] != 0) 
		return dp[fl][x * (N - 37) + lst] - 1;
	//printf("x = %d fl = %d lst = %d\n", x, fl, lst);
	//char c; scanf("%c", &c);
	if(!jesam[x]){
		ll sm = 0;
		for(pii nxt : v[x]){
			if(nxt.X == lst) continue;
			ll opt = max(f(nxt.X, 0, x), f(nxt.X, 1, x) + (int)nxt.Y);
			sm += opt;
			kand[x].PB({f(nxt.X, 2, x) + nxt.Y - opt, nxt.X});
		}
		kand[x].PB({-INF, -1}); 
		kand[x].PB({-INF, -1}); 
		sort(kand[x].rbegin(), kand[x].rend());
		jesam[x] = (x == lst ? n + 1 : lst); zbr[x] = sm;
		return (dp[fl][x * (N - 37) + lst] = sm + kand[x][0].X * (fl == 1) + 1) - 1;
	}
	if(jesam[x] == lst){
		return (dp[fl][x * (N - 37) + lst] = zbr[x] + kand[x][0].X * (fl == 1) + 1) - 1;		
	}
	if(jesam[x] < n + 1){
		ll opt = max(f(jesam[x], 0, x), f(jesam[x], 1, x) + vr[x * (N - 37) + jesam[x]]);
		zbr[x] += opt;
		kand[x].PB({f(jesam[x], 2, x) + vr[x * (N - 37) + jesam[x]] - opt, jesam[x]});
		sort(kand[x].rbegin(), kand[x].rend());
		jesam[x] = n + 1;
	}
	ll sm = zbr[x] - (x == lst ? 0 : max(f(lst, 0, x), f(lst, 1, x) + vr[x * (N - 37) + lst]));
	ll dod = kand[x][0].X;
	if(kand[x][0].Y == lst)
		dod = kand[x][1].X;
	//printf("x = %d fl = %d lst = %d --> %intd + %intd\n", x, fl, lst, sm , dod * (fl == 1));
	return (dp[fl][x * (N - 37) + lst] = sm + dod * (fl == 1) + 1) - 1;		
}
 
 
int main(){
	scanf("%d", &n);
	for(int i = 1;i < n;i++){
		int x, y, z; scanf("%d%d%d", &x, &y, &z);
		v[x].PB({y, z}), v[y].PB({x, z});
		vr[x * (N - 37) + y] = z, vr[y * (N - 37) + x] = z;
	}
	ll sol = 0;
	for(int i = 1;i <= n;i++)
		sol = max(sol, f(i, 0, i));
	printf("%lld\n", sol);
	return 0;
}

Compilation message

beads.cpp: In function 'int main()':
beads.cpp:69:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
beads.cpp:71:21: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int x, y, z; scanf("%d%d%d", &x, &y, &z);
                ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9728 KB Output is correct
2 Correct 5 ms 9728 KB Output is correct
3 Correct 5 ms 9728 KB Output is correct
4 Correct 6 ms 9728 KB Output is correct
5 Correct 5 ms 9728 KB Output is correct
6 Correct 6 ms 9728 KB Output is correct
7 Correct 6 ms 9728 KB Output is correct
8 Correct 6 ms 9728 KB Output is correct
9 Correct 5 ms 9728 KB Output is correct
10 Correct 7 ms 9728 KB Output is correct
11 Correct 5 ms 9728 KB Output is correct
12 Correct 5 ms 9728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9728 KB Output is correct
2 Correct 5 ms 9728 KB Output is correct
3 Correct 5 ms 9728 KB Output is correct
4 Correct 6 ms 9728 KB Output is correct
5 Correct 5 ms 9728 KB Output is correct
6 Correct 6 ms 9728 KB Output is correct
7 Correct 6 ms 9728 KB Output is correct
8 Correct 6 ms 9728 KB Output is correct
9 Correct 5 ms 9728 KB Output is correct
10 Correct 7 ms 9728 KB Output is correct
11 Correct 5 ms 9728 KB Output is correct
12 Correct 5 ms 9728 KB Output is correct
13 Correct 6 ms 9856 KB Output is correct
14 Correct 6 ms 9856 KB Output is correct
15 Correct 7 ms 9856 KB Output is correct
16 Correct 6 ms 9856 KB Output is correct
17 Correct 6 ms 9856 KB Output is correct
18 Correct 8 ms 9856 KB Output is correct
19 Correct 7 ms 9856 KB Output is correct
20 Correct 6 ms 9856 KB Output is correct
21 Correct 6 ms 9856 KB Output is correct
22 Correct 6 ms 9856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9728 KB Output is correct
2 Correct 5 ms 9728 KB Output is correct
3 Correct 5 ms 9728 KB Output is correct
4 Correct 6 ms 9728 KB Output is correct
5 Correct 5 ms 9728 KB Output is correct
6 Correct 6 ms 9728 KB Output is correct
7 Correct 6 ms 9728 KB Output is correct
8 Correct 6 ms 9728 KB Output is correct
9 Correct 5 ms 9728 KB Output is correct
10 Correct 7 ms 9728 KB Output is correct
11 Correct 5 ms 9728 KB Output is correct
12 Correct 5 ms 9728 KB Output is correct
13 Correct 6 ms 9856 KB Output is correct
14 Correct 6 ms 9856 KB Output is correct
15 Correct 7 ms 9856 KB Output is correct
16 Correct 6 ms 9856 KB Output is correct
17 Correct 6 ms 9856 KB Output is correct
18 Correct 8 ms 9856 KB Output is correct
19 Correct 7 ms 9856 KB Output is correct
20 Correct 6 ms 9856 KB Output is correct
21 Correct 6 ms 9856 KB Output is correct
22 Correct 6 ms 9856 KB Output is correct
23 Correct 23 ms 13740 KB Output is correct
24 Correct 17 ms 14004 KB Output is correct
25 Correct 17 ms 14004 KB Output is correct
26 Correct 36 ms 17540 KB Output is correct
27 Correct 28 ms 18236 KB Output is correct
28 Correct 126 ms 17700 KB Output is correct
29 Correct 33 ms 17512 KB Output is correct
30 Correct 43 ms 17448 KB Output is correct
31 Correct 29 ms 17944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9728 KB Output is correct
2 Correct 5 ms 9728 KB Output is correct
3 Correct 5 ms 9728 KB Output is correct
4 Correct 6 ms 9728 KB Output is correct
5 Correct 5 ms 9728 KB Output is correct
6 Correct 6 ms 9728 KB Output is correct
7 Correct 6 ms 9728 KB Output is correct
8 Correct 6 ms 9728 KB Output is correct
9 Correct 5 ms 9728 KB Output is correct
10 Correct 7 ms 9728 KB Output is correct
11 Correct 5 ms 9728 KB Output is correct
12 Correct 5 ms 9728 KB Output is correct
13 Correct 6 ms 9856 KB Output is correct
14 Correct 6 ms 9856 KB Output is correct
15 Correct 7 ms 9856 KB Output is correct
16 Correct 6 ms 9856 KB Output is correct
17 Correct 6 ms 9856 KB Output is correct
18 Correct 8 ms 9856 KB Output is correct
19 Correct 7 ms 9856 KB Output is correct
20 Correct 6 ms 9856 KB Output is correct
21 Correct 6 ms 9856 KB Output is correct
22 Correct 6 ms 9856 KB Output is correct
23 Correct 23 ms 13740 KB Output is correct
24 Correct 17 ms 14004 KB Output is correct
25 Correct 17 ms 14004 KB Output is correct
26 Correct 36 ms 17540 KB Output is correct
27 Correct 28 ms 18236 KB Output is correct
28 Correct 126 ms 17700 KB Output is correct
29 Correct 33 ms 17512 KB Output is correct
30 Correct 43 ms 17448 KB Output is correct
31 Correct 29 ms 17944 KB Output is correct
32 Correct 160 ms 51180 KB Output is correct
33 Correct 155 ms 53008 KB Output is correct
34 Correct 153 ms 51176 KB Output is correct
35 Runtime error 479 ms 131072 KB Execution killed with signal 9 (could be triggered by violating memory limits)
36 Halted 0 ms 0 KB -