码迷,mamicode.com
首页 > 其他好文 > 详细

洛谷 P3496 [POI2010]GIL-Guilds

时间:2018-04-08 18:14:18      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:names   ase   memset   lin   ons   input   gil   blank   lan   

题目描述

King Byteasar faces a serious matter.

Two competing trade organisations, The Tailors Guild and The Sewers Guild asked, at the same time, for permissions to open their offices in each town of the kingdom.

There are 技术分享图片 towns in Byteotia.

Some of them are connected with bidirectional roads.

Each of the guilds postulate that every town should:

have an office of the guild, or be directly connected to another town that does.

The king, however, suspects foul play. He fears that if there is just a single town holding the offices of both guilds, it could lead to a clothing cartel.

For this reason he asks your help.

给一张无向图,要求你用黑白给点染色,且满足对于任意一个黑点,至少有一个白点和他相邻;对于任意一个白点,至少有一个黑点与他相邻,问能否成功

输入输出格式

输入格式:

 

Two integers, 技术分享图片 and 技术分享图片 (技术分享图片技术分享图片), are given in the first line of the standard input. These denote the number of towns and roads in Byteotia, respectively.

The towns are numbered from 技术分享图片 to 技术分享图片.

Then the roads are given as follows: the input line no. 技术分享图片 describes the 技术分享图片-th road; it holds the numbers 技术分享图片 and 技术分享图片 (技术分享图片技术分享图片), denoting that the ![](http://main.edu.…

 

输出格式:

 

Your program should print out one word in the first line of the standard output:

TAK (yes in Polish) - if the offices can be placed in towns according to these rules, or NIE (no in Polish) - in the opposite case.

If the answers is TAK, then the following 技术分享图片 lines should give an exemplary placement of the offices. Thus the line no. 技术分享图片should hold:

the letter K if there should be an office of The Tailors Guild in the town 技术分享图片, or the letter S if there should be an office of The Sewers Guild in the town 技术分享图片, or the letter N if there should be no office in the town 技术分享图片.

 

输入输出样例

输入样例#1: 复制
7 8
1 2
3 4
5 4
6 4
7 4
5 6
5 7
6 7
输出样例#1: 复制
TAK
K
S
K
S
K
K
N
思路:可以得到一个结论,只要没有度数为0的点就一定可以。
然后宽搜染色就可以了。
技术分享图片
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 500010
using namespace std;
queue<int>que;
int n,m,tot;
int fa[MAXN],col[MAXN],into[MAXN];
int to[MAXN*2],net[MAXN*2],head[MAXN];
int find(int x){
    if(fa[x]==x)    return fa[x];
    else return fa[x]=find(fa[x]);
}
void add(int u,int v){
    to[++tot]=v;net[tot]=head[u];head[u]=tot;
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)    fa[i]=i;
    for(int i=1;i<=m;i++){
        int x,y;scanf("%d%d",&x,&y);
        add(x,y);add(y,x);into[x]++;into[y]++;
        int dx=find(x);int dy=find(y);fa[dy]=dx;
    }
    for(int i=1;i<=n;i++)
        if(into[i]==0){ printf("NIE\n");return 0;}
    memset(col,-1,sizeof(col));
    for(int i=1;i<=n;i++)
        if(fa[i]==i){ que.push(i);col[i]=1; }
    while(!que.empty()){
        int now=que.front();
        que.pop();
        for(int i=head[now];i;i=net[i])
            if(col[to[i]]==-1){
                col[to[i]]=(col[now]==1?0:1);
                que.push(to[i]);
            }
    }
    printf("TAK\n");
    for(int i=1;i<=n;i++)
        if(col[i]==1)    printf("K\n");
        else    printf("S\n");
}

 

 

洛谷 P3496 [POI2010]GIL-Guilds

标签:names   ase   memset   lin   ons   input   gil   blank   lan   

原文地址:https://www.cnblogs.com/cangT-Tlan/p/8746172.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!