# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
102696 | 2019-03-26T20:22:28 Z | tincamatei | Election Campaign (JOI15_election_campaign) | C++14 | 249 ms | 30424 KB |
#include <bits/stdc++.h> using namespace std; const int MAX_N = 100000; const int MAX_M = 100000; const int INF = 1000000000; vector<int> graph[1+MAX_N]; vector<int> chainstart[1+MAX_N]; int dp[1+MAX_N], subarb[1+MAX_N], cost[MAX_N]; void calcSubarb(int nod, int papa = 0) { subarb[nod] = chainstart[nod].size(); for(auto it: graph[nod]) if(it != papa) { calcSubarb(it, nod); subarb[nod] += subarb[it]; } } struct NodeCell { static int bestCost[1+MAX_N]; static bool nodeExists[1+MAX_N]; static int lazy; vector<pair<int, int> > chains; static void init() { for(int i = 0; i <= MAX_N; ++i) nodeExists[i] = false; lazy = 0; } void reset() { for(int i = 0; i < chains.size(); ++i) { nodeExists[chains[i].first] = false; chains[i].second = getVal(chains[i].first); } } static bool findNode(int nod) { return nodeExists[nod]; } static void addVal(int x) { lazy += x; } static int getVal(int nod) { return bestCost[nod] + lazy; } static void insertNode(int nod, int val) { nodeExists[nod] = true; bestCost[nod] = val - lazy; } static void eraseNode(int nod) { nodeExists[nod] = false; } } node[1+MAX_N]; int NodeCell::bestCost[1+MAX_N]; int NodeCell::lazy; bool NodeCell::nodeExists[1+MAX_N]; void dfs(int nod, int papa = 0) { int heavySon = -1, sumdp = 0; for(auto it: graph[nod]) if(it != papa && (heavySon == -1 || (heavySon != -1 && subarb[it] > subarb[heavySon]))) heavySon = it; for(auto it: graph[nod]) if(it != papa && it != heavySon) { dfs(it, nod); node[it].reset(); sumdp += dp[it]; } if(heavySon != -1) { dfs(heavySon, nod); sumdp += dp[heavySon]; node[nod].chains.swap(node[heavySon].chains); for(auto it: graph[nod]) if(it != papa && it != heavySon) { for(auto x: node[it].chains) { int nodch, costch; nodch = x.first; costch = x.second; if(NodeCell::findNode(nodch)) { // Trebuie sa sterg lantul dp[nod] = max(dp[nod], costch + NodeCell::getVal(nodch) + sumdp + cost[nodch]); NodeCell::eraseNode(nodch); } else { NodeCell::insertNode(nodch, costch); node[nod].chains.push_back(x); } } } } for(auto it: chainstart[nod]) { if(NodeCell::findNode(it)) { dp[nod] = max(dp[nod], NodeCell::getVal(it) + sumdp + cost[it]); NodeCell::eraseNode(it); } else { NodeCell::insertNode(it, 0); node[nod].chains.push_back(make_pair(it, 0)); } } dp[nod] = max(dp[nod], sumdp); NodeCell::addVal(sumdp - dp[nod]); } int main() { #ifdef HOME FILE *fin = fopen("input.in", "r"); FILE *fout = fopen("output.out", "w"); #else FILE *fin = stdin; FILE *fout = stdout; #endif int N, M; fscanf(fin, "%d", &N); for(int i = 0; i < N - 1; ++i) { int a, b; fscanf(fin, "%d%d", &a, &b); graph[a].push_back(b); graph[b].push_back(a); } fscanf(fin, "%d", &M); for(int i = 0; i < M; ++i) { int a, b, c; fscanf(fin, "%d%d%d", &a, &b, &c); chainstart[a].push_back(i); chainstart[b].push_back(i); cost[i] = c; } calcSubarb(1); NodeCell::init(); dfs(1); fprintf(fout, "%d", dp[1]); fclose(fin); fclose(fout); return 0; }
Compilation message
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 8 ms | 7424 KB | Output is correct |
2 | Correct | 8 ms | 7424 KB | Output is correct |
3 | Correct | 8 ms | 7552 KB | Output is correct |
4 | Correct | 10 ms | 7552 KB | Output is correct |
5 | Correct | 100 ms | 11896 KB | Output is correct |
6 | Correct | 61 ms | 24184 KB | Output is correct |
7 | Correct | 108 ms | 19832 KB | Output is correct |
8 | Correct | 81 ms | 12152 KB | Output is correct |
9 | Correct | 108 ms | 17372 KB | Output is correct |
10 | Correct | 69 ms | 12152 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 8 ms | 7552 KB | Output is correct |
2 | Correct | 9 ms | 7552 KB | Output is correct |
3 | Correct | 12 ms | 7680 KB | Output is correct |
4 | Correct | 117 ms | 27888 KB | Output is correct |
5 | Correct | 115 ms | 27860 KB | Output is correct |
6 | Correct | 124 ms | 27836 KB | Output is correct |
7 | Correct | 123 ms | 27892 KB | Output is correct |
8 | Correct | 124 ms | 27908 KB | Output is correct |
9 | Correct | 184 ms | 27888 KB | Output is correct |
10 | Correct | 171 ms | 27892 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 8 ms | 7552 KB | Output is correct |
2 | Correct | 9 ms | 7552 KB | Output is correct |
3 | Correct | 12 ms | 7680 KB | Output is correct |
4 | Correct | 117 ms | 27888 KB | Output is correct |
5 | Correct | 115 ms | 27860 KB | Output is correct |
6 | Correct | 124 ms | 27836 KB | Output is correct |
7 | Correct | 123 ms | 27892 KB | Output is correct |
8 | Correct | 124 ms | 27908 KB | Output is correct |
9 | Correct | 184 ms | 27888 KB | Output is correct |
10 | Correct | 171 ms | 27892 KB | Output is correct |
11 | Correct | 26 ms | 8756 KB | Output is correct |
12 | Correct | 135 ms | 27992 KB | Output is correct |
13 | Correct | 155 ms | 27888 KB | Output is correct |
14 | Correct | 124 ms | 27804 KB | Output is correct |
15 | Correct | 138 ms | 27844 KB | Output is correct |
16 | Correct | 116 ms | 27916 KB | Output is correct |
17 | Correct | 147 ms | 27944 KB | Output is correct |
18 | Correct | 125 ms | 27940 KB | Output is correct |
19 | Correct | 129 ms | 27848 KB | Output is correct |
20 | Correct | 128 ms | 27900 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 228 ms | 23412 KB | Output is correct |
2 | Correct | 122 ms | 27888 KB | Output is correct |
3 | Correct | 168 ms | 24176 KB | Output is correct |
4 | Correct | 168 ms | 19056 KB | Output is correct |
5 | Correct | 191 ms | 23512 KB | Output is correct |
6 | Correct | 163 ms | 18928 KB | Output is correct |
7 | Correct | 190 ms | 23268 KB | Output is correct |
8 | Correct | 209 ms | 21868 KB | Output is correct |
9 | Correct | 118 ms | 30036 KB | Output is correct |
10 | Correct | 167 ms | 24308 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 8 ms | 7424 KB | Output is correct |
2 | Correct | 8 ms | 7424 KB | Output is correct |
3 | Correct | 8 ms | 7552 KB | Output is correct |
4 | Correct | 10 ms | 7552 KB | Output is correct |
5 | Correct | 100 ms | 11896 KB | Output is correct |
6 | Correct | 61 ms | 24184 KB | Output is correct |
7 | Correct | 108 ms | 19832 KB | Output is correct |
8 | Correct | 81 ms | 12152 KB | Output is correct |
9 | Correct | 108 ms | 17372 KB | Output is correct |
10 | Correct | 69 ms | 12152 KB | Output is correct |
11 | Correct | 10 ms | 7680 KB | Output is correct |
12 | Correct | 8 ms | 7680 KB | Output is correct |
13 | Correct | 11 ms | 7748 KB | Output is correct |
14 | Correct | 11 ms | 7680 KB | Output is correct |
15 | Correct | 11 ms | 7680 KB | Output is correct |
16 | Correct | 10 ms | 7680 KB | Output is correct |
17 | Correct | 10 ms | 7600 KB | Output is correct |
18 | Correct | 10 ms | 7680 KB | Output is correct |
19 | Correct | 9 ms | 7680 KB | Output is correct |
20 | Correct | 10 ms | 7680 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 8 ms | 7424 KB | Output is correct |
2 | Correct | 8 ms | 7424 KB | Output is correct |
3 | Correct | 8 ms | 7552 KB | Output is correct |
4 | Correct | 10 ms | 7552 KB | Output is correct |
5 | Correct | 100 ms | 11896 KB | Output is correct |
6 | Correct | 61 ms | 24184 KB | Output is correct |
7 | Correct | 108 ms | 19832 KB | Output is correct |
8 | Correct | 81 ms | 12152 KB | Output is correct |
9 | Correct | 108 ms | 17372 KB | Output is correct |
10 | Correct | 69 ms | 12152 KB | Output is correct |
11 | Correct | 8 ms | 7552 KB | Output is correct |
12 | Correct | 9 ms | 7552 KB | Output is correct |
13 | Correct | 12 ms | 7680 KB | Output is correct |
14 | Correct | 117 ms | 27888 KB | Output is correct |
15 | Correct | 115 ms | 27860 KB | Output is correct |
16 | Correct | 124 ms | 27836 KB | Output is correct |
17 | Correct | 123 ms | 27892 KB | Output is correct |
18 | Correct | 124 ms | 27908 KB | Output is correct |
19 | Correct | 184 ms | 27888 KB | Output is correct |
20 | Correct | 171 ms | 27892 KB | Output is correct |
21 | Correct | 26 ms | 8756 KB | Output is correct |
22 | Correct | 135 ms | 27992 KB | Output is correct |
23 | Correct | 155 ms | 27888 KB | Output is correct |
24 | Correct | 124 ms | 27804 KB | Output is correct |
25 | Correct | 138 ms | 27844 KB | Output is correct |
26 | Correct | 116 ms | 27916 KB | Output is correct |
27 | Correct | 147 ms | 27944 KB | Output is correct |
28 | Correct | 125 ms | 27940 KB | Output is correct |
29 | Correct | 129 ms | 27848 KB | Output is correct |
30 | Correct | 128 ms | 27900 KB | Output is correct |
31 | Correct | 228 ms | 23412 KB | Output is correct |
32 | Correct | 122 ms | 27888 KB | Output is correct |
33 | Correct | 168 ms | 24176 KB | Output is correct |
34 | Correct | 168 ms | 19056 KB | Output is correct |
35 | Correct | 191 ms | 23512 KB | Output is correct |
36 | Correct | 163 ms | 18928 KB | Output is correct |
37 | Correct | 190 ms | 23268 KB | Output is correct |
38 | Correct | 209 ms | 21868 KB | Output is correct |
39 | Correct | 118 ms | 30036 KB | Output is correct |
40 | Correct | 167 ms | 24308 KB | Output is correct |
41 | Correct | 10 ms | 7680 KB | Output is correct |
42 | Correct | 8 ms | 7680 KB | Output is correct |
43 | Correct | 11 ms | 7748 KB | Output is correct |
44 | Correct | 11 ms | 7680 KB | Output is correct |
45 | Correct | 11 ms | 7680 KB | Output is correct |
46 | Correct | 10 ms | 7680 KB | Output is correct |
47 | Correct | 10 ms | 7600 KB | Output is correct |
48 | Correct | 10 ms | 7680 KB | Output is correct |
49 | Correct | 9 ms | 7680 KB | Output is correct |
50 | Correct | 10 ms | 7680 KB | Output is correct |
51 | Correct | 220 ms | 24180 KB | Output is correct |
52 | Correct | 121 ms | 30368 KB | Output is correct |
53 | Correct | 168 ms | 24688 KB | Output is correct |
54 | Correct | 156 ms | 21256 KB | Output is correct |
55 | Correct | 196 ms | 25812 KB | Output is correct |
56 | Correct | 117 ms | 30324 KB | Output is correct |
57 | Correct | 176 ms | 25072 KB | Output is correct |
58 | Correct | 195 ms | 21568 KB | Output is correct |
59 | Correct | 249 ms | 24164 KB | Output is correct |
60 | Correct | 124 ms | 30424 KB | Output is correct |
61 | Correct | 177 ms | 25240 KB | Output is correct |
62 | Correct | 162 ms | 21908 KB | Output is correct |
63 | Correct | 208 ms | 25840 KB | Output is correct |
64 | Correct | 114 ms | 30320 KB | Output is correct |
65 | Correct | 169 ms | 25460 KB | Output is correct |
66 | Correct | 161 ms | 21112 KB | Output is correct |
67 | Correct | 209 ms | 26008 KB | Output is correct |
68 | Correct | 121 ms | 30380 KB | Output is correct |
69 | Correct | 237 ms | 23596 KB | Output is correct |
70 | Correct | 152 ms | 21104 KB | Output is correct |