Codeforces Round #360 (Div. 2)C. NP-Hard Problem

题意:给出一个无向图,问是否可以是二分图,

思路:染色就行了,二分图又称作二部图,是图论中的一种特殊模型。 设G=(V,E)是一个无向图,如果顶点V可分割为两个互不相交的子集(A,B),并且图中的每条边(i,j)所关联的两个顶点i和j分别属于这两个不同的顶点集(i in A,j in B),则称图G为一个二分图。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int N=1e5+10;
 4 struct node{
 5     int to,next;
 6 }e[N*4];
 7 int head[N],tot;
 8 void init(){
 9     memset(head,-1,sizeof(head));
10     tot=0;
11 }
12 int n,m;
13
14 void add(int u,int v){
15     e[tot].to=v;e[tot].next=head[u];head[u]=tot++;
16 }
17 int color[N],vis[N];
18 set<int >a[3];
19 int t=0;
20 void dfs(int u,int x){
21     if(color[u]!=-1){
22         if(color[u]!=x) t=1;return ;
23     }
24     color[u]=x;
25     a[x].insert(u);
26     for(int i=head[u];i!=-1;i=e[i].next){
27         dfs(e[i].to,x^1);
28     }
29 }
30 int main(){
31     scanf("%d%d",&n,&m);
32     init();
33     memset(color,-1,sizeof(color));
34     int x,y;
35     for(int i=1;i<=m;i++){
36         scanf("%d%d",&x,&y);
37         add(x,y);add(y,x);
38     }
39         for(int i=1;i<=n;i++){
40             if(color[i]==-1) dfs(i,1);
41         }
42         if(t) {cout<<-1<<endl;return 0;}
43         cout<<a[1].size()<<endl;
44         for(set<int >::iterator it=a[1].begin();it!=a[1].end();it++){
45             printf("%d ",*it);
46         }
47         printf("\n");
48         cout<<a[0].size()<<endl;
49         for(set<int >::iterator it=a[0].begin();it!=a[0].end();it++){
50             printf("%d ",*it);
51         }
52         printf("\n");
53
54 }
时间: 2024-08-28 18:49:11

Codeforces Round #360 (Div. 2)C. NP-Hard Problem的相关文章

Codeforces Round #360 (Div. 2) D 数学推导 E dp

Codeforces Round #360 (Div. 2) A  == B  水,但记一下: 第 n 个长度为偶数的回文数是  n+reverse(n). C    dfs 01染色,水 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i

Codeforces Round #360 (Div. 1)A (二分图&dfs染色)

题目链接:http://codeforces.com/problemset/problem/687/A 题意:给出一个n个点m条边的图,分别将每条边连接的两个点放到两个集合中,输出两个集合中的点,若不可能则输出-1: 思路:通过画图我们不难发现,图中没有出现长度为奇数的环则是可行的,反之则是不行的.那么现在我们只需判断有木有长度为偶数的环即可. 对于这点我们可以直接用dfs搜索+染色,对于当前标记为1的点,我们将其所有儿子标记为2, 对于当前标记为2的点,将其所有儿子标记为1,若出现某个节点的标

Codeforces Round #360 (Div. 2) D. Remainders Game(中国剩余定理)

D. Remainders Game Today Pari and Arya are playing a game called Remainders. Pari chooses two positive integer x and k, and tells Arya k but not x. Arya have to find the value . There are n ancient numbers c1, c2, ..., cn and Pari has to tell Arya  i

Codeforces Round #360 (Div. 2) C D E

每次AB秒出 到了C难度陡然上升...翻译都弄不懂... C 给出一张图 找出两个点的覆盖集(覆盖集是指这图中每条边都有至少一个点在这个点集里面) 并且两个点集没有交集 英文很难看懂...就是二分图的判定 看看这张图是不是二分图 输出两边的点 不是二分图输出-1 坑点是这是special judge 但是题目没说 每个联通块都要进行一次bfs 那些独立点可以不输出也可以随意分配 D 给出k与n个数ci 我们知道一个未知的数x%ci的数 问能不能求出x%k的数 可以利用中国剩余定理来解 如果我们知

Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 并查集求奇偶元环

D. Dividing Kingdom II Long time ago, there was a great kingdom and it was being ruled by The Great Arya and Pari The Great. These two had some problems about the numbers they like, so they decided to divide the great kingdom between themselves. The

Codeforces Round #279 (Div. 2) ABCD

Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name     A Team Olympiad standard input/output 1 s, 256 MB  x2377 B Queue standard input/output 2 s, 256 MB  x1250 C Hacking Cypher standard input/output 1 s, 256 MB  x740 D Chocolate standard input/

Codeforces Round #129 (Div. 1)E. Little Elephant and Strings

Codeforces Round #129 (Div. 1)E. Little Elephant and Strings 题意:给出n个字符串,问每个字符串有多少个子串(不同位置,相同的子串视作不同)至少出现在这n个字符串中的k个当中. 解法:这题学到了一个SAM的新技能,对于这多个串,建SAM的时候,不是把它们连在一起,建立SAM,而是先给它们建立Trie树,然后广搜这棵Trie树,对于Trie树上的V节点,在建SAM的时候,它应该接在Trie树上他的父亲节点后面,我们用TtoM[U]表示Tr

Codeforces Round #244 (Div. 2)D (后缀自动机)

Codeforces Round #244 (Div. 2)D (后缀自动机) (标号为0的节点一定是null节点,无论如何都不能拿来用,切记切记,以后不能再错了) 这题用后缀自动机的话,对后缀自动机的很多性质有足够深刻的理解.没想过后缀数组怎么做,因为不高兴敲.... 题意:给出两个长度均不超过5000的字符串s1,s2,求这两个串中,都只出现一次的最短公共子串. 解题思路:求的是公共子串,然后对出现的次数又有限制,第一想法就是后缀自动机啊,后缀自动机处理子串出现次数再合适不过了.做法是这样的

Codeforces Round #244 (Div. 2)D (后缀自己主动机)

Codeforces Round #244 (Div. 2)D (后缀自己主动机) (标号为0的节点一定是null节点,不管怎样都不能拿来用,切记切记,以后不能再错了) 这题用后缀自己主动机的话,对后缀自己主动机的非常多性质有足够深刻的理解. 没想过后缀数组怎么做.由于不高兴敲... . 题意:给出两个长度均不超过5000的字符串s1,s2,求这两个串中,都仅仅出现一次的最短公共子串. 解题思路:求的是公共子串,然后对出现的次数又有限制,第一想法就是后缀自己主动机啊,后缀自己主动机处理子串出现次