This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "friend.h"
#include <bits/stdc++.h>
using namespace std;
template<class T, class U>
void ckmin(T &a, U b)
{
if (a > b) a = b;
}
template<class T, class U>
void ckmax(T &a, U b)
{
if (a < b) a = b;
}
#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
#define SZ(x) ((int) ((x).size()))
#define ALL(x) (x).begin(), (x).end()
#define MAXN 100013
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
int N;
int arr[MAXN];
int dsu[MAXN];
int val[MAXN];
vi edge[MAXN];
int dp[MAXN][2];
int ans;
int get(int u)
{
return (u == dsu[u] ? u : dsu[u] = get(dsu[u]));
}
void merge(int u, int v)
{
u = get(u); v = get(v);
dsu[u] = v;
return;
}
void solve(int u, int p)
{
dp[u][0] = 0;
dp[u][1] = val[u];
for (int v : edge[u])
{
if (v == p) continue;
solve(v, u);
dp[u][0] += dp[v][1];
dp[u][1] += dp[v][0];
}
ckmax(dp[u][1], dp[u][0]);
}
int findSample(int n, int confidence[], int host[], int protocol[])
{
N = n;
FOR(i, 0, N)
{
arr[i] = confidence[i];
dsu[i] = i;
}
//0 im: just a normal edge. 1 myf: they literally merge. 2 wereyourfriends:
//so you somehow want to store like components and info about them.
FORD(i, N, 1)
{
int p = host[i], qid = protocol[i];
if (qid == 1)
{
merge(i, p);
}
}
FOR(i, 0, N)
{
val[get(i)] += arr[i];
}
FOR(i, 0, N)
{
arr[i] = val[i];
}
//the structure is just like, you have a bunch of cliques inside a tree
FORD(i, N, 1)
{
int p = host[i], qid = protocol[i];
if (qid == 2)
{
//we are your friends!
merge(i, p);
}
}
FOR(i, 0, N)
{
ckmax(val[get(i)], arr[i]);
}
FORD(i, N, 1)
{
int p = host[i], qid = protocol[i];
if (qid == 0)
{
//yeah
edge[get(p)].PB(get(i));
edge[get(i)].PB(get(p));
}
}
FOR(i, 0, N)
{
if (get(i) == i)
{
solve(i, N);
ans = dp[i][1];
break;
}
}
return ans;
}
# | 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... |