题面

题解

神仙构造题。

分五种情况考虑:

  • 如果存在一个环,那么令环上的点权值为\(1\),其余点权值为\(0\)。
  • 如果存在一个度数大于\(3\)的点,令这个点的权值为\(2\),和它相邻的点权值为\(1\),否则权值为\(0\)。
  • 如果存在两个度数等于\(3\)的点,令这两个点的路径上点的权值为\(2\),其余的点权值为\(1\)。
  • 如果只有一个点度数为\(3\),那么这张图一定是这个点伸出三条链,设三条链的长度分别为\(l_1, l_2, l_3\),那么有解当且仅当\(\frac 1{l_1 + 1} + \frac 1{l_2 + 1} + \frac 1{l_3 + 1} \leq 1\)。
  • 如果没有点度数为\(3\),可以证明一定无解。

于是就可以按照结论欢乐构造了。

代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define file(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout) inline int read()
{
int data = 0, w = 1; char ch = getchar();
while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') w = -1, ch = getchar();
while (ch >= '0' && ch <= '9') data = data * 10 + (ch ^ 48), ch = getchar();
return data * w;
} const int maxn(1e5 + 10);
struct edge { int next, to; } e[maxn << 1];
int head[maxn], e_num, n, m, vis[maxn], d[maxn], deg[maxn];
std::vector<int> vec[3];
inline void add_edge(int from, int to)
{
e[++e_num] = (edge) {head[from], to};
head[from] = e_num, ++deg[to];
} void Set(int x)
{
vis[x] = 0; if (!d[x]) d[x] = 1;
for (int i = head[x]; i; i = e[i].next)
if (vis[e[i].to]) Set(e[i].to);
} int findCir(int x, int f)
{
vis[x] = 1;
for (int i = head[x]; i; i = e[i].next)
if (!vis[e[i].to]) { if (findCir(e[i].to, x)) return 1; }
else if (e[i].to != f) return 1;
return 0;
} int findDeg(int x, int f)
{
if (f && deg[x] == 3) return d[x] = 2, 1;
for (int i = head[x]; i; i = e[i].next)
if (e[i].to != f) if (findDeg(e[i].to, x)) { d[x] = 2; return 1; }
return 0;
} void dfs(int x, int f, std::vector<int> &v)
{
v.push_back(x);
for (int i = head[x]; i; i = e[i].next)
if (e[i].to != f) dfs(e[i].to, x, v);
} int main()
{
for (int T = read(); T--; )
{
n = read(), m = read();
memset(head, 0, (n + 1) << 2), memset(vis, 0, (n + 1) << 2);
memset(d, 0, (n + 1) << 2), memset(deg, 0, (n + 1) << 2), e_num = 0;
for (int i = 1, a, b; i <= m; i++)
a = read(), b = read(), add_edge(a, b), add_edge(b, a);
int f = 0;
for (int i = 1; !f && i <= n; i++)
if (!vis[i]) if ((f = findCir(i, 0))) Set(i);
for (int i = 1; !f && i <= n; i++)
if (deg[i] > 3) { d[i] = 2, Set(i), f = 1; }
for (int i = 1; !f && i <= n; i++)
if (deg[i] == 3) if ((f = findDeg(i, 0)))
memset(vis, 1, sizeof vis), Set(i);
for (int i = 1; !f && i <= n; i++) if (deg[i] == 3)
{
int cur = 0; vec[0].clear(), vec[1].clear(), vec[2].clear();
for (int j = head[i]; j; j = e[j].next) dfs(e[j].to, i, vec[cur++]);
if (vec[0].size() > vec[1].size()) std::swap(vec[0], vec[1]);
if (vec[0].size() > vec[2].size()) std::swap(vec[0], vec[2]);
if (vec[1].size() > vec[2].size()) std::swap(vec[1], vec[2]);
if (vec[1].size() == 1 || (vec[0].size() == 1 && vec[1].size() == 2
&& vec[2].size() < 5)) continue;
f = 1;
if (vec[2].size() >= 5)
{
d[i] = 6, d[vec[0][0]] = 3, d[vec[1][0]] = 4, d[vec[1][1]] = 2;
for (int j = 0; j < 5; j++) d[vec[2][j]] = 5 - j;
}
else
{
d[i] = (vec[0].size() + 1) * (vec[1].size() + 1) * (vec[2].size() + 1);
for (int j = 0; j < 3; j++) for (int k = 0; k < (int) vec[j].size(); k++)
d[vec[j][k]] = d[i] / (vec[j].size() + 1) * (vec[j].size() - k);
}
}
if (!f) { puts("NO"); continue; } puts("YES");
for (int i = 1; i <= n; i++) printf("%d%c", d[i], " \n"[i == n]);
}
return 0;
}

CF830E Perpetual Motion Machine的更多相关文章

  1. Thinking Clearly about Performance

    http://queue.acm.org/detail.cfm?id=1854041 The July/August issue of acmqueue is out now acmqueue is ...

  2. unity5之代码创建状态机,玩的666

    http://blog.csdn.net/litaog00/article/details/50483189 最近做项目的时候用到了状态机,网上搜了一下帖子,大部分都是简单介绍使用方法的,讲解的详细的 ...

  3. Machine and Deep Learning with Python

    Machine and Deep Learning with Python Education Tutorials and courses Supervised learning superstiti ...

  4. Motion control encoder extrapolation

    Flying Saw debug Part1 Encoder extrapolation Machine introduction A tube cutting saw, is working for ...

  5. booklist for machine learning

    Recommended Books Here is a list of books which I have read and feel it is worth recommending to fri ...

  6. [C5] Andrew Ng - Structuring Machine Learning Projects

    About this Course You will learn how to build a successful machine learning project. If you aspire t ...

  7. Pattern Recognition and Machine Learning-02-1.0-Introduction

    Introduction The problem of searching for patterns in data is a fundamental one and has a long and s ...

  8. New Machine Learning Server for Deep Learning in Nuke(翻译)

    最近一直在开发Orchestra Pipeline System,歇两天翻译点文章换换气.这篇文章是无意间看到的,自己从2015年就开始关注机器学习在视效领域的应用了,也曾利用碎片时间做过一些算法移植 ...

  9. 【Machine Learning】KNN算法虹膜图片识别

    K-近邻算法虹膜图片识别实战 作者:白宁超 2017年1月3日18:26:33 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现的深入理解.本系列文章是作者结 ...

随机推荐

  1. 【转载】C#中List集合使用LastOrDefault方法查找出最后一个符合条件的元素

    在C#的List集合中,FirstOrDefault方法一般用来查找List集合中第一个符合条件的对象,如果未查到则返回相应默认值.其实如果要查找最后一个符合条件的List集合元素对象,可以使用Las ...

  2. 浅谈JS中 var let const 变量声明

    浅谈JS中 var let const 变量声明 用var来声明变量会出现的问题: 1. 允许重复的变量声明:导致数据被覆盖 2. 变量提升:怪异的数据访问.闭包问题 3. 全局变量挂载到全局对象:全 ...

  3. python day 18: thinking in UML与FTP作业重写

    目录 python day 18 1. thinking in UML读书小感 2. FTP作业重写 2.1 软件目录结构 2.2 FTPClient端脚本 2.3 FTPServer端脚本 pyth ...

  4. Ext.create使用(上)

    本文介绍前两种使用方法: 通过full name, alias 或者 alternate name实例化一个类 // 别名 // alias var window = Ext.create('widg ...

  5. react-router4的使用备注

    1.安装 react-router是核心库,在项目中不需要安装,web开发只需安装react-router-dom.native开发安装react-router-native. 2.url参数携带与获 ...

  6. Python爬虫系列:五、正则表达式

    1.了解正则表达式 正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑. 正则表达式 ...

  7. JS中的this、apply、call、bind(经典面试题)

    1.什么是this 在JavaScript中this可以是全局对象.当前对象或者任意对象,这完全取决于函数的调用方式,this 绑定的对象即函数执行的上下文环境(context). 为了帮助理解,让我 ...

  8. Linux/Raspbian 每个目录用途说明

    本文转自无聊小博,很多刚接触树莓派/Linux 的同学会在给树莓派安装.卸载.配置软件时,软件和配置文件等存放在哪儿产生疑惑.也会遇到诸如“磁盘分区”.U盘挂载等涉及到的目录路径问题.Linux 的目 ...

  9. SpringMVC的全局异常处理

    @ControllerAdvice的使用 我们都知道做项目一般都会有全局异常统一处理的类,那么这个类在Spring中可以用@ControllerAdvice来实现. @ControllerAdvice ...

  10. javax validation--参数基础校验

    Bean Validation 1.1当前实现是hibernate validator 5,且spring4才支持.接下来我们从以下几个方法讲解Bean Validation 1.1,当然不一定是新特 ...