而是Div2的最后一题,当时打比赛的时候还不会最大流。自己能够把它写出来然后1A还是很开心的。

题意:

有n个不小于2的整数,现在要把他们分成若干个圈。在每个圈中,数字的个数不少于3个,而且相邻的两个数之和是质数。

分析:

因为每个数都不小于2,所以相加得到的质数一定是奇数,那么在某个圈中,一定是奇偶相间的。

也就是 奇数相邻的两个数是偶数,偶数相邻的两个数是奇数。

所以一个圈中的数字一定是偶数个,所有的输入中也必须是偶数和奇数的个数相同才可能有解。

这转化为了二分图匹配,其中X是奇数,Y是偶数,如果X和Y中的两个数加起来是质数,则连一条容量为1的边。

因为每个奇数的两边是偶数,所以将X中的点与源点连一条容量为2的边。

同样地,将Y中的点与汇点连一条容量为2的边。

求一次最大流,如果满载也就是流量为n的话,说明有解。

输出解:可以根据求解最大流的时候,找到的路径,再建一个图,然后DFS找环。

 #include <bits/stdc++.h>

 using namespace std;

 const int maxn =  + ;
const int INF = ; struct Edge
{
int from, to, cap, flow;
Edge(int u, int v, int c, int f): from(u), to(v), cap(c), flow(f) {}
}; struct EdmondsKarp
{
int n, m;
vector<Edge> edges;
vector<int> G[maxn];
int a[maxn]; //可改进量
int p[maxn]; //上一条弧 void Init(int n)
{
for(int i = ; i < n; ++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, , ));
m = edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
} int MaxFlow(int s, int t)
{
int flow = ;
for(;;)
{
memset(a, , sizeof(a));
queue<int> Q;
Q.push(s);
a[s] = INF;
while(!Q.empty())
{
int x = Q.front(); Q.pop();
for(int i = ; i < G[x].size(); ++i)
{
Edge& e = edges[G[x][i]];
if(!a[e.to] && e.cap > e.flow)
{
a[e.to] = min(a[x], e.cap - e.flow);
p[e.to] = G[x][i];
Q.push(e.to);
}
}
if(a[t]) break;
}
if(!a[t]) break;
for(int u = t; u != s; u = edges[p[u]].from)
{
edges[p[u]].flow += a[t];
edges[p[u]^].flow -= a[t];
}
flow += a[t];
}
return flow;
}
}g; int a[maxn], odd[maxn], even[maxn], p1, p2;
vector<int> G[maxn], ans[maxn];
const int maxp = ;
bool prime[maxp + ], vis[maxn]; void prime_table()
{
int m = sqrt(maxp + 0.5);
for(int i = ; i <= m; ++i) if(!prime[i])
for(int j = i*i; j <= maxp; j += i) prime[j] = true;
} void find_circle(int cnt, int u)
{
ans[cnt].push_back(u);
vis[u] = true;
for(int i = ; i < G[u].size(); ++i)
{
int v = G[u][i];
if(!vis[v]) find_circle(cnt, v);
}
} int main()
{
//freopen("in.txt", "r", stdin); int n;
scanf("%d", &n);
g.Init(n+);
for(int i = ; i <= n; ++i)
{
scanf("%d", &a[i]);
if(a[i] & ) odd[p1++] = i;
else even[p2++] = i;
}
if(p1 != p2) { puts("Impossible"); return ; }//奇数和偶数个数不同 for(int i = ; i < p1; ++i)
{
g.AddEdge(, odd[i], );
g.AddEdge(even[i], n+, );
} prime_table();
for(int i = ; i < p1; ++i)
for(int j = ; j < p1; ++j)
if(!prime[ a[odd[i]] + a[even[j]] ])
g.AddEdge(odd[i], even[j], ); int flow = g.MaxFlow(, n+);
if(flow != n) { puts("Impossible"); return ; } for(int i = ; i < g.edges.size(); ++i)
{//为了寻找路径,建一个新图
Edge& e = g.edges[i];
if(e.cap == && e.flow == )
{
G[e.from].push_back(e.to);
G[e.to].push_back(e.from);
}
} int cnt = ;
for(int i = ; i <= n; ++i) if(!vis[i]) find_circle(cnt++, i); printf("%d\n", cnt);
for(int i = ; i < cnt; ++i)
{
printf("%d %d", ans[i].size(), ans[i][]);
for(int j = ; j < ans[i].size(); ++j) printf(" %d", ans[i][j]);
puts("");
} return ;
}

代码君

CodeForces Round #290 Fox And Dinner的更多相关文章

  1. 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 ...

  2. Codeforces Round #290 (Div. 2) D. Fox And Jumping dp

    D. Fox And Jumping 题目连接: http://codeforces.com/contest/510/problem/D Description Fox Ciel is playing ...

  3. Codeforces Round #290 (Div. 2) C. Fox And Names dfs

    C. Fox And Names 题目连接: http://codeforces.com/contest/510/problem/C Description Fox Ciel is going to ...

  4. Codeforces Round #290 (Div. 2) B. Fox And Two Dots dfs

    B. Fox And Two Dots 题目连接: http://codeforces.com/contest/510/problem/B Description Fox Ciel is playin ...

  5. Codeforces Round #290 (Div. 2) A. Fox And Snake 水题

    A. Fox And Snake 题目连接: http://codeforces.com/contest/510/problem/A Description Fox Ciel starts to le ...

  6. Codeforces Round #290 (Div. 2) B. Fox And Two Dots(DFS)

    http://codeforces.com/problemset/problem/510/B #include "cstdio" #include "cstring&qu ...

  7. DFS Codeforces Round #290 (Div. 2) B. Fox And Two Dots

    题目传送门 /* DFS:每个点四处寻找,判断是否与前面的颜色相同,当走到已走过的表示成一个环 */ #include <cstdio> #include <iostream> ...

  8. 找规律 Codeforces Round #290 (Div. 2) A. Fox And Snake

    题目传送门 /* 水题 找规律输出 */ #include <cstdio> #include <iostream> #include <cstring> #inc ...

  9. 拓扑排序 Codeforces Round #290 (Div. 2) C. Fox And Names

    题目传送门 /* 给出n个字符串,求是否有一个“字典序”使得n个字符串是从小到大排序 拓扑排序 详细解释:http://www.2cto.com/kf/201502/374966.html */ #i ...

随机推荐

  1. DevExpress GridControl使用(转)

    DevExpress GridControl使用 (一)原汁原味的表格展示 Dev控件中的表格控件GridControl控件非常强大.不过,一些细枝末节的地方有时候用起来不好找挺讨厌的.使用过程中,多 ...

  2. Ubuntu下安装配置zsh和oh my zsh

    zsh优势:自动补全功能强大和很高的可配置性 1.查看当前系统装了哪些shell    cat /etc/shells 2.当前正在运行的是哪个版本的shell    echo $SHELL 3.安装 ...

  3. c数组与指针

    0.数组和指针并不是相同的 我们声明数组时,同时分配了一些内存空间,用于容纳数组元素,但是当我们声明一个指针时,只分配了用于容纳指针本身的内存空间. 从这个方面也可以理解sizeof后面跟数组名和指针 ...

  4. 2876: [Noi2012]骑行川藏 - BZOJ

    Description 蛋蛋非常热衷于挑战自我,今年暑假他准备沿川藏线骑着自行车从成都前往拉萨.川藏线的沿途有着非常美丽的风景,但在这一路上也有着很多的艰难险阻,路况变化多端,而蛋蛋的体力十分有限,因 ...

  5. UVA 714 Copying Books 二分

    题目链接: 题目 Copying Books Time limit: 3.000 seconds 问题描述 Before the invention of book-printing, it was ...

  6. HTML教程XHTML教程:HTML标记嵌套使用技巧

    网页制作Webjx文章简介:WEB标准-HTML元素嵌套. 先来看以下这样一段代码: <ul>    <li><h4><a href="" ...

  7. PAT-乙级-1049. 数列的片段和(20)

    1049. 数列的片段和(20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CAO, Peng 给定一个正数数列,我们可以从中截 ...

  8. Web Server 和 HTTP 协议

    https://toutiao.io/posts/xm2fr/preview 一直在找实习,有点什么东西直接就在evernote里面记了,也没时间来更新到这里.找实习真是个蛋疼的事,一直找的是困难模式 ...

  9. C/C++框架和库

    http://blog.csdn.net/xiaoxiaoyeyaya/article/details/42541419 值得学习的C语言开源项目 - 1. Webbench Webbench是一个在 ...

  10. C++ Variables and Basic Types Notes

    1. Type conversion: If we assign an out-of-range value to an object of unsigned type, the result is ...