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[20];
//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=0,f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;
}
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("");
}
//**************************************************************************************

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]=1;
    q.push(x);
    while(!q.empty())
    {
        int now=q.front();
        q.pop();
        for(int i=0;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]=1;
            q.push(next);
        }
    }
}
int main()
{
    cnt=10;
    int n=read();
    for(int i=0;i<n-1;i++)
    {
        int x=read(),y=read();
        e[x].push_back(y);
        e[y].push_back(x);
    }
    int flag=0;
    for(int i=1;i<=n;i++)
    {
        if(e[i].size()!=1)
        {
            flag=1;
            a[i].x=1;
            a[i].y=2;
            a[e[i][0]].x=1;
            a[e[i][0]].y=3;
            a[e[i][1]].x=2;
            a[e[i][1]].y=4;
            bfs(i);
            break;
        }
    }
    if(flag==0)
    {
        for(int i=1;i<=n-1;i++)
        {
            a[i].x=i;
            a[i].y=i+1;
        }
        a[n].x=n-1;
        a[n].y=n;
    }
    for(int i=1;i<=n;i++)
        cout<<a[i].x<<" "<<a[i].y<<endl;
}
时间: 2024-10-18 01:20:02

uva 6957 Hyacinth bfs的相关文章

UVA 12130 - Summits(BFS+贪心)

UVA 12130 - Summits 题目链接 题意:给定一个h * w的图,每个位置有一个值,现在要求出这个图上的峰顶有多少个.峰顶是这样定义的,有一个d值,如果一个位置是峰顶,那么它不能走到不大于该峰顶高度 - d的位置,如果满足这个条件下,并且无法走到更高的山峰,那么它就是峰顶 思路:利用贪心的策略,把所有点丢到优先队列,每次取出最高的峰值开始找,进行广搜,搜的过程中记录下最大值的点的个数,如果这个是峰顶,就加上这个数.判断是不是峰顶的方法为,如果广搜过程中,不会找到一个点的能到的最高峰

uva 12130 - Summits(BFS)

题目链接:uva 12130 - Summits 题目大意:给定一个N?M的图,每个位置有一个值.给定D,表示有节点值为G的位置作为起点的话,将不能移动到值小于等于G?D的节点.现在要求找到整个图中所有的峰值点,峰值点的定义是不能移动到比自己节点值大的位置. 解题思路:将每个位置按照权值排序,逐个作为起点进行移动,v[x][y]数组值可以到达x,y的节点的值的最大值,如果起始点可以移动到v[x][y]大于本身的点,则说明该起点不是峰值点. #include <cstdio> #include

UVa 11624 Fire!(BFS 逃离火灾)

题意   n*m的迷宫中有一些着火点  每个着火点上下左右相邻的非墙点下一秒也将成为一个着火点  Joe每秒能向相邻的点移动一步  给你所有着火点的位置和Joe的位置  问Joe逃离这个迷宫所需的最小时间 可以先一遍bfs把每个点的最早着火时间存起来   只有Joe到达该点的时间小于这个时间Joe才能走这个点   只需要对Joe所在的点为起点再来一次bfs就行了   需要注意的是开始可能有多个着火点  我开始以为只有一个着火点被坑了好久 v[i][j]第一遍bfs记录点i,j的最早着火时间  第

UVA 439-Knight Moves(bfs)

Knight Moves Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that

Colour Hash (Uva 704 双向bfs)

Colour Hash Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description This puzzle consists of two wheels. Both wheels can rotate both clock and counter-clockwise. They contain 21 coloured pieces, 10 of which

hdu 2771(uva 12171) Sculpture bfs+离散化

题意: 给出一些边平行于坐标轴的长方体,这些长方体可能相交.也可能相互嵌套.这些长方体形成了一个雕塑,求这个雕塑的整体积和表面积. 题解: 最easy想到直接进行bfs或者dfs统计,但此题的麻烦之处在于求整个雕塑的外表面积和雕塑内部可能出现四个长方体所搭成的空心.空心不能计算到表面积中,可是计算整体积却要计入,于是直接bfs或者dfs不优点理.于是,能够想到直接统计整个雕塑外围的全部小方块.就可以非常方便地求出雕塑地表面积和体积(雕塑地整体积==整个空间地体积-外围想方块的体积),另一点就是因

UVA 1377 Ruler bfs+状压搜索

题目链接:点击打开链接 题意:给定n个刻度.下面是n个刻度. 要构造一个尺子使得上面的n个刻度能直接量出来. 且要满足尺子上的刻度线个数最少,最少的情况下尺子最短. 第一个刻度总为0 题目保证总是存在<7个刻度线的解. 思路: bfs,每次枚举新加一个刻度后,哪些可以直接量出来,用二进制表示,然后暴力搜 剪枝: 1.若加入一个新刻度后并不能多测量给定的n个刻度那么加入的刻度就无效(即加入前和加入后的状态相同) 2.尺子的长度若大于最大刻度则无效 import java.io.PrintWrite

UVA 1601 双向BFS

但是我们还不是很清楚每一次的状态怎么储存?我们可以用一个结构体,将每次的位置存起来,但是这个程序中用了一个更好的储存方法:我们知道最大的格数是16*16个,也就是256个,那么我们转换为二进制表示就是8位数,那么我们可以使用24位的二进制表示啊!然后我们再进行解压缩,所以这就是很神奇的地方! 普通BFS #include<iostream> #include<string> #include<cmath> #include<cstring> #include

uva 567 Risk bfs

#include <cstdio> #include <iostream> #include <algorithm> #include <queue> #include <cstring> using namespace std; int ma[25][25]; int d[25]; int vis[25]; int fun(int x,int y){ queue<int> que; que.push(x); vis[x] = 1;