• [CF688C]NP-Hard Problem(二分图染色)


    题目链接:http://codeforces.com/contest/688/problem/C

    题意:给一张图,问是否可以划分成二分图。可以的话打印出两个集合。

    直接染色,相同颜色放入同一个集合里。如果有单独的点,则任意划分。

      1 /*
      2 ━━━━━┒ギリギリ♂ eye!
      3 ┓┏┓┏┓┃キリキリ♂ mind!
      4 ┛┗┛┗┛┃\○/
      5 ┓┏┓┏┓┃ /
      6 ┛┗┛┗┛┃ノ)
      7 ┓┏┓┏┓┃
      8 ┛┗┛┗┛┃
      9 ┓┏┓┏┓┃
     10 ┛┗┛┗┛┃
     11 ┓┏┓┏┓┃
     12 ┛┗┛┗┛┃
     13 ┓┏┓┏┓┃
     14 ┃┃┃┃┃┃
     15 ┻┻┻┻┻┻
     16 */
     17 #include <algorithm>
     18 #include <iostream>
     19 #include <iomanip>
     20 #include <cstring>
     21 #include <climits>
     22 #include <complex>
     23 #include <fstream>
     24 #include <cassert>
     25 #include <cstdio>
     26 #include <bitset>
     27 #include <vector>
     28 #include <deque>
     29 #include <queue>
     30 #include <stack>
     31 #include <ctime>
     32 #include <set>
     33 #include <map>
     34 #include <cmath>
     35 using namespace std;
     36 #define fr first
     37 #define sc second
     38 #define cl clear
     39 #define BUG puts("here!!!")
     40 #define W(a) while(a--)
     41 #define pb(a) push_back(a)
     42 #define Rint(a) scanf("%d", &a)
     43 #define Rs(a) scanf("%s", a)
     44 #define Cin(a) cin >> a
     45 #define FRead() freopen("in", "r", stdin)
     46 #define FWrite() freopen("out", "w", stdout)
     47 #define Rep(i, len) for(int i = 0; i < (len); i++)
     48 #define For(i, a, len) for(int i = (a); i < (len); i++)
     49 #define Cls(a) memset((a), 0, sizeof(a))
     50 #define Clr(a, x) memset((a), (x), sizeof(a))
     51 #define Full(a) memset((a), 0x7f7f7f, sizeof(a))
     52 #define lrt rt << 1
     53 #define rrt rt << 1 | 1
     54 #define pi 3.14159265359
     55 #define RT return
     56 #define lowbit(x) x & (-x)
     57 #define onecnt(x) __builtin_popcount(x)
     58 typedef long long LL;
     59 typedef long double LD;
     60 typedef unsigned long long ULL;
     61 typedef pair<int, int> pii;
     62 typedef pair<string, int> psi;
     63 typedef pair<LL, LL> pll;
     64 typedef map<string, int> msi;
     65 typedef vector<int> vi;
     66 typedef vector<LL> vl;
     67 typedef vector<vl> vvl;
     68 typedef vector<bool> vb;
     69 
     70 
     71 typedef struct Edge {
     72     int u, v;
     73     int next;
     74 }Edge;
     75 
     76 const int maxn = 100100;
     77 int n, m, ecnt;
     78 int color[maxn];
     79 int head[maxn];
     80 Edge edge[maxn*100];
     81 bool flag;
     82 vector<int> ret[2];
     83 
     84 void init() {
     85     ecnt = 0;
     86     Clr(head, -1); Clr(color, -1);
     87 }
     88 void adde(int u, int v) {
     89     edge[ecnt].u = u;
     90     edge[ecnt].v = v;
     91     edge[ecnt].next = head[u];
     92     head[u] = ecnt++;
     93 }
     94 
     95 void dfs(int u, int c) {
     96     if(!flag) return;
     97     if(color[u] != -1) {
     98         if(color[u] != c) flag = 0;
     99         return;
    100     }
    101     color[u] = c;
    102     ret[c].push_back(u);
    103     for(int i = head[u]; ~i; i=edge[i].next) {
    104         int v = edge[i].v;
    105         dfs(v, c^1);
    106     }
    107 }
    108 
    109 int main() {
    110     // FRead();
    111     int u, v;
    112     while(~Rint(n) && Rint(m)) {
    113         init(); ret[0].clear(); ret[1].clear(); flag = 1;
    114         Rep(i, m) {
    115             Rint(u); Rint(v);
    116             adde(u, v); adde(v, u);
    117         }
    118         For(i, 1, n+1) {
    119             if(color[i] == -1) dfs(i, 0);
    120         }
    121         if(!flag) {
    122             puts("-1");
    123             continue;
    124         }
    125         Rep(i, 2) {
    126             printf("%d
    ", ret[i].size());
    127             Rep(j, ret[i].size()) printf("%d ", ret[i][j]);
    128             printf("
    ");
    129         }
    130     }
    131     RT 0;
    132 }
  • 相关阅读:
    LOJ6284. 数列分块入门 8 题解
    LOJ6283. 数列分块入门 7 题解
    LOJ6281. 数列分块入门 5 题解
    LOJ6280. 数列分块入门 4 题解
    LOJ6279. 数列分块入门 3 题解
    LOJ6278. 数列分块入门 2 题解
    LOJ6277. 数列分块入门 1 题解
    洛谷P3402 可持久化并查集 题解
    P3919 【模板】可持久化线段树 1(可持久化数组)题解 主席树模板题
    计算机图形学:凹凸贴图、法线贴图、切线空间、TBN矩阵
  • 原文地址:https://www.cnblogs.com/kirai/p/5805105.html
Copyright © 2020-2023  润新知