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()
/*
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 && flow[a][b] < cap[a][b]){
if(fdfs(b, t)){
flow[a][b]++;
flow[b][a]--;
return 1;
}
}
}
return 0;
}
bool bfs(int s, int t){
queue<int> q({s});
h.assign(n, -1);
h[s] = 0;
while(q.size()){
int a = q.front(); q.pop();
idx[a] = adj[a].begin();
for (auto &b : adj[a]) {
if(h[b] == -1 && flow[a][b] < cap[a][b]){
h[b] = h[a] + 1;
q.push(b);
}
}
}
return h[t] != -1;
}
int maxFlow(int s, int t){
int res = 0;
while(bfs(s, t)){
while(fdfs(s, t)) res++;
}
return res;
}
*/
class Dinics {
public:
int n;
vector<int> cur, lvl;
vector<vector<int>> adj, flow, cap;
Dinics(int _n) : n(_n) {
adj.resize(n);
flow.resize(n, vector<int>(n));
cap.resize(n, vector<int>(n));
}
void ae(int a, int b, int c) {
adj[a].push_back(b);
adj[b].push_back(a);
cap[a][b] += c;
}
bool bfs(int s, int t) {
cur = vector<int>(n, 0);
lvl = vector<int>(n, -1);
queue<int> q({ s });
lvl[s] = 0;
while (!q.empty()) {
int a = q.front(); q.pop();
for (auto& b : adj[a]) {
if (lvl[b] == -1 && cap[a][b] > flow[a][b]) {
q.push(b);
lvl[b] = lvl[a] + 1;
}
}
}
return (lvl[t] != -1);
}
int dfs(int a, int t, int rem) {
if (a == t) return rem;
for (; cur[a] < adj[a].size(); cur[a]++) {
int b = adj[a][cur[a]];
if (lvl[a] + 1 == lvl[b] && cap[a][b] > flow[a][b]) {
if (int ret = dfs(b, t, min(rem, cap[a][b] - flow[a][b]))) {
flow[a][b] += ret;
flow[b][a] -= ret;
return ret;
}
}
}
return 0;
}
int maxFlow(int s, int t) {
int res = 0;
while (bfs(s, t)) {
while (int ret = dfs(s, t, INT_MAX)) res += ret;
}
return res;
}
};
// Find out best sample
int findSample(int n,int confidence[],int host[],int protocol[]){
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;
}
vector<vector<int>> adj(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(ones && no2){
vector<int> vis(n);
vector<int> lef, rig;
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, 1);
Dinics d(n + 2);
for (auto &i : lef) {
for (auto &j : adj[i]) {
d.ae(i, j, 1);
}
d.ae(n, i, 1);
}
for (auto &i : rig) {
d.ae(i, n + 1, 1);
}
int x = d.maxFlow(n, n + 1);
return rig.size() - x + lef.size();
}
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;
}
Compilation message (stderr)
friend.cpp: In member function 'int Dinics::dfs(int, int, int)':
friend.cpp:89:23: warning: comparison of integer expressions of different signedness: '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | for (; cur[a] < adj[a].size(); cur[a]++) {
# | 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... |