zoukankan      html  css  js  c++  java
  • UVa 10004 Bicoloring

    方法:dfs 染色

    code:

      1 #include <cstdio>
      2 #include <cstring>
      3 #include <algorithm>
      4 #include <iostream>
      5 #include <string>
      6 #include <vector>
      7 #include <stack>
      8 #include <bitset>
      9 #include <cstdlib>
     10 #include <cmath>
     11 #include <set>
     12 #include <list>
     13 #include <deque>
     14 #include <map>
     15 #include <queue>
     16 #include <fstream>
     17 #include <cassert>
     18 #include <unordered_map>
     19 #include <unordered_set>
     20 #include <cmath>
     21 #include <sstream>
     22 #include <time.h>
     23 #include <complex>
     24 #include <iomanip>
     25 #define Max(a,b) ((a)>(b)?(a):(b))
     26 #define Min(a,b) ((a)<(b)?(a):(b))
     27 #define FOR(a,b,c) for (ll (a)=(b);(a)<(c);++(a))
     28 #define FORN(a,b,c) for (ll (a)=(b);(a)<=(c);++(a))
     29 #define DFOR(a,b,c) for (ll (a)=(b);(a)>=(c);--(a))
     30 #define FORSQ(a,b,c) for (ll (a)=(b);(a)*(a)<=(c);++(a))
     31 #define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a))
     32 #define FOREACH(a,b) for (auto &(a) : (b))
     33 #define rep(i,n) FOR(i,0,n)
     34 #define repn(i,n) FORN(i,1,n)
     35 #define drep(i,n) DFOR(i,n-1,0)
     36 #define drepn(i,n) DFOR(i,n,1)
     37 #define MAX(a,b) a = Max(a,b)
     38 #define MIN(a,b) a = Min(a,b)
     39 #define SQR(x) ((LL)(x) * (x))
     40 #define Reset(a,b) memset(a,b,sizeof(a))
     41 #define fi first
     42 #define se second
     43 #define mp make_pair
     44 #define pb push_back
     45 #define all(v) v.begin(),v.end()
     46 #define ALLA(arr,sz) arr,arr+sz
     47 #define SIZE(v) (int)v.size()
     48 #define SORT(v) sort(all(v))
     49 #define REVERSE(v) reverse(ALL(v))
     50 #define SORTA(arr,sz) sort(ALLA(arr,sz))
     51 #define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
     52 #define PERMUTE next_permutation
     53 #define TC(t) while(t--)
     54 #define forever for(;;)
     55 #define PINF 1000000000000
     56 #define newline '
    '
     57 
     58 #define test if(1)if(0)cerr
     59 using namespace std;
     60 using namespace std;
     61 typedef vector<int> vi;
     62 typedef vector<vi> vvi;
     63 typedef pair<int,int> ii;
     64 typedef pair<double,double> dd;
     65 typedef pair<char,char> cc;
     66 typedef vector<ii> vii;
     67 typedef long long ll;
     68 typedef unsigned long long ull;
     69 typedef pair<ll, ll> l4;
     70 const double pi = acos(-1.0);
     71 
     72 const int maxn = 200;
     73 bool g[maxn][maxn];
     74 int color[maxn];
     75 int n, l;
     76 
     77 bool dfs(int cur, int c)
     78 {
     79     if (color[cur] == -1)
     80     {
     81         color[cur] = c;
     82         for (int i = 0; i < n; ++i) if (g[cur][i] && !dfs(i, 1-c)) return false;
     83     }
     84     else if (color[cur] != c) return false;
     85     return true;
     86 }
     87 bool solve()
     88 {
     89     memset(color, -1, sizeof(color));
     90     for (int i = 0; i < n; ++i) if (color[i] == -1 && !dfs(i, 0)) return false;
     91     return true;
     92 }
     93 int main()
     94 {
     95     while (cin >> n && n)
     96     {
     97         memset(g, false, sizeof(g));
     98         cin >> l;
     99         for (int i = 0; i < l; ++i)
    100         {
    101             int u, v;   cin >> u >> v;
    102             g[u][v] = g[v][u] = true;
    103         }
    104         if (!solve()) cout << "NOT ";
    105         cout << "BICOLORABLE.
    ";
    106     }
    107 }
    108 
    109 /*
    110  3
    111  3 
    112  0 1
    113  1 2
    114  2 0
    115  3
    116  2 
    117  0 1
    118  1 2
    119  9
    120  8 
    121  0 1
    122  0 2
    123  0 3
    124  0 4
    125  0 5
    126  0 6
    127  0 7
    128  0 8
    129  0
    130  */
    View Code
  • 相关阅读:
    React Native 使用 react-native-webview 渲染 HTML
    如何对 React 函数式组件进行优化?
    如何在前端中使用protobuf?
    Grunt之预处理
    基于Hooks 的 Redux 速成课
    AssemblyScript 入门指南
    webpack常用构建优化总览
    如何在前端中使用protobuf(node篇)
    哪种编程语言最适合区块链?
    hdu 相遇周期
  • 原文地址:https://www.cnblogs.com/skyette/p/6368560.html
Copyright © 2011-2022 走看看