当前位置: 首页 > news >正文

UVA1613 K-Graph Oddity(无向图染色简单题)

题目链接


在这里插入图片描述
1.题目大意:给出一个无向图,现在需要给每个节点染色,相邻节点颜色不同,输出最小的颜色数以及每个节点的颜色

2.第一次接触图论染色的题,实际上就是贪心,无权图的话度数最大的度数为奇数则就为最大奇数,否则则可以加一得到最大的奇数。那么直接暴力贪心即可

#include <set>
#include <map>
#include <stack>
#include <queue>
#include <math.h>
#include <cstdio>
#include <string>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
#define lowbit(x) (x&(-x))
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
const double eps=1e-8;
const double pi=acos(-1.0);
const int inf=0x3f3f3f3f;
const ll INF=1e18;
const int Mod=1e9+7;
const int maxn=2e5+10;

vector<int> G[maxn];
bool vis[maxn];
int degree[maxn],ans[maxn];
int x;

bool check(int u,int y){  //y颜色能否被染
    for(auto i : G[u]){ //遍历相邻点
        if(ans[i]==y) return 0;
    }
    return 1;
}

void dfs(int u){
    vis[u]=1;
    for(int j=1;j<=x;j++)
        if(check(u,j)){
            ans[u]=j;
            break;
        }
    for(auto i : G[u]){
        if(!vis[i]) dfs(i);
    }
}

int main(){
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int n,m,a,b;
    while(cin>>n>>m){
        memset(degree,0,sizeof degree);
        memset(vis,0,sizeof vis);
        memset(ans,0,sizeof ans);
        for(int i=1;i<=n;i++) G[i].clear();
        for(int i=0;i<m;i++){
            cin>>a>>b;
            G[a].push_back(b);
            G[b].push_back(a);
            degree[a]++,degree[b]++;
        }
        x=-1;
        for(int i=1;i<=n;i++)
            x=max(x,degree[i]);
        if(!(x&1)) x++;
        cout<<x<<endl;
        dfs(1);
        for(int i=1;i<=n;i++) cout<<ans[i]<<endl;
        cout<<endl;
    }
    return 0;
}




相关文章:

  • 属于我们的移动智能时代
  • UVA1614 Hell on the Markets(结论+贪心)
  • OPhone开发环境设置备忘录
  • 尺有所长,寸有所短——谁是主人
  • Codeforces Round #636 (Div. 3) E. Weights Distributing(bfs求无向无权图最短路径)
  • 另一种MTK特效制作的方法,层复制
  • 字典树(Trie)——入门篇
  • 警惕:电信加紧发力为哪般?(下)
  • Codeforces Round #637 (Div. 2) C. Nastya and Strange Generator(阅读理解/思维)
  • Broken Space Bar(Trie)[待补]
  • 小白兔和小灰兔
  • VC图片显示特效
  • UVA1153 Keep the Customer Satisfied(贪心+优先队列)
  • UVA10570 Meeting with Aliens(枚举/优化)
  • flash小实验
  • 【笔记】你不知道的JS读书笔记——Promise
  • 08.Android之View事件问题
  • 78. Subsets
  • IDEA 插件开发入门教程
  • Java多线程(4):使用线程池执行定时任务
  • php面试题 汇集2
  • Spring框架之我见(三)——IOC、AOP
  • Vue--数据传输
  • 经典排序算法及其 Java 实现
  • 快速构建spring-cloud+sleuth+rabbit+ zipkin+es+kibana+grafana日志跟踪平台
  • 前端攻城师
  • 一个SAP顾问在美国的这些年
  • 用简单代码看卷积组块发展
  • 原生JS动态加载JS、CSS文件及代码脚本
  • 基于django的视频点播网站开发-step3-注册登录功能 ...
  • ![CDATA[ ]] 是什么东东
  • #鸿蒙生态创新中心#揭幕仪式在深圳湾科技生态园举行
  • (9)目标检测_SSD的原理
  • (C语言)深入理解指针2之野指针与传值与传址与assert断言
  • (六)库存超卖案例实战——使用mysql分布式锁解决“超卖”问题
  • (一)Thymeleaf用法——Thymeleaf简介
  • (转)视频码率,帧率和分辨率的联系与区别
  • . NET自动找可写目录
  • .NET 发展历程
  • .NetCore项目nginx发布
  • .NET程序员迈向卓越的必由之路
  • .net网站发布-允许更新此预编译站点
  • .pyc文件还原.py文件_Python什么情况下会生成pyc文件?
  • // an array of int
  • @vue/cli脚手架
  • [145] 二叉树的后序遍历 js
  • [AIGC] Java 和 Kotlin 的区别
  • [android]-如何在向服务器发送request时附加已保存的cookie数据
  • [BZOJ1010] [HNOI2008] 玩具装箱toy (斜率优化)
  • [BZOJ2208][Jsoi2010]连通数
  • [CDOJ 838]母仪天下 【线段树手速练习 15分钟内敲完算合格】
  • [CTF]2022美团CTF WEB WP
  • [datastore@cyberfear.com].Elbie、[thekeyishere@cock.li].Elbie勒索病毒数据怎么处理|数据解密恢复
  • [IE编程] IE中使网页元素进入编辑模式
  • [javaSE] GUI(Action事件)