Submission #248968

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

Compilation message

beads.cpp: In function 'int main()':
beads.cpp:81:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
beads.cpp:83: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 15 ms 27008 KB Output is correct
2 Correct 15 ms 27008 KB Output is correct
3 Correct 14 ms 27008 KB Output is correct
4 Correct 14 ms 27008 KB Output is correct
5 Correct 14 ms 27008 KB Output is correct
6 Correct 15 ms 27008 KB Output is correct
7 Correct 14 ms 27008 KB Output is correct
8 Correct 14 ms 27008 KB Output is correct
9 Correct 16 ms 27008 KB Output is correct
10 Correct 14 ms 27024 KB Output is correct
11 Correct 15 ms 27008 KB Output is correct
12 Correct 14 ms 27008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 27008 KB Output is correct
2 Correct 15 ms 27008 KB Output is correct
3 Correct 14 ms 27008 KB Output is correct
4 Correct 14 ms 27008 KB Output is correct
5 Correct 14 ms 27008 KB Output is correct
6 Correct 15 ms 27008 KB Output is correct
7 Correct 14 ms 27008 KB Output is correct
8 Correct 14 ms 27008 KB Output is correct
9 Correct 16 ms 27008 KB Output is correct
10 Correct 14 ms 27024 KB Output is correct
11 Correct 15 ms 27008 KB Output is correct
12 Correct 14 ms 27008 KB Output is correct
13 Correct 15 ms 27008 KB Output is correct
14 Correct 15 ms 27008 KB Output is correct
15 Correct 15 ms 27008 KB Output is correct
16 Correct 15 ms 27008 KB Output is correct
17 Correct 15 ms 26960 KB Output is correct
18 Correct 15 ms 27136 KB Output is correct
19 Correct 15 ms 27008 KB Output is correct
20 Correct 15 ms 27008 KB Output is correct
21 Correct 14 ms 27008 KB Output is correct
22 Correct 15 ms 27008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 27008 KB Output is correct
2 Correct 15 ms 27008 KB Output is correct
3 Correct 14 ms 27008 KB Output is correct
4 Correct 14 ms 27008 KB Output is correct
5 Correct 14 ms 27008 KB Output is correct
6 Correct 15 ms 27008 KB Output is correct
7 Correct 14 ms 27008 KB Output is correct
8 Correct 14 ms 27008 KB Output is correct
9 Correct 16 ms 27008 KB Output is correct
10 Correct 14 ms 27024 KB Output is correct
11 Correct 15 ms 27008 KB Output is correct
12 Correct 14 ms 27008 KB Output is correct
13 Correct 15 ms 27008 KB Output is correct
14 Correct 15 ms 27008 KB Output is correct
15 Correct 15 ms 27008 KB Output is correct
16 Correct 15 ms 27008 KB Output is correct
17 Correct 15 ms 26960 KB Output is correct
18 Correct 15 ms 27136 KB Output is correct
19 Correct 15 ms 27008 KB Output is correct
20 Correct 15 ms 27008 KB Output is correct
21 Correct 14 ms 27008 KB Output is correct
22 Correct 15 ms 27008 KB Output is correct
23 Correct 19 ms 27520 KB Output is correct
24 Correct 19 ms 27520 KB Output is correct
25 Correct 20 ms 27520 KB Output is correct
26 Correct 24 ms 27896 KB Output is correct
27 Correct 26 ms 28024 KB Output is correct
28 Correct 23 ms 28032 KB Output is correct
29 Correct 22 ms 27904 KB Output is correct
30 Correct 24 ms 27904 KB Output is correct
31 Correct 24 ms 28672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 27008 KB Output is correct
2 Correct 15 ms 27008 KB Output is correct
3 Correct 14 ms 27008 KB Output is correct
4 Correct 14 ms 27008 KB Output is correct
5 Correct 14 ms 27008 KB Output is correct
6 Correct 15 ms 27008 KB Output is correct
7 Correct 14 ms 27008 KB Output is correct
8 Correct 14 ms 27008 KB Output is correct
9 Correct 16 ms 27008 KB Output is correct
10 Correct 14 ms 27024 KB Output is correct
11 Correct 15 ms 27008 KB Output is correct
12 Correct 14 ms 27008 KB Output is correct
13 Correct 15 ms 27008 KB Output is correct
14 Correct 15 ms 27008 KB Output is correct
15 Correct 15 ms 27008 KB Output is correct
16 Correct 15 ms 27008 KB Output is correct
17 Correct 15 ms 26960 KB Output is correct
18 Correct 15 ms 27136 KB Output is correct
19 Correct 15 ms 27008 KB Output is correct
20 Correct 15 ms 27008 KB Output is correct
21 Correct 14 ms 27008 KB Output is correct
22 Correct 15 ms 27008 KB Output is correct
23 Correct 19 ms 27520 KB Output is correct
24 Correct 19 ms 27520 KB Output is correct
25 Correct 20 ms 27520 KB Output is correct
26 Correct 24 ms 27896 KB Output is correct
27 Correct 26 ms 28024 KB Output is correct
28 Correct 23 ms 28032 KB Output is correct
29 Correct 22 ms 27904 KB Output is correct
30 Correct 24 ms 27904 KB Output is correct
31 Correct 24 ms 28672 KB Output is correct
32 Correct 69 ms 31864 KB Output is correct
33 Correct 70 ms 31864 KB Output is correct
34 Correct 71 ms 31864 KB Output is correct
35 Correct 384 ms 46328 KB Output is correct
36 Correct 392 ms 49916 KB Output is correct
37 Correct 385 ms 49784 KB Output is correct
38 Correct 277 ms 48740 KB Output is correct
39 Correct 287 ms 48600 KB Output is correct
40 Correct 295 ms 49128 KB Output is correct
41 Correct 367 ms 58064 KB Output is correct