# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
102691 | tincamatei | Election Campaign (JOI15_election_campaign) | C++14 | 235 ms | 30464 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |