제출 #1025080

#제출 시각아이디문제언어결과실행 시간메모리
1025080huutuanFriend (IOI14_friend)C++14
46 / 100
185 ms2772 KiB
#include "friend.h" #include <bits/stdc++.h> using namespace std; namespace sub1{ bool adj[30][30]; bool check(int n,int confidence[],int host[],int protocol[]){ return n<=20; } int solve(int n,int confidence[],int host[],int protocol[]){ for (int i=1; i<n; ++i){ if (protocol[i]==0){ adj[i][host[i]]=1; adj[host[i]][i]=1; }else if (protocol[i]==1){ for (int j=0; j<i; ++j) if (adj[host[i]][j]){ adj[i][j]=1; adj[j][i]=1; } }else{ for (int j=0; j<i; ++j) if (adj[host[i]][j] || j==host[i]){ adj[i][j]=1; adj[j][i]=1; } } } int ans=0; for (int i=0; i<(1<<n); ++i){ int cur=0; bool check=1; for (int j=0; j<n; ++j) if (i>>j&1){ cur+=confidence[j]; for (int k=j+1; k<n; ++k) if (i>>k&1){ check&=!adj[j][k]; } } if (check) ans=max(ans, cur); } return ans; } } namespace sub2{ bool check(int n,int confidence[],int host[],int protocol[]){ return *min_element(protocol+1, protocol+n)==1 && *max_element(protocol+1, protocol+n)==1; } int solve(int n,int confidence[],int host[],int protocol[]){ return accumulate(confidence, confidence+n, 0); } } namespace sub3{ bool check(int n,int confidence[],int host[],int protocol[]){ return *min_element(protocol+1, protocol+n)==2 && *max_element(protocol+1, protocol+n)==2; } struct DSU{ vector<int> lab, val; void init(int n, int confidence[]){ lab.assign(n, -1); vector<int>(confidence, confidence+n).swap(val); } int find_set(int u){ return lab[u]<0?u:lab[u]=find_set(lab[u]); } void union_sets(int u, int v){ u=find_set(u); v=find_set(v); if (u!=v){ if (lab[u]>lab[v]) swap(u, v); lab[u]+=lab[v]; lab[v]=u; val[u]=max(val[u], val[v]); val[v]=0; } } } dsu; int solve(int n,int confidence[],int host[],int protocol[]){ dsu.init(n, confidence); for (int i=1; i<n; ++i) dsu.union_sets(i, host[i]); return accumulate(dsu.val.begin(), dsu.val.end(), 0); } } namespace sub4{ bool check(int n,int confidence[],int host[],int protocol[]){ return *min_element(protocol+1, protocol+n)==0 && *max_element(protocol+1, protocol+n)==0; } const int N=1e3; vector<int> g[N]; int f[N][2]; int a[N]; void dfs(int u){ f[u][0]=0; f[u][1]=a[u]; for (int v:g[u]) dfs(v), f[u][0]+=max(f[v][0], f[v][1]), f[u][1]+=f[v][0]; } int solve(int n,int confidence[],int host[],int protocol[]){ for (int i=1; i<n; ++i) g[host[i]].push_back(i); for (int i=0; i<n; ++i) a[i]=confidence[i]; dfs(0); return max(f[0][0], f[0][1]); } } namespace sub5{ bool check(int n,int confidence[],int host[],int protocol[]){ return *min_element(protocol+1, protocol+n)==0 && *max_element(protocol+1, protocol+n)==1 && *max_element(confidence, confidence+n)==1; } struct FlowEdge{ int u, v, c, f; FlowEdge(int _u=0, int _v=0, int _c=0, int _f=0){ u=_u, v=_v, c=_c, f=_f; } }; const int N=1e3+10; vector<int> adj[N]; int a[N], color[N]; vector<FlowEdge> e; vector<int> g[N]; int dep[N], ptr[N]; int source, sink; void add_edge(int u, int v, int c){ g[u].push_back(e.size()); e.emplace_back(u, v, c); g[v].push_back(e.size()); e.emplace_back(v, u, 0); } bool bfs(){ memset(dep, -1, sizeof dep); dep[source]=0; queue<int> q; q.push(source); while (q.size()){ int u=q.front(); q.pop(); if (u==sink) return 1; for (int i:g[u]){ if (dep[e[i].v]!=-1 || e[i].c==e[i].f) continue; dep[e[i].v]=dep[u]+1; q.push(e[i].v); } } return 0; } int dfs(int u, int pushed){ if (u==sink || !pushed) return pushed; for (int &cid=ptr[u]; cid<(int)g[u].size(); ++cid){ int eid=g[u][cid]; if (dep[e[eid].v]!=dep[u]+1 || e[eid].c==e[eid].f) continue; int t=dfs(e[eid].v, min(pushed, e[eid].c-e[eid].f)); if (!t) continue; e[eid].f+=t; e[eid^1].f-=t; return t; } return 0; } int dinic(){ int flow=0; while (bfs()){ memset(ptr, 0, sizeof ptr); int pushed; while ((pushed=dfs(source, 1e9))) flow+=pushed; } return flow; } int solve(int n,int confidence[],int host[],int protocol[]){ int sum=0; for (int i=0; i<n; ++i) a[i]=confidence[i], sum+=a[i]; for (int i=1; i<n; ++i){ if (protocol[i]==0){ adj[host[i]].push_back(i); adj[i].push_back(host[i]); color[i]=color[host[i]]^1; }else{ adj[i]=adj[host[i]]; color[i]=color[host[i]]; } } source=n; sink=n+1; for (int i=0; i<n; ++i){ if (color[i]==0){ add_edge(source, i, 1); for (int j:adj[i]) add_edge(i, j, 1); }else{ add_edge(i, sink, 1); } } return dinic(); } } // Find out best sample int findSample(int n,int confidence[],int host[],int protocol[]){ if (sub1::check(n, confidence, host, protocol)) return sub1::solve(n, confidence, host, protocol); if (sub2::check(n, confidence, host, protocol)) return sub2::solve(n, confidence, host, protocol); if (sub3::check(n, confidence, host, protocol)) return sub3::solve(n, confidence, host, protocol); if (sub4::check(n, confidence, host, protocol)) return sub4::solve(n, confidence, host, protocol); if (sub5::check(n, confidence, host, protocol)) return sub5::solve(n, confidence, host, protocol); return -1; }
#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...