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;
}

 

posted @ 2015-05-18 13:03  qscqesze  阅读(436)  评论(0编辑  收藏  举报