Codeforces Round 586 (Div. 1 + Div. 2) D题解
[D. Alex and Julian][http://codeforces.com/contest/1220/problem/D]
Boy Dima gave Julian a birthday present - set consisting of positive integers. However, he didn’t know, that Julian hates sets, but enjoys bipartite graphs more than anything else!
Julian was almost upset, but her friend Alex said, that he can build an undirected graph using this set in such way: let all integer numbers be vertices, then connect any two and with an edge if belongs to .
Unfortunately, Julian doesn’t like the graph, that was built using . Alex decided to rectify the situation, so he wants to erase some numbers form , so that graph built using the new set is bipartite. The difficulty of this task is that the graph, Alex has to work with, has an infinite number of vertices and edges! It is impossible to solve this task alone, so Alex asks you for help. Write a program that erases a subset of minimum size from so that graph constructed on the new set is bipartite.
Recall, that graph is bipartite if all its vertices can be divided into two disjoint sets such that every edge connects a vertex from different sets.
Input
First line contains an integer — size of
Second line contains integers — numbers of , all are unique
Output
In first line print single integer – number of erased elements. In second line print integers – values of erased elements.
If there are multiple answers, print any of them.
Examples
input
1 | 3 |
output
1 | 1 |
input
1 | 2 |
output
1 | 0 |
题意:把所有的整数视作节点(无限多个),如果两个整数的差值在给定的B集合中,则两点连无向边,要求得到的图是二分图,求B中至少要删掉的数。
一道有趣的图论与数论结合的题目。
首先,要知道二分图等价于图上没有奇环。所以要把所有可能形成奇环的数字组合删去。
定义:设整数 ,,其中 为奇数,若 ,则称 同阶。
以下证明,若 不同阶,且 都属于 ,则必定形成奇环。
设 为 的最小公倍数。
我们知道,若两点之间存在两条没有重复点的路径,则会形成一个环。
那么我们取两点为 ,其中 为任意整数, 为上述最小公倍数。
从 到 构造一条路径 ,可知这条路径上有 个点。
再构造另一条路径 ,这条路径上有 个点,则总共有 个点(b,q重复计算了)。
由于 不同阶,所以假设 ,,其中 为奇数。则 ,则 , 必定一奇一偶,和为奇数。
由于 是 的最小公倍数,所以两条路径上除了起点终点之外必定没有重复点。
则必定可以形成奇环。
以下证明,若 同阶,则必定不会有奇环。
假设存在奇环,且任意一条边的两端点的差值同阶。
设 ,其中 都是奇数。
有了上一段的经验,我们可以知道,若假设环的起点为 ,则可以把环的边视作用 进行一次加减,则奇环可以视为 经过奇数次的加减后又变为了 ,那么这奇数次加减之和 = 0。
即
提出
由于
所以
说明奇数个奇数的加减得到0这个偶数。与事实不符。
所以假设不成立。
结论可以类推为:有多个同阶的整数,则必定不会有奇环。
综上,集合中不能存在不同阶的数。
然而这么向很难实现。所以换种思路,对于任意一个数,最多可以留下与它同阶的所有数。
那么枚举阶数,同一阶数的所有数都留下,比较选哪一阶使得留下的数最多即可。
1 |
|