이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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[2][MAXN];
int val[MAXN];
vi edge[MAXN];
int dp[MAXN][2];
int ans;
int get(int flag, int u)
{
return (u == dsu[flag][u] ? u : dsu[flag][u] = get(flag, dsu[flag][u]));
}
void merge(int flag, int u, int v)
{
u = get(flag, u); v = get(flag, v);
dsu[flag][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[0][i] = i;
dsu[1][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.
FOR(i, 0, N)
{
int p = host[i], qid = protocol[i];
if (qid == 1 || qid == 2)
{
merge(0, i, p);
if (qid == 1)
{
merge(1, i, p);
arr[get(1, p)] += arr[i];
arr[i] = 0;
}
}
}
FOR(i, 0, N)
{
ckmax(val[get(0, i)], arr[i]);
}
FORD(i, N, 1)
{
int p = host[i], qid = protocol[i];
if (qid == 0)
{
//yeah
edge[get(0, p)].PB(get(0, i));
edge[get(0, i)].PB(get(0, p));
}
}
FOR(i, 0, N)
{
if (get(0, 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... |