ZOJ1486 Color the Tree
来源:互联网 发布:阿尔法猎手 软件 编辑:程序博客网 时间:2024/05/17 22:15
如果从第一点开始染色,则第一点可以染m种色。我们如果以第一点为根节点,用dfs来构建一个有向图的话,入度为0的点能够染m种色,而且因为是用dfs建图的,所以入度不为0的话只能为1,这时只能染m-1种色。所以只需要单纯的根据入度来计算就可以了。以为数值非常大,所以需要大数乘法,这里我在网上随便找个模板过了。。。
/******************************************************************************* # Author : Neo Fung # Email : neosfung@gmail.com # Last modified: 2012-04-08 20:51 # Filename: ZOJ1486 Color the Tree.cpp # Description : ******************************************************************************/#ifdef _MSC_VER#define DEBUG#define _CRT_SECURE_NO_DEPRECATE#endif#include <fstream>#include <stdio.h>#include <iostream>#include <string.h>#include <string>#include <limits.h>#include <algorithm>#include <math.h>#include <numeric>#include <functional>#include <ctype.h>#include <vector>#define MAX 60#define MAX_DIGIT 200using namespace std;vector<int> vec_array[MAX];// int visit[MAX],ans;int n,m;int multiply(int* a,int ca,int* b,int cb,int* c) { int i,j,* s; s=(int*)malloc(sizeof(int)*(ca+cb)); for (i=0;i<ca+cb;i++) s[i]=0; for (i=0;i<ca;i++) for (j=0;j<cb;j++) s[i+j+1]+=(a[i])*(b[j]);//i+j+1的目的就是为了防止最高位进位而产生错误 for (i=ca+cb-1;i>=0;i--) if (s[i]>=10) { s[i-1]+=s[i]/10; s[i]%=10; } i=0; while (s[i]==0) i++;//去除前导0 for (j=0;i<ca+cb;i++,j++) c[j]=s[i]; free(s); return j;} bool in_degree[MAX],visit[MAX];void dfs(const int &x){visit[x]=true;for(int i=0;i<vec_array[x].size();++i)if(!visit[vec_array[x][i]]){in_degree[vec_array[x][i]]=true;dfs(vec_array[x][i]);}}int main(void){#ifdef DEBUG freopen("../stdin.txt","r",stdin); freopen("../stdout.txt","w",stdout); #endif int u,v; while(~scanf("%d%d",&n,&m) && n+m) {for(int i=1;i<=n;++i)vec_array[i].clear();for(int i=1;i<n;++i){scanf("%d%d",&u,&v);vec_array[u].push_back(v);vec_array[v].push_back(u);}memset(visit,false,sizeof(visit));memset(in_degree,false,sizeof(in_degree));for(int i=1;i<=n;++i)if(!visit[i])dfs(i);int a[MAX_DIGIT],b[MAX_DIGIT],c[MAX_DIGIT];int ca=0,cb=0,cc=0;int tmp=m;if(in_degree[1])--tmp;if(tmp>9){a[0]=tmp/10;a[1]=tmp%10;ca=2;}else{a[0]=tmp;ca=1;}memset(b,0,sizeof(b));memset(c,0,sizeof(c));for(int i=2;i<=n;++i){tmp=m;if(in_degree[i])--tmp;if(tmp>9){b[0]=tmp/10;b[1]=tmp%10;cb=2;}else{b[0]=tmp;cb=1;}cc=multiply(a,ca,b,cb,c);memcpy(a,c,sizeof(a));ca=cc;} if(ca==0) printf("0"); else for(int i=0;i<ca;++i) printf("%d",a[i]);printf("\n"); } return 0;}
- ZOJ1486 Color the Tree
- hdu 4603 Color the Tree
- hdu 4603 Color the Tree
- Implement the avl tree, rbtree without the two fields: parent pointer and balance/color
- Color a Tree
- Color a Tree
- Pku2054 Color a Tree
- poj2054 Color a Tree
- POJ2054.Color a Tree
- hdu_1055 Color a Tree
- PKU2054 Color a Tree
- Color a Tree
- HDU 4275 Color the Tree 树的不重复染色 求树的中心+hash
- HDU 4275 Color the Tree 树的Hash 2012年长春网络赛I题
- HDU 4275 Color the Tree(哈希+树同构+组合数学+树形dp)
- The Color Changer
- Set the background color
- get the RGB color.
- packet
- spring ibatis 配置事务
- Map的简单用法入门
- Mootools和Jquery
- Apache反向代理
- ZOJ1486 Color the Tree
- Java求马鞍点(行中最大,列中最小)
- Programming Methodology(二)
- 杰克琼斯
- WRITTEN AT FIRST
- IOS开发相关资料
- 结构标记不能嵌套在内容标记?
- htmlpaser打造个性化的爬虫程序 第一天
- 自定义方法copy数组 和 Java类库对数组复制的支持