【网络流】 Codeforces Round #290 (Div. 1) C. Fox And Dinner

题意:给出n个数,分成一些桌,每桌大于3个人,使得每人分别与左右两人的和均为素数,并输出一种方案。。。

解法:源点向偶数连容量为2的边,奇数向汇点连容量为2的边,偶数向能构成素数的奇数连容量为1的边,若最大流则有解。。dfs输出方案。。。


#include <iostream>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 20005
#define maxm 200005
#define eps 1e-7
#define mod 1000000007
#define INF 0x3f3f3f3f
#define PI (acos(-1.0))
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid 
#define rson o<<1 | 1, mid+1, R
#define pii pair<int, int>
#pragma comment(linker, "/STACK:16777216")
typedef long long LL;
typedef unsigned long long ULL;
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
//head

struct Edge
{
	int v, c, next;
	Edge(){}
	Edge(int v, int c, int next) : v(v), c(c), next(next) {}
}E[maxm];

struct edge
{
	int v;
	edge *next;
}*h[maxn], EE[maxm], *edges;

int H[maxn], cntE;
queue<int> q;
bool vis[maxn];
int dis[maxn];
int cur[maxn];
int cnt[maxn];
int pre[maxn];
int p[maxn];
int a[maxn];
vector<int> ans[maxn];
int n, m, s, t, flow, nv, Cnt;

void addedges(int u, int v, int c)
{
	E[cntE] = Edge(v, c, H[u]);
	H[u] = cntE++;
	E[cntE] = Edge(u, 0, H[v]);
	H[v] = cntE++;
}

void addedges(int u, int v)
{
	edges->v = v;
	edges->next = h[u];
	h[u] = edges++;
}

void bfs()
{
	memset(cnt, 0, sizeof cnt);
	memset(dis, -1, sizeof dis);
	cnt[0] = 1, dis[t] = 0;
	q.push(t);
	while(!q.empty()) {
		int u =q.front();
		q.pop();
		for(int e = H[u]; ~e; e = E[e].next) {
			int v = E[e].v;
			if(dis[v] == -1) {
				dis[v] = dis[u] + 1;
				cnt[dis[v]]++;
				q.push(v);
			}
		}
	}
}

int isap()
{
	memcpy(cur, H, sizeof H);
	flow = 0;
	bfs();
	int u = pre[s] = s, e, minv, pos, f;
	while(dis[s] < nv) {
		if(u == t) {
			f = INF;
			for(int i = s; i != t; i = E[cur[i]].v) if(E[cur[i]].c < f) {
				f = E[cur[i]].c;
				pos = i;
			}
			for(int i = s; i != t; i = E[cur[i]].v) {
				E[cur[i]].c -= f;
				E[cur[i] ^ 1].c += f;
			}
			flow += f;
			u = pos;
		}
		for(e = H[u]; ~e; e = E[e].next) if(E[e].c && dis[E[e].v] + 1 == dis[u]) break;
		if(~e) {
			cur[u] = e;
			pre[E[e].v] = u;
			u = E[e].v;
		}
		else {
			if(--cnt[dis[u]] == 0) break;
			for(e = H[u], minv = nv; ~e; e = E[e].next) if(E[e].c && minv > dis[E[e].v]) {
				minv = dis[E[e].v];
				cur[u] = e;
			}
			dis[u] = minv + 1;
			cnt[dis[u]]++;
			u = pre[u];
		}
	}
	return flow;
}

void dfs(int u)
{
	vis[u] = true;
	ans[Cnt].push_back(u);
	for(edge *e = h[u]; e; e = e->next) if(!vis[e->v]) dfs(e->v);
}

void init()
{
	edges = EE;
	memset(h, 0, sizeof h);
	memset(H, -1, sizeof H);
	for(int i = 2; i <= 20000; i++) if(p[i] == 0)
		for(int j = i + i; j <= 20000; j += i) p[j] = 1;
}

void read()
{
	scanf("%d", &n);
	for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
}

void work()
{
	Cnt = 0;
	int res = 0;
	s = 0, t = n + 1, nv = t + 1;
	for(int i = 1; i <= n; i++) {
		if(a[i] % 2 == 0) addedges(s, i, 2), res += 2, Cnt++;
		else addedges(i, t, 2);
	}
	for(int i = 1; i <= n; i++) if(a[i] % 2 == 0)
		for(int j = 1; j <= n; j++) if(p[a[i] + a[j]] == 0)
			addedges(i, j, 1);
	int t = isap();
	if(t != res || Cnt * 2 != n) {
		printf("Impossible\n");
		return;
	}
	for(int i = 1; i <= n; i++) if(a[i] % 2 == 0)
		for(int e = H[i]; ~e; e = E[e].next)
			if(E[e].c == 0) addedges(i, E[e].v), addedges(E[e].v, i);
	Cnt = 0;
	for(int i = 1; i <= n; i++) if(!vis[i]) dfs(i), Cnt++;
	printf("%d\n", Cnt);
	for(int i = 0; i < Cnt; i++) {
		printf("%d ", (int)ans[i].size());
		for(int j = 0; j < ans[i].size(); j++)
			printf("%d%c", ans[i][j], j == ans[i].size() - 1 ? '\n' : ' ');
	}
	
}

int main()
{
	init();
	read();
	work();
	
	return 0;
}


你可能感兴趣的:(codeforces,网络流)