码迷,mamicode.com
首页 > 其他好文 > 详细

CodeForces 687A NP-Hard Problem

时间:2016-07-05 23:53:56      阅读:328      评论:0      收藏:0      [点我收藏+]

标签:

Portal:http://codeforces.com/problemset/problem/687/A

二分图染色 好模板题 有SPJ

值得注意的是,因为C++的奇妙的运算机制

若在vector变量x.size()=0,则x.size()-1会溢出(此处坑了我3h)

这不禁让我想起了文明2里的甘地

技术分享
 1 #include<iostream>
 2 #include<algorithm>
 3 #include<set>
 4 #include<cstdio>
 5 #include<cstdlib>
 6 #include<cmath>
 7 #include<vector>
 8 using namespace std;
 9 #define FOR(i,j,k) for(int i=j;i<=k;i++)
10 #define FORD(i,j,k) for(int i=j;i>=k;i--)
11 #define LL long long
12 #define maxn 100010
13 #define SZ(x) int(x.size())
14 int n,m,j,x,y;
15 bool flag=true;
16 vector<int> a[maxn];
17 vector<int> ans[2];
18 int col[maxn],len[maxn],vis[maxn];
19 bool dfs(int s,int color)
20 {
21     col[s]=color;
22     vis[s]=1;
23     FOR(i,0,SZ(a[s])-1)
24     {
25     if(vis[a[s][i]]) {if(col[a[s][i]]==color) return false;}
26     else if(!dfs(a[s][i],-color)) return false;
27     }
28     if(color==1) ans[0].push_back(s); else ans[1].push_back(s);
29     return true;
30 }
31 int main()
32 {
33 cin>>n>>m;
34 FOR(i,1,m)
35 {
36     cin>>x>>y;
37     a[x].push_back(y);
38     a[y].push_back(x);
39 }
40 FOR(i,1,n)
41 {
42 if(!vis[i]) if(!dfs(i,1)) flag=false;
43 }
44 if(flag) 
45 FOR(i,0,1)
46 {
47     cout<<SZ(ans[i])<<endl;
48     FOR(j,0,SZ(ans[i])-1) cout<<ans[i][j]<< ;
49     cout<<endl;
50 }
51 else cout<<-1<<endl;
52 return 0;
53 }
神奇代码

神奇的(一劳永逸的?)做法

#define SZ(x) int(x.size())

技术分享

嘿嘿嘿 define大法好

 

CodeForces 687A NP-Hard Problem

标签:

原文地址:http://www.cnblogs.com/mukoiaoi/p/5645344.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!