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 ...
随机推荐
- 剑指offer算法题
数组中只出现一次的数字(一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字): 解法在于位运算中的异或,直接异或可以得到这两个数的异或,按照最后的有效数字位可以 ...
- 安装sudo apt-get install ros-kinetic-desktop-full,报错mv: 无法获取'/var/lib/ni/licenses.xml.dpkg-old' 的文件状态(stat): 没有。。。。
安装sudo apt-get install ros-kinetic-desktop-full,报了一堆错误 mv: 无法获取'/var/lib/ni/licenses.xml.dpkg-old' 的 ...
- 使AJAX可缓存——基于flask
主流浏览器都有缓存机制,主要基于HTTP协议定义的缓存策略.对于一定时间内不发生变动的文档缓存起来,对于下次请求,就可以直接返回缓存的结果.使用缓存有以下好处: 1.减少冗余的数据传输,节省网络流量成 ...
- python tornado 中使用 flash消息闪现
1.html 中引入文件 {% block head %} <link href="/static/common/sweetalert/sweetalert.css" rel ...
- meta标签的使用方法(PC端)
<!DOCTYPE html> <html lang="en"> <head> <!--设定页面使用的字符集--> <meta ...
- Percona XtraDB Cluster(PXC) -集群环境安装
Percona XtraDB Cluster(PXC) ---服务安装篇 1.测试环境搭建: Ip 角色 OS PXC-version 172.16.40.201 Node1 Redhat/C ...
- gentoo emerge unable to sync
gentoo emerge unable to sync Author: Tubo After setting SYNC to customized URL: SYNC="rsync://m ...
- linux中使用vim编译C++程序
Vi三种模式详解 命令行模式 (command mode/一般模式) 任何时候,不管用户处于何种模式,只要按一下“ESC”键,即可使Vi进入命令行模式:我们在shell环境(提示符为$)下输入启动Vi ...
- 不需要打密码的sudo方法
Linux下频繁输入sudo很麻烦.如果你的账户已经是sudoer了,那么编辑/etc/sudoers,将 %sudo ALL=(ALL:ALL) ALL 修改为: %sudo ALL=(ALL) N ...
- 小程序canvasu真机上数据图片不能使用
canvas遇到的坑 1.文字换行 2.真机不能使用网络数据图片(真坑) 点击显示效果我就不写了,你们可以自己加一下 全部代码贴出来 css #preview { width: 100%; heigh ...