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;
typedef pair <ll, ll> pii;
#define precision(n) fixed << setprecision(n)
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define mp make_pair
#define eps (double)1e-9
#define PI 2*acos(0.0)
#define endl "\n"
#define sz(v) (int)(v).size()
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define do_not_disturb ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int N = 1e5+7;
vector <vector <int>> graph(N), flgraph;
vector <int> red, blue, par;
int dp[1007][2], adj[13][13], vis[1007], res[1007][1007], mf, f;
void augment(int v, int mnedge){
if(v == 1001){
f = mnedge;
return;
}
else if(par[v] != -1){
augment(par[v], min(mnedge, res[par[v]][v]));
res[par[v]][v] -= f;
res[v][par[v]] += f;
}
}
void findmf(){
mf = 0;
while(1){
f = 0;
bitset <1007> vismf;
vismf[1001] = 1;
queue <int> K; K.push(1001);
par.assign(1007, -1);
while(sz(K)){
int u = K.front(); K.pop();
if(u == 1002) break;
for(auto &to : flgraph[u]){
if(res[u][to] > 0 && !vismf[to]){
vismf[to] = 1;
K.push(to);
par[to] = u;
}
}
}
augment(1002, 2000000007);
if(!f) break;
mf += f;
}
}
void dfs1(int v, int alt = 1){
if(alt) blue.pb(v);
else red.pb(v);
vis[v] = 1;
for(auto to : graph[v]){
if(!vis[to]){
dfs1(to, alt^1);
}
}
}
void dfs(int v, int p = -1){
for(auto to : graph[v]){
if(to != p){
dfs(to, v);
dp[v][1] += dp[to][0];
dp[v][0] += max(dp[to][1], dp[to][0]);
}
}
}
int findSample(int n, int conf[], int host[], int prot[]){
int cnt = 0, cnt1 = 0, cnt2 = 0, flagc = 0, flagp = 0;
for(int i = 1; i < n; i++){
if(!prot[i]){
cnt++;
graph[host[i]].pb(i);
graph[i].pb(host[i]);
}
else if(prot[i]&1){
cnt1++;
for(auto to : graph[host[i]]){
graph[to].pb(i);
graph[i].pb(to);
}
}
else{
cnt2++;
flagp++;
for(auto to : graph[host[i]]){
graph[to].pb(i);
graph[i].pb(to);
}
graph[host[i]].pb(i);
graph[i].pb(host[i]);
}
if(conf[i] != 1) flagc++;
}
if(n < 11){
for(int i = 0; i < n; i++){
for(auto to : graph[i]){
adj[i][to] = adj[to][i] = 1;
}
}
int mx = 0;
for(int mask = 0; mask < (1<<n); mask++){
int sum = 0;
for(int i = 0; i < n; i++){
if(mask & (1 << i)){
sum += conf[i];
for(int j = 0; j < n; j++){
if((mask & (1 << j)) && adj[j][i]) sum = -2e9;
}
}
}
mx = max(mx, sum);
}
return mx;
}
else if(cnt1 == n-1){
int sum = 0;
for(int i = 0; i < n; i++){
sum += conf[i];
}
return sum;
}
else if(cnt2 == n-1){
int mx = 0;
for(int i = 0; i < n; i++){
mx = max(mx, conf[i]);
}
return mx;
}
else if(cnt == n-1){
for(int i = 0; i < n; i++){
dp[i][1] = conf[i];
}
dfs(0);
return max(dp[0][0], dp[0][1]);
}
else if(!flagc && !flagp){
int sum = 0;
for(int i = 0; i < n; i++){
if(!vis[i]){
dfs1(i);
flgraph = vector <vector <int>> (1007);
for(auto to : red){
flgraph[1001].pb(to);
res[1001][to] = 1;
for(auto it : graph[to]){
res[to][it] = 1;
flgraph[to].pb(it);
}
}
for(auto to : blue){
flgraph[to].pb(1002);
res[to][1002] = 1;
}
findmf();
sum += sz(red)+sz(blue)-mf;
}
}
return sum;
}
return 1;
}
| # | 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... |