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;
struct edge{
int bg;
int en;
int flow;
edge (int bg, int en, int flow){
this -> bg = bg;
this -> en = en;
this -> flow = flow;
}
};
int answer = 0;
vector<int> adj[1005];
int dp_get[1005], dp_didnt_get[1005];
int side[1005];
vector<edge> edges;
vector<pair<int, int> > adj1[1005];
pair<int, int> parent[1005];
bool used[1005];
void dp (int current, int parent, int confidence[]) {
dp_get[current] = confidence[current];
bool leaf = 1;
for (int x : adj[current]) {
if (x == parent) continue;
leaf = 0;
dp(x, current, confidence);
}
if (leaf) return;
for (int x : adj[current]) {
if (x == parent) continue;
dp_get[current] += dp_didnt_get[x];
dp_didnt_get[current] += max(dp_didnt_get[x], dp_get[x]);
}
}
void make_edge(int bg, int en) {
edges.push_back(edge(bg, en, 1));
adj1[bg].push_back(make_pair(en, edges.size() - 1));
adj1[en].push_back(make_pair(bg, edges.size() - 1));
}
int found_path(int nn) {
queue <int> q;
memset(used, 0, sizeof used);
q.push(nn);
parent[nn] = make_pair(-1, -1);
used[nn] = 1;
while (!q.empty()) {
int current = q.front();
q.pop();
for (pair<int, int> x : adj1[current]) {
if (used[x.first]) continue;
if (edges[x.second].bg == current && edges[x.second].flow == 1) {
used[x.first] = 1;
parent[x.first] = make_pair(current, x.second);
q.push(x.first);
}
if (edges[x.second].en == current && edges[x.second].flow == 0) {
used[x.first] = 1;
parent[x.first] = make_pair(current, x.second);
q.push(x.first);
}
}
}
return used[nn + 1];
}
int findSample(int n, int confidence[],int host[],int protocol[]){
for (int i = 1; i < n; i++) {
if (protocol[i] == 1 || protocol[i] == 2) {
for (int x : adj[host[i]]) {
adj[x].push_back(i);
adj[i].push_back(x);
}
}
if (protocol[i] == 0 || protocol[i] == 2) {
adj[i].push_back(host[i]);
adj[host[i]].push_back(i);
}
}
if (n <= 10) {
for (int i = 0; i < (1<<n); i++) {
vector<int> group;
bool found_connectedness = 0;
for (int j = 0; j < n; j++) {
if (i & (1<<j)) group.push_back(j);
}
for (int i : group) {
for (int j : group) {
for (int x : adj[i]) if ( x == j) found_connectedness = 1;
}
}
if (found_connectedness == 1) continue;
int sum = 0;
for (int x : group) sum += confidence[x];
answer = max(answer, sum);
}
return answer;
}
bool all0 = 1, all1 = 1, all2 = 1;
for (int i = 1; i < n; i++) {
if (protocol[i] == 0) {
all1 = 0;
all2 = 0;
}
if (protocol[i] == 1) {
all0 = 0;
all2 = 0;
}
if (protocol[i] == 2) {
all0 = 0;
all1 = 0;
}
}
if (all1 == 1) {
int sum = 0;
for (int i = 0; i < n; i++) sum += confidence[i];
return sum;
}
if (all2 == 1) {
int ma = 0;
for (int i = 0; i < n; i++) ma = max(ma, confidence[i]);
return ma;
}
if (all0 == 1) {
dp(0, -1, confidence);
return max(dp_get[0], dp_didnt_get[0]);
}
side[0] = 0;
for (int i = 1; i < n; i++) {
if (protocol[i] == 0) side[i] = 1 - side[host[i]];
if (protocol[i] == 1) side[i] = side[host[i]];
}
for (int i = 0; i < n; i++) {
if (side[i] == 1) continue;
for (int x : adj[i]) make_edge(i, x);
}
for (int i = 0; i < n; i++) {
if (side[i] == 0) make_edge(n, i);
if (side[i] == 1) make_edge(i, n + 1);
}
int maximum_matching = 0;
while (found_path(n)) {
maximum_matching++;
for (int i = n + 1; parent[i].first != -1; i = parent[i].first) {
edges[parent[i].second].flow ^= 1;
}
}
return (n - maximum_matching);
}
# | 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... |