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;
typedef long long ll;
#define all(v) v.begin(), v.end()
vector<vector<int>> adj;
vector<vector<int>> blocked;
vector<vector<int>> flows;
vector<int> h;
vector<vector<int>::iterator> idx;
int n;
bool fdfs(int a, int t){
if(a == t) return 1;
for(; idx[a] != adj[a].end(); idx[a]++){
int b = *idx[a];
if(h[b] == h[a] + 1 && blocked[a][b] < flows[a][b]){
if(fdfs(b, t)){
blocked[a][b]++;
blocked[b][a]--;
return 1;
}
}
}
return 0;
}
bool bfs(int s, int t){
queue<int> q({s});
h[s] = 0;
bool ret = 0;
while(q.size()){
int a = q.front(); q.pop();
idx[a] = adj[a].begin();
for (auto &b : adj[a]) {
if(h[b] == -1 && blocked[a][b] < flows[a][b]){
h[b] = h[a] + 1;
q.push(b);
if(b == t) ret = 1;
}
}
}
return ret;
}
int flow(int s, int t){
h.assign(n, -1);
int res = 0;
while(bfs(s, t)){
int f;
while(fdfs(s, t)) res++;
h.assign(n, -1);
}
return res;
}
// Find out best sample
int findSample(int N,int confidence[],int host[],int protocol[]){
n = N;
bool no0 = 1, no1 = 1, no2 = 1, ones = 1;
for (int i = 1; i < n; i++) {
if(protocol[i] == 0) no0 = false;
if(protocol[i] == 1) no1 = false;
if(protocol[i] == 2) no2 = false;
if(confidence[i] > 1) ones = false;
}
adj.resize(n);
for (int i = 1; i < n; i++) {
if(protocol[i] == 1 || protocol[i] == 2){
for (auto &j : adj[host[i]]) {
adj[i].push_back(j);
adj[j].push_back(i);
}
}
if(protocol[i] == 0 || protocol[i] == 2) {
adj[i].push_back(host[i]);
adj[host[i]].push_back(i);
}
}
if(no0 && no2){ // no edges
int res = 0;
for (int i = 0; i < n; i++) {
res += confidence[i];
}
return res;
}
if(no0 && no1){ // fully conected
int res = 0;
for (int i = 0; i < n; i++) {
res = max(res, confidence[i]);
}
return res;
}
if(no1 && no2){ // tree
vector<int> odp(n, 0);
vector<int> ndp(n, 0);
function<void(int, int)> dfs = [&](int a, int p){
odp[a] = confidence[a];
for (auto &b : adj[a]) {
if(b == p) continue;
dfs(b, a);
ndp[a] += max(odp[b], ndp[b]);
odp[a] += ndp[b];
}
};
dfs(0, 0);
return max(odp[0], ndp[0]);
}
if(n <= 15){ // brute force
int mx = 0;
for (int i = 0; i < (1 << n); i++) {
vector<bool> active(n, 0);
int res = 0;
bool bad = 0;
for (int j = 0; j < n; j++) {
if(i & (1 << j)){
active[j] = 1;
res += confidence[j];
}
}
for (int j = 0; j < n; j++) {
for (auto &k : adj[j]) {
if(active[j] && active[k]) bad = 1;
}
}
if(!bad) mx = max(mx, res);
}
return mx;
}
if(ones && no2){
vector<int> vis(n);
vector<int> lef, rig;
n += 2;
adj.resize(n);
blocked.resize(n, vector<int>(n));
flows.resize(n, vector<int>(n));
idx.resize(n);
function<void(int, bool)> dfs = [&](int a, bool l){
vis[a] = true;
if(l) lef.push_back(a);
else rig.push_back(a);
for (auto &b : adj[a]) {
if(!vis[b]) dfs(b, !l);
}
};
dfs(0, 0);
for (auto &i : lef) {
for (auto &j : adj[i]) {
flows[i][j] = 1;
}
adj[n - 2].push_back(i);
adj[i].push_back(n - 2);
flows[n - 2][i] = 1;
}
for (auto &i : rig) {
adj[n - 1].push_back(i);
adj[i].push_back(n - 1);
flows[i][n - 1] = 1;
}
int x = flow(n - 2, n - 1);
return rig.size() - x + lef.size();
}
}
Compilation message (stderr)
friend.cpp: In function 'int flow(int, int)':
friend.cpp:52:7: warning: unused variable 'f' [-Wunused-variable]
52 | int f;
| ^
friend.cpp: In function 'int findSample(int, int*, int*, int*)':
friend.cpp:171:1: warning: control reaches end of non-void function [-Wreturn-type]
171 | }
| ^
# | 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... |