uva 6957 Hyacinth bfs
Hyacinth
Time Limit: 20 Sec Memory Limit: 256 MB
题目连接
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=133436
Description
As a new employee at the Northwestern Europe Routing Company (NWERC), you do a lot of thinking about wireless network architectures. Lately you learned about a multi-channel mesh network architecture (called Hyacinth) that equips each mesh network node with multiple network interface cards (NICs) to increase the network throughput. You can choose a channel frequency for each NIC. In order to communicate, for every two network nodes that are in range of each other, their NICs must share at least one common frequency. The theoretical throughput is optimal when the total number of used frequencies in the network is maximal.
Your bosses at NWERC want you to figure out a procedure for assigning frequencies to the NICs such that the number of frequencies in use is maximized, subject to the constraint that all pairs of adjacent nodes must be able to communicate. A frequency is considered used if any pair of nodes within range of each other share that frequency. In the mesh network that you will be dealing with, each node is equipped with exactly two NICs (i.e., each node can use at most two frequencies). Since you are new at NWERC, your bosses further restrict the network layouts to make your job easier: the network graph will form a tree.
Input
The input consists of:
one line with one integer n (2≤n≤10000), the number of nodes in the network;
n−1 lines, each with two space-separated integers i and j, with 1≤i,j≤n signifying that the (one-indexed) network nodes i and j are in range of each other.
Output
Output a frequency assignment for each of the 2n NICs such that all adjacent nodes can communicate and the number of used frequencies is maximized. You should output n lines, where the ith line contains the two frequencies of network node i. Valid frequencies are nonnegative integers less than 109.
If there are several correct answers, you can output any of them.
Sample Input
14
1 2
1 3
1 4
2 5
2 6
3 7
4 8
4 9
4 10
7 11
7 12
7 13
7 14
Sample Output
4711 815
666 4711
4711 42
815 7
47 666
666 54
23 42
7 2
7 1
7 3
23 4
42 5
23 6
42 8
HINT
题意
给你一颗树,然后让你染色,要求边相连的两点必须有一个颜色相同,然后问你最多有多少组相同的颜色,并输出一种样例
注意,一定是分享过后的颜色,才能算数。
题解:
找一个度不为1的结点作为根结点,除了根结点外,其他结点一个频率跟所有儿子一样,一个频率跟父亲一样找一个度不为1的结点作为根结点,除了根结点外,其他结点一个频率跟所有儿子一样,一个频率跟父亲一样
根结点特殊处理一下就好
代码:
//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200001
#define mod 10007
#define eps 1e-9
int Num;
char CH[];
//const int inf=0x7fffffff; //нчоч╢С
const int inf=0x3f3f3f3f;
/* inline void P(int x)
{
Num=0;if(!x){putchar('0');puts("");return;}
while(x>0)CH[++Num]=x%10,x/=10;
while(Num)putchar(CH[Num--]+48);
puts("");
}
*/
inline ll read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline void P(int x)
{
Num=;if(!x){putchar('');puts("");return;}
while(x>)CH[++Num]=x%,x/=;
while(Num)putchar(CH[Num--]+);
puts("");
}
//************************************************************************************** vector<int> e[maxn];
struct node
{
int x,y;
}a[maxn];
int vis[maxn];
int cnt;
void bfs(int x)
{
queue<int> q;
vis[x]=;
q.push(x);
while(!q.empty())
{
int now=q.front();
q.pop();
for(int i=;i<e[now].size();i++)
{
int next=e[now][i];
if(vis[next])
continue;
if(!a[next].x)
{
a[next].x=a[now].y;
a[next].y=cnt++;
}
vis[next]=;
q.push(next);
}
}
}
int main()
{
cnt=;
int n=read();
for(int i=;i<n-;i++)
{
int x=read(),y=read();
e[x].push_back(y);
e[y].push_back(x);
}
int flag=;
for(int i=;i<=n;i++)
{
if(e[i].size()!=)
{
flag=;
a[i].x=;
a[i].y=;
a[e[i][]].x=;
a[e[i][]].y=;
a[e[i][]].x=;
a[e[i][]].y=;
bfs(i);
break;
}
}
if(flag==)
{
for(int i=;i<=n-;i++)
{
a[i].x=i;
a[i].y=i+;
}
a[n].x=n-;
a[n].y=n;
}
for(int i=;i<=n;i++)
cout<<a[i].x<<" "<<a[i].y<<endl;
}
uva 6957 Hyacinth bfs的更多相关文章
- UVA - 11624 Fire! bfs 地图与人一步一步先后搜/搜一次打表好了再搜一次
UVA - 11624 题意:joe在一个迷宫里,迷宫的一些部分着火了,火势会向周围四个方向蔓延,joe可以向四个方向移动.火与人的速度都是1格/1秒,问j能否逃出迷宫,若能输出最小时间. 题解:先考 ...
- UVA 11624 Fire! bfs 难度:0
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVA 12130 - Summits(BFS+贪心)
UVA 12130 - Summits 题目链接 题意:给定一个h * w的图,每一个位置有一个值.如今要求出这个图上的峰顶有多少个.峰顶是这样定义的.有一个d值,假设一个位置是峰顶.那么它不能走到不 ...
- UVA 10603 - Fill BFS~
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&c ...
- UVA 11624 Fire! BFS搜索
题意:就是问你能不能在火烧到你之前,走出一个矩形区域,如果有,求出最短的时间 分析:两遍BFS,然后比较边界 #include<cstdio> #include<algorithm& ...
- UVA 11624 Fire! (bfs)
算法指南白书 分别求一次人和火到达各个点的最短时间 #include<cstdio> #include<cstring> #include<queue> #incl ...
- UVA - 11624 多点bfs [kuangbin带你飞]专题一
题意:某人身陷火场,总有k个点着火,着火点可向四周扩散,问此人能否逃离. 思路:可能有多个着火点,以这些着火点作为起点进行bfs,得到整个火场的最短距离,然后又以人所在坐标作为起点进行bfs,得到该人 ...
- hdu 2771(uva 12171) Sculpture bfs+离散化
题意: 给出一些边平行于坐标轴的长方体,这些长方体可能相交.也可能相互嵌套.这些长方体形成了一个雕塑,求这个雕塑的整体积和表面积. 题解: 最easy想到直接进行bfs或者dfs统计,但此题的麻烦之处 ...
- UVa 12717 Fiasco (BFS模拟)
题意:给定一个错误代码,让你修改数据,使得它能够输出正确答案,错误代码是每次取最短的放入. 析:那么我们就可以模拟这个过程,然后修改每条边的权值,使得它能输出正确答案. 代码如下: #pragma c ...
随机推荐
- 浅谈iOS多线程
浅谈iOS多线程 首先,先看看进程和线程的概念. 图1.1 这一块不难理解,重点点下他们的几个重要区别: 1,地址空间和资源:进程可以申请和拥有系统资源,线程不行.资源进程间相互独立,同一进程的各线程 ...
- groovy的三个强劲属性(一)Gpath
我们先从GPath开始,一个GPath是groovy代码的一个强劲对象导航的结构,名称的选择与XPath相似,XPath是一个用来描述XML(和等价物)文档的标准,正如XPath,GP ...
- Linux删除除了今天以外的文件
[背景] 开发到日志记录功能时,每天都会产生当天的一个日志,久而久之就会产生累积,想要查看的时候,tab键无法自动补全,还要自己额外输入. 比较麻烦. [命令] 经过查找和实验,找到了以下的方法: 1 ...
- java版云笔记(七)之事务管理
事务管理 事务:程序为了保证业务处理的完整性,执行的一条或多条SQL语句. 事务管理:对事务中的SQL语句进行提交或者回滚. 事物管理对于企业应用来说是至关重要的,好使出现异常情况,它也可以保证数据的 ...
- LightOJ 1323 Billiard Balls(找规律(蚂蚁爬木棍))
题目链接:https://vjudge.net/contest/28079#problem/M 题目大意: 一个边界长为L宽为W的平面同时发射n个台球,运动K秒,台球碰到桌面及两(多)个台球相撞情况如 ...
- 【转载】python-协程
转载自:廖雪峰的官方网站 协程,又称微线程,纤程.英文名Coroutine. 协程的概念很早就提出来了,但直到最近几年才在某些语言(如Lua)中得到广泛应用. 子程序,或者称为函数,在所有语言中都是层 ...
- [Ext JS 4] MVC 应用程序框架
前言 大型客户端应用程序总是很难编写,很难组织和很难维护.随着功能的增加和更多的开发人员加入项目,对项目的控制也越来越困难了.Ext JS 4 提供了一个新的应用程序框架帮助组织代码. 模型 - 一组 ...
- 【Mac 10.13.0】安装 libimobiledevice,提示报错:warning: unable to access '/Users/lucky/.config/git/attributes': Permission denied解决方案
打开终端,执行命令: 1.sudo chown -R XXX /usr/local (XXX表示当前用户名) 2.ruby -e "$(curl -fsSL https://raw.git ...
- mouseover与mouseenter,mouseout与mouseleave的区别
mouseover与mouseenter 不论鼠标指针穿过被选元素或其子元素,都会触发 mouseover 事件.只有在鼠标指针穿过被选元素时,才会触发 mouseenter 事件. mouseout ...
- Unity3D 向量运算
写在前面的话,前两天有个朋友在QQ上问我 如何获取主角面朝方向一定区域中的敌人对象.这个命题看似简单,其实里面蕴含了很多数学方面的东西.今天刚好有时间我就彻底的把这个疑问写在博客中.希望可以帮助到他. ...