codeforces 687A NP-Hard Problem

本文探讨了一种将图中的顶点分为两个不相交子集的问题,使得这两个子集都构成顶点覆盖。介绍了问题背景及求解算法,并通过实例说明了如何实现这一目标。

http://www.elijahqi.win/archives/1154
Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex cover problem very interesting.

Suppose the graph G is given. Subset A of its vertices is called a vertex cover of this graph, if for each edge uv there is at least one endpoint of it in this set, i.e. or (or both).

Pari and Arya have won a great undirected graph as an award in a team contest. Now they have to split it in two parts, but both of them want their parts of the graph to be a vertex cover.

They have agreed to give you their graph and you need to find two disjoint subsets of its vertices A and B, such that both A and B are vertex cover or claim it’s impossible. Each vertex should be given to no more than one of the friends (or you can even keep it for yourself).

Input
The first line of the input contains two integers n and m (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000) — the number of vertices and the number of edges in the prize graph, respectively.

Each of the next m lines contains a pair of integers ui and vi (1  ≤  ui,  vi  ≤  n), denoting an undirected edge between ui and vi. It’s guaranteed the graph won’t contain any self-loops or multiple edges.

Output
If it’s impossible to split the graph between Pari and Arya as they expect, print “-1” (without quotes).

If there are two disjoint sets of vertices, such that both sets are vertex cover, print their descriptions. Each description must contain two lines. The first line contains a single integer k denoting the number of vertices in that vertex cover, and the second line contains k integers — the indices of vertices. Note that because of m ≥ 1, vertex cover cannot be empty.

Examples
Input
4 2
1 2
2 3
Output
1
2
2
1 3
Input
3 3
1 2
2 3
1 3
Output
-1
Note
In the first sample, you can give the vertex number 2 to Arya and vertices numbered 1 and 3 to Pari and keep vertex number 4 for yourself (or give it someone, if you wish).

In the second sample, there is no way to satisfy both Pari and Arya.

题意我读了很久没读懂..以致于睡着了

就是n个点m条边每个点分别分到两组

单独的点随便分 还可以给自己x

那个样例不是很好 大家看看题目就行 毕竟这题有spj

#include<cstdio>
#include<cstring>
#include<vector>
#define N 110000
using namespace std;
inline char gc(){
    static char now[1<<16], *S, *T;
    if(S==T){T=(S=now)+fread(now, 1, 1<<16, stdin); if(S==T)return EOF;}
    return *S++;
}
inline long long read(){
    long long x=0;char ch=gc();
    while (ch<'0'||ch>'9') ch=gc();
    while (ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=gc();}
    return x;
}
int color[N],h[N],n,m,num;
struct node{
    int y,next;
}data[220000];
bool dfs(int x,int colour){
    color[x]=colour;
    for (int i=h[x];i;i=data[i].next){
        int y=data[i].y;
        if (color[y]==color[x]) return false;
        if (color[y]!=-1) continue;
        if (!dfs(y,colour^1)) return false;
    }return true;
}
int main(){
    freopen("cf.in","r",stdin);
    n=read();m=read();
    for (int i=1;i<=m;++i){
        int x=read(),y=read();
        data[++num].y=y;data[num].next=h[x];h[x]=num;
        data[++num].y=x;data[num].next=h[y];h[y]=num;
    }
    memset(color,-1,sizeof(color));
    for (int i=1;i<=n;++i){
        if (color[i]==-1) {
            if (!dfs(i,0)){
                printf("-1");return 0;
            }
        }
    }
    vector<int> ans1,ans2;
    for (int i=1;i<=n;++i){
        if (!color[i]) ans1.push_back(i);
        if (color[i]==1) ans2.push_back(i);
    }
    printf("%d\n",ans1.size());
    for (int i=0;i<ans1.size();++i) printf("%d ",ans1[i]);printf("\n");
    printf("%d\n",ans2.size());
    for (int i=0;i<ans2.size();++i) printf("%d ",ans2[i]);
    return 0;
}

根据原作 https://pan.quark.cn/s/459657bcfd45 的源码改编 Classic-ML-Methods-Algo 引言 建立这个项目,是为了梳理和总结传统机器学习(Machine Learning)方法(methods)或者算法(algo),和各位同仁相互学习交流. 现在的深度学习本质上来自于传统的神经网络模型,很大程度上是传统机器学习的延续,同时也在不少时候需要结合传统方法来实现. 任何机器学习方法基本的流程结构都是通用的;使用的评价方法也基本通用;使用的一些数学知识也是通用的. 本文在梳理传统机器学习方法算法的同时也会顺便补充这些流程,数学上的知识以供参考. 机器学习 机器学习是人工智能(Artificial Intelligence)的一个分支,也是实现人工智能最重要的手段.区别于传统的基于规则(rule-based)的算法,机器学习可以从数据中获取知识,从而实现规定的任务[Ian Goodfellow and Yoshua Bengio and Aaron Courville的Deep Learning].这些知识可以分为四种: 总结(summarization) 预测(prediction) 估计(estimation) 假想验证(hypothesis testing) 机器学习主要关心的是预测[Varian在Big Data : New Tricks for Econometrics],预测的可以是连续性的输出变量,分类,聚类或者物品之间的有趣关联. 机器学习分类 根据数据配置(setting,是否有标签,可以是连续的也可以是离散的)和任务目标,我们可以将机器学习方法分为四种: 无监督(unsupervised) 训练数据没有给定...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值