Submission #299781

#TimeUsernameProblemLanguageResultExecution timeMemory
299781kevleeFriend (IOI14_friend)C++17
69 / 100
40 ms4480 KiB
#include "friend.h" #include <bits/stdc++.h> using namespace std; #define pb push_back #define mod 1000000007 #define h1 7897897897897897 #define h2 7897466719774591 #define b1 98762051 #define b2 98765431 #define inf 1000000000 #define pi 3.1415926535897932384626 #define LMAX 9223372036854775807 #define ll long long #define fi first #define se second #define pii pair<int, int> #define pll pair<ll, ll> #define vi vector<int> #define vl vector<ll> #define vp vector<pii> #define SET(a, b) memset(a, b, sizeof(a)) #define all(x) (x).begin(), (x).end() #define FOR(i, a, b) for (int i = (a); i <= (b); i++) #define FORD(i, a, b) for (int i = (a); i >= (b); i--) vi edges[1005]; int dp[1005][2], a[1005], par[1005], dist[1005], color[1005], n, exhaustion, cap[1005][1005]; bool pick[15]; bool vis[1005]; void dfs(int x, int p) { dp[x][1] = a[x]; for (auto y: edges[x]) { if (y != p) { dfs(y, x); dp[x][1] += dp[y][0]; dp[x][0] += max(dp[y][0], dp[y][1]); } } } void dfs_color(int x, int c) { if (color[x]) { return; } color[x] = c; for (auto y: edges[x]) { dfs_color(y, 3-c); } } bool bfs() { queue <int> q; q.push(n); SET(vis, false); dist[n] = inf; vis[n] = true; //cout << "start" << endl; while (!q.empty()) { int f = q.front(); q.pop(); //cout << "f = " << f << endl; if (f == n+1) return true; for (auto y: edges[f]) { if (!vis[y] && cap[f][y] > 0) { q.push(y); vis[y] = true; par[y] = f; dist[y] = min(dist[f], cap[f][y]); } } } return false; } int subtask5() { int maxflow = 0; FOR(i, 0, n-1) { if (!color[i]) { dfs_color(i, 1); } } //source = n //sink = n+1 FOR(i, 0, n-1) { if (color[i] == 1) { for (auto y: edges[i]) { cap[i][y] = 1; } edges[n].pb(i); edges[i].pb(n); cap[n][i] = 1; } else { edges[n+1].pb(i); edges[i].pb(n+1); cap[i][n+1] = 1; } } while (true) { bool foundpath = bfs(); if (!foundpath) break; int path = dist[n+1]; //cout << "path = " << path << endl; maxflow += path; int node = n+1; while (node != n) { cap[par[node]][node] -= path; cap[node][par[node]] += path; node = par[node]; } } return n - maxflow; } int subtask4() { dfs(0, -1); return max(dp[0][0], dp[0][1]); } bool connected(int x, int y) { for (auto it: edges[x]) { if (it == y) return true; } return false; } void recur(int x) { if (x == n) { bool ok = true; FOR(i, 0, n-1) { FOR(j, i+1, n-1) { if (pick[i] && pick[j] && connected(i, j)) ok = false; } } if (ok) { int cnt = 0; FOR(i, 0, n-1) if (pick[i]) cnt += a[i]; exhaustion = max(exhaustion, cnt); } return; } pick[x] = 0; recur(x+1); pick[x] = 1; recur(x+1); } int subtask1() { recur(0); return exhaustion; //return subtask5(); } int findSample(int N,int confidence[],int host[],int protocol[]){ n = N; FOR(i, 0, n-1) a[i] = confidence[i]; FOR(i, 1, n-1) { if (protocol[i] == 0) { edges[i].pb(host[i]); edges[host[i]].pb(i); } else if (protocol[i] == 1) { for (auto y: edges[host[i]]) { edges[i].pb(y); edges[y].pb(i); } } else { for (auto y: edges[host[i]]) { edges[i].pb(y); edges[y].pb(i); } edges[i].pb(host[i]); edges[host[i]].pb(i); } } if (n <= 10) { return subtask1(); } if (*max_element(a, a+n) == 1) { return subtask5(); } if (protocol[1] == 0) { return subtask4(); } if (protocol[1] == 1) { int sum = 0; FOR(i, 0, n-1) { sum += a[i]; } return sum; } return *max_element(a, a+n); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...