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大法好
来源:https://www.cnblogs.com/mukoiaoi/p/5645344.html
