(并查集+DFS) poi guilds

Guilds

Memory limit: 64 MB

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.

Input

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 -th road connects the towns  and . Every pair of towns is (directly) connected by at most one road. The roads do not cross - meeting only in towns - but may lead through tunnels and overpasses.

Output

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 .

Example

For the input data:

7 8
1 2
3 4
5 4
6 4
7 4
5 6
5 7
6 7

the correct result is:

TAK
K
S
K
S
K
K
N

The towns in which an office of The Tailors Guild should open are marked with circles, whereas those in which an office of The Sewers Guild should open are marked with rhombi.

Task author: Marcin Pilipczuk.

题目大意

给你一张图,并对图中的一些点进行红黑染色,要求: 
1、对于每个红色的点一定有黑色点与其相连, 
2、对于每个黑色的点一定有红色点与其相连, 
3、对于每个未染色的点一定有红色点和黑色点与其相连。 
判断这个图是否有一个染色的可行解,若有,输出一个解

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
vector<int> e[200005];
int n,m,fa[200005],siz[200005],col[200005];
bool vis[200005];
int find(int x)
{
    if(x!=fa[x])
        fa[x]=find(fa[x]);
    return fa[x];
}
void dfs(int u,int dep)
{
    vis[u]=1;
    if(dep%2==1)
        col[u]=1;
    else
        col[u]=2;
    for(int i=0;i<e[u].size();i++)
    {
        int v=e[u][i];
        if(vis[v]) continue;
        dfs(v,dep+1);
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        fa[i]=i,siz[i]=1;
    for(int i=1;i<=m;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        e[x].push_back(y);
        e[y].push_back(x);
        int fx,fy;
        fx=find(x),fy=find(y);
        if(fx!=fy)
        {
            fa[fx]=fy;
            siz[fy]+=siz[fx];
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(siz[find(i)]==1)
        {
            printf("NIE");
            return 0;
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(fa[i]==i)
            dfs(i,0);
    }
    printf("TAK\n");
    for(int i=1;i<=n;i++)
    {
        if(!col[i])
            printf("N\n");
        else if(col[i]==1)
            printf("K\n");
        else
            printf("S\n");
    }
    return 0;
}

  

时间: 2024-10-07 04:17:40

(并查集+DFS) poi guilds的相关文章

BZOJ 3562: [SHOI2014]神奇化合物 并查集+dfs

点击打开链接 注意到20w条边,但是询问只有1w,所以有很多边是从头到尾不变的. 首先离线处理,将从未删除的边缩点,缩点后的图的点数不会超过2w,对于每一次add或者delete,直接dfs看是否能从a走到b,然后维护一个ans. 数据不强,不然这种复杂度起码要跑10s.. #include<stdio.h> #include<iostream> #include<algorithm> #include<cstring> using namespace st

Codeforces 455C Civilization(并查集+dfs)

题目链接:Codeforces 455C Civilization 题目大意:给定N,M和Q,N表示有N个城市,M条已经修好的路,修好的路是不能改变的,然后是Q次操作,操作分为两种,一种是查询城市x所在的联通集合中,最长的路为多长.二是连接两个联通集合,采用联通之后最长路最短的方案. 解题思路:因为一开时的图是不可以改变的,所以一开始用dfs处理出各个联通集合,并且记录住最大值,然后就是Q次操作,用并查集维护,注意因为联通的时候要采用最长路径最短的方案,所以s的转移方程变为s = max(s,

1021. Deepest Root (25) 并查集&amp;&amp;DFS

1021. Deepest Root (25) 时间限制 1500 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A graph which is connected and acyclic can be considered a tree. The height of the tree depends on the selected root. Now you are supposed to find the root t

CCF 201512-4 送货 (并查集+DFS,欧拉路)

问题描述 为了增加公司收入,F公司新开设了物流业务.由于F公司在业界的良好口碑,物流业务一开通即受到了消费者的欢迎,物流业务马上遍及了城市的每条街道.然而,F公司现在只安排了小明一个人负责所有街道的服务. 任务虽然繁重,但是小明有足够的信心,他拿到了城市的地图,准备研究最好的方案.城市中有n个交叉路口,m条街道连接在这些交叉路口之间,每条街道的 首尾都正好连接着一个交叉路口.除开街道的首尾端点,街道不会在其他位置与其他街道相交.每个交叉路口都至少连接着一条街道,有的交叉路口可能只连接着一 条或两

Deepest Root (并查集+DFS树的深度)

A graph which is connected and acyclic can be considered a tree. The height of the tree depends on the selected root. Now you are supposed to find the root that results in a highest tree. Such a root is called the deepest root. Input Specification: E

2018 计蒜之道复赛 贝壳找房魔法师顾问(并查集+dfs判环)

贝壳找房在遥远的传奇境外,找到了一个强大的魔法师顾问.他有 22 串数量相同的法力水晶,每个法力水晶可能有不同的颜色.为了方便起见,可以将每串法力水晶视为一个长度不大于 10^5105,字符集不大于 10^5105 的字符串.现在魔法师想要通过一系列魔法使得这两个字符串相同.每种魔法形如 (u,\ v),\ u,\ v \le 10^5(u, v), u, v≤105,可以将一个字符 uu改成一个字符 vv,并且可以使用无限次.出于种种原因,魔法师会强行指定这两个串能否进行修改. 若失败输出 -

F2 - Spanning Tree with One Fixed Degree - 并查集+DFS

这道题还是非常有意思的,题意很简单,就是给定一个图,和图上的双向边,要求1号节点的度(连接边的条数)等于K,求这棵树的生成树. 我们首先要解决,如何让1号节点的度时为k的呢???而且求的是生成树,意思是不是所有边都会选择.那么我们如何选择才能保证1号节点有K个度呢???这里就要考虑联通分量的问题了,我们刨除1号点,那么联通分量的个数,就是我们让图联通的最小个数,因此我们需要用并查集,把点分在不同的联通块内部. 再考虑我们每个联通块,至少需要1条连接1号点的边.不够K再添加连接1号点的边. 然后考

【LCA】Tarjan离线算法(并查集+dfs)模板

vector <int> Q[N]; int Find(int x) { if(x != fa[x]) return fa[x] = Find(fa[x]); return x; } void Union(int x, int y) { int fx = Find(x), fy = Find(y); if(fy != fx) fa[fy] = fx; } void dfs(int u) { anc[u] = u; for(int i = head[u]; ~i; i = e[i].next)

tarjan求lca :并查集+dfs

//参考博客 https://www.cnblogs.com/jsawz/p/6723221.html#include<bits/stdc++.h> using namespace std; #define maxn 420000 struct Query{int to,nxt,lca;}q[maxn]; struct Edge{int to,nxt;}edge[maxn<<1]; int n,m,p,x,y,tot_e,tot_q,head_q[maxn],head_e[maxn