BZOJ 4878([Lydsy2017年5月月赛]挑战NP-Hard-染色问题)
来源:互联网 发布:美工做不出图 编辑:程序博客网 时间:2024/06/02 06:17
染色问题,每次沿边染max,注意最后如果颜色数超过k,则可以按(k+1)-k-…-1的简单路径
#include<bits/stdc++.h> using namespace std;#define For(i,n) for(int i=1;i<=n;i++)#define Fork(i,k,n) for(int i=k;i<=n;i++)#define ForkD(i,k,n) for(int i=n;i>=k;i--)#define Rep(i,n) for(int i=0;i<n;i++)#define ForD(i,n) for(int i=n;i;i--)#define RepD(i,n) for(int i=n;i>=0;i--)#define Forp(x) for(int p=pre[x];p;p=next[p])#define Forpiter(x) for(int &p=iter[x];p;p=next[p]) #define Lson (o<<1)#define Rson ((o<<1)+1)#define MEM(a) memset(a,0,sizeof(a));#define MEMI(a) memset(a,0x3f,sizeof(a));#define MEMi(a) memset(a,128,sizeof(a));#define MEMx(a,b) memset(a,b,sizeof(a));#define INF (0x3f3f3f3f)#define F (1000000007)#define pb push_back#define mp make_pair#define fi first#define se second#define vi vector<int> #define pi pair<int,int>#define SI(a) ((a).size())#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;#define PRi2D(a,n,m) For(i,n) { \ For(j,m-1) cout<<a[i][j]<<' ';\ cout<<a[i][m]<<endl; \ } #pragma comment(linker, "/STACK:102400000,102400000")#define ALL(x) (x).begin(),(x).end()typedef long long ll;typedef long double ld;typedef unsigned long long ull;ll mul(ll a,ll b){return (a*b)%F;}ll add(ll a,ll b){return (a+b)%F;}ll sub(ll a,ll b){return ((a-b)%F+F)%F;}void upd(ll &a,ll b){a=(a%F+b%F)%F;}inline int read(){ int x=0,f=1; char ch=getchar(); while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();} while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();} return x*f;} #define MAXN (1020)int n,m,k;vi edges[MAXN];int col[MAXN];bool b[MAXN]={};void dfs(int x,int fa) { col[x]=1; Rep(i,SI(edges[x])) b[col[edges[x][i]]]=1; while(b[col[x]]) ++col[x]; Rep(i,SI(edges[x])) b[col[edges[x][i]]]=0; Rep(i,SI(edges[x])) { int v=edges[x][i]; if (!col[v]) dfs(v,x); }}int main(){// freopen("bzoj4878.in","r",stdin);// freopen(".out","w",stdout); int T=read(); while(T--) { n=read(),m=read(),k=read(); For(i,m) { int u=read(),v=read(); edges[u].pb(v); edges[v].pb(u); } MEM(col) For(i,n) if (!col[i]) dfs(i,0); int mx=*max_element(col+1,col+1+n); if (mx<=k) { printf("color"); For(i,n) printf(" %d",col[i]); puts(""); } else { printf("path"); int p=max_element(col+1,col+1+n)-col; For(i,k+1) { printf(" %d",p); Rep(i,SI(edges[p])) if (col[edges[p][i]]==col[p]-1) {p=edges[p][i];break; } } puts(""); } For(i,n) edges[i].clear(); } return 0;}
阅读全文
0 0
- BZOJ 4878([Lydsy2017年5月月赛]挑战NP-Hard-染色问题)
- 4878: [Lydsy2017年5月月赛]挑战NP-Hard
- bzoj 4878: [Lydsy2017年5月月赛]挑战NP-Hard dfs
- bzoj 4878 [Lydsy2017年5月月赛]挑战NP-Hard(dfs)
- bzoj4878: [Lydsy2017年5月月赛]挑战NP-Hard
- BZOJ4878: [Lydsy2017年5月月赛]挑战NP-Hard
- BZOJ 4881 [Lydsy2017年5月月赛] 二分图染色+线段树
- bzoj 4880: [Lydsy2017年5月月赛]排名的战争
- bzoj4881 [ Lydsy2017年5月月赛 ] -- 二分图染色+线段树
- bzoj4885: [Lydsy2017年5月月赛]长方体
- BZOJ4885: [Lydsy2017年5月月赛]长方体
- bzoj5049 [Lydsy2017年5月月赛]导航系统
- BZOJ 4801([Lydsy2017年4月月赛]打牌-分类讨论)
- [BZOJ]4832: [Lydsy2017年4月月赛]抵制克苏恩
- bzoj 4831: [Lydsy2017年4月月赛]序列操作
- [bzoj-4832][Lydsy2017年4月月赛]抵制克苏恩 题解
- [BZOJ 4879][Lydsy2017年5月月赛]失控的数位板:模拟
- [BZOJ 4883][Lydsy2017年5月月赛]棋盘上的守卫:最小生成树
- 机器学习笔记
- Lua知识点三 迭代器与泛型for
- Spring_day04
- 输入一个整数,将这个整数以字符串的形式逆序输出 程序不考虑负数的情况,若数字含有0,则逆序形式也含有0,如输入为100,则输出为001
- 算法谜题80 王子之旅
- BZOJ 4878([Lydsy2017年5月月赛]挑战NP-Hard-染色问题)
- 连通图的(领接矩阵储存)创建和遍历(包含BFS,DFS两种算法)
- Adobe Flash Builder 4.7破解方法(绝对可用)
- 浏览器兼容 3 CSS重设(reset)方法总结
- POJ2886_Who Gets the Most Candies?_反素数|树状数组与Joseph问题
- Anaconda安装使用教程 python 科学计算 发行版
- ps怎么给人物抠图教程详细图解
- idea导出可执行jar包
- 在 Ubuntu 14.04 下安装 mysql 5.7.10