이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "friend.h"
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("Ofast")
using namespace std;
using namespace __gnu_pbds;
#define lg long long
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int N = 1e3+5;
vector<set<int>> adj(N);
int cost[N], dp[N][2], par[N], ans[N];
int get(int n)
{
if(par[n] == n) return n;
return par[n] = get(par[n]);
}
void join(int u, int v)
{
par[get(u)] = get(v);
return;
}
int dfs(int src, int par = -1, int x = 0)
{
auto&ret = dp[src][x];
if(~ret) return ret;
int ans = (!x)*cost[src];
for(auto it : adj[src])
{
if(it == par) continue;
int ch = dfs(it, src, x^1);
if(x)
{
ch = max(ch, dfs(it, src, x));
}
ans += ch;
}
return ret = ans;
}
int findSample(int n, int c[], int h[], int p[])
{
cost[0] = c[0];
for(int i = 0; i < n; i++) par[i] = i;
for(int i = 1; i < n; i++)
{
p[i]++;
cost[i] = c[i];
if(p[i]&2)
{
for(auto it : adj[h[i]])
{
adj[i].insert(it);
adj[it].insert(i);
join(i, it);
}
}
if(p[i]&1)
{
adj[i].insert(h[i]);
adj[h[i]].insert(i);
join(i, h[i]);
}
}
for(int i = 0; i < n; i++)
{
memset(dp, -1, sizeof(dp));
ans[get(i)] = max(ans[get(i)], dfs(i));
}
int fin = 0;
for(int i = 0; i < n; i++)
{
if(i == get(i)) fin += ans[i];
}
return fin;
}
# | 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... |