CodeForces 510E Fox And Dinner
网络流。
原点到偶数连边,容量为2,
奇数到汇点连边,容量为2,
偶数到与之能凑成素数的奇数连边,容量为1
如果奇数个数不等于偶数个数,输出不可能
如果原点到偶数的边不满流,输出不可能
剩下的情况有解:因为一个偶数点选了两个奇数点,一个奇数点被两个偶数点选择,一定能构造出环。
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std; const int maxn = + ;
const int INF = 0x7FFFFFFF;
struct Edge
{
int from, to, cap, flow;
Edge(int u, int v, int c, int f) :from(u), to(v), cap(c), flow(f){}
};
vector<Edge>edges;
vector<int>G[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn];
int n, m, s, t;
int num[maxn]; vector<int>g[maxn];
vector<int> ans[maxn];
bool f[maxn];
int block; void init()
{
for (int i = ; i < maxn; i++) G[i].clear();
edges.clear();
}
void AddEdge(int from, int to, int cap)
{
edges.push_back(Edge(from, to, cap, ));
edges.push_back(Edge(to, from, , ));
int w = edges.size();
G[from].push_back(w - );
G[to].push_back(w - );
}
bool BFS()
{
memset(vis, , sizeof(vis));
queue<int>Q;
Q.push(s);
d[s] = ;
vis[s] = ;
while (!Q.empty())
{
int x = Q.front();
Q.pop();
for (int i = ; i<G[x].size(); i++)
{
Edge e = edges[G[x][i]];
if (!vis[e.to] && e.cap>e.flow)
{
vis[e.to] = ;
d[e.to] = d[x] + ;
Q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int x, int a)
{
if (x == t || a == )
return a;
int flow = , f;
for (int &i = cur[x]; i<G[x].size(); i++)
{
Edge e = edges[G[x][i]];
if (d[x]+ == d[e.to]&&(f=DFS(e.to,min(a,e.cap-e.flow)))>)
{
edges[G[x][i]].flow+=f;
edges[G[x][i] ^ ].flow-=f;
flow+=f;
a-=f;
if(a==) break;
}
}
if(!flow) d[x] = -;
return flow;
}
int dinic(int s, int t)
{
int flow = ;
while (BFS())
{
memset(cur, , sizeof(cur));
flow += DFS(s, INF);
}
return flow;
} bool prime(int x)
{
for(int i=;i*i<=x;i++)
if(x%i==) return ;
return ;
} void Find(int now)
{
f[now]=;
ans[block].push_back(now);
for(int i=;i<g[now].size();i++)
{
if(f[g[now][i]]) continue;
Find(g[now][i]);
}
} int main()
{
while(~scanf("%d",&n))
{
for(int i=;i<=n;i++) scanf("%d",&num[i]);
init();
s=;t=n+; int e1=,e2=;
int flag=;
for(int i=;i<=n;i++)
{
if(num[i]%==) { e1++; AddEdge(s,i,); }
else { e2++; AddEdge(i,t,); }
}
if(e1!=e2) flag=;
else
{
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(num[i]%==&&num[j]%==&&prime(num[i]+num[j]))
AddEdge(i,j,);
int Flow=dinic(s,t);
if(Flow!=*e1) flag=;
}
if(flag==) printf("Impossible\n");
else
{
block=; memset(f,,sizeof f);
for(int i=;i<maxn;i++) { g[i].clear(); ans[i].clear(); }
for(int i=;i<edges.size();i=i+)
{
if(edges[i].flow==)
{
int u=edges[i].from;
int v=edges[i].to;
g[u].push_back(v);
g[v].push_back(u);
}
} for(int i=;i<=n;i++)
{
if(f[i]) continue;
Find(i); block++;
} printf("%d\n",block);
for(int i=;i<block;i++)
{
printf("%d ",ans[i].size());
for(int j=;j<ans[i].size();j++)
printf("%d ",ans[i][j]);
printf("\n");
}
}
}
return ;
}
CodeForces 510E Fox And Dinner的更多相关文章
- codeforces 510E. Fox And Dinner 网络流
题目链接 给出n个人, 以及每个人的值, 要求他们坐在一些桌子上面, 每个桌子如果有人坐, 就必须做3个人以上. 并且相邻的两个人的值加起来必须是素数.每个人的值都>=2. 由大于等于2这个条件 ...
- Codeforces Round #290 (Div. 2) E. Fox And Dinner 网络流建模
E. Fox And Dinner time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- CF510E. Fox And Dinner
CF510E. Fox And Dinner https://codeforces.com/contest/510 分析: 由于\(a_i>2\), 相邻两个数一定一奇一偶,按奇偶建立二分图. ...
- 网络流 I - Fox And Dinner CodeForces - 510E
Fox Ciel is participating in a party in Prime Kingdom. There are n foxes there (include Fox Ciel). T ...
- Fox And Dinner CodeForces - 510E (最大流)
大意: n只狐狸, 要求分成若干个环, 每个环的狐狸不少于三只, 相邻狐狸年龄和为素数. 狐狸年龄都>=2, 那么素数一定为奇数, 相邻必须是一奇一偶, 也就是一个二分图, 源点向奇数点连容量为 ...
- 网络流(最大流)CodeForces 512C:Fox And Dinner
Fox Ciel is participating in a party in Prime Kingdom. There are n foxes there (include Fox Ciel). T ...
- Codeforces 510 E. Fox And Dinner
题目链接:http://codeforces.com/problemset/problem/510/E 乍一看和那啥魔术球问题有点神似啊/XD 其实是不一样的. 解决这道问题的关键在于发现若是相邻的两 ...
- CodeForces Round #290 Fox And Dinner
而是Div2的最后一题,当时打比赛的时候还不会最大流.自己能够把它写出来然后1A还是很开心的. 题意: 有n个不小于2的整数,现在要把他们分成若干个圈.在每个圈中,数字的个数不少于3个,而且相邻的两个 ...
- 【Codeforces】512C Fox and Dinner
[解析]欧拉筛法,奇偶分析.建二分图,网络流 [Analysis] http://blog.csdn.net/qq574857122/article/details/43453087. 所谓的连通块就 ...
随机推荐
- Perl 之 use(), require(), do(), %INC and @INC
来源: http://www.cnblogs.com/itech/archive/2013/03/12/2956185.html 转自:http://perl.apache.org/docs/gene ...
- Winsock SPI-Socks5-SSL
- ASP.NET获取客户端信息,获取客户端IP等等
山上明月 ASP.NET能知道的东西 获取服务器电脑名: Page.Server.ManchineName 获取用户信息: Page.User 获取客户端电脑名:Page.Request.UserHo ...
- Windsock套接字I/O模型学习 --- 第二章
1. select模型 select模型主要借助于apiselect来实现,所以先介绍一下select函数 int select( int nfds, // 忽略,仅是为了与 Berkeley 套接字 ...
- windows MySQL 安装
MySQL安装文件分为两种,一种是msi格式的,一种是zip格式的.如果是msi格式的可以直接点击安装,按照它给出的安装提示进行安装(相信大家的英文可以看懂英文提示),一般MySQL将会安装在C:\P ...
- php 弹窗插件
index.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:// ...
- jsp ${param.id}用法
它的取值范围Page,Request,Session,Application. ${param.id} 与输入有关,相对于 request.getParameter("id").意 ...
- 在CDockablePane中嵌入CFormView
CDockablePane中嵌入CFormView与嵌入CDialogEx稍有不同,差异主要体现在CFormView类本身与CDialogEx类的不同上,CDockablePane层面的操作完全相同. ...
- iOS 开发者应该知道的 ARM 结构
http://news.cnblogs.com/n/68903/ 我在写「NEON on iPhone 入门」的时候,曾以为读者已经比较了解 iOS设备的处理器知识.然而,看过网上的一些讨论,我才发现 ...
- HDU1171--Big Event in HDU(多重背包)
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...