【问题描述】

小美有 n 个点 m 条边。

让你给每个点一个正整数编号。

每条边有两个属性,相连的两个点的编号的 GCD 和 LCM。

题目保证整张图连通。

让你构造出一个编号。

【输入格式】

从文件 math.in 中读入数据。

第一行两个正整数 n 和 m。

接下去m行每行 4 个正整数 xi,yi,gcdi,lcmi。

【输出格式】

输出到文件 math.out 中。

如果是有解:

第一行一行 YES。

第二行 n 个数字表示编号。

否则输出一行NO。

【样例】

【样例输入】

1 0

【样例输出】

YES 1

【样例输入】

2 1

1 2 1 3

【样例输出】

YES 1 3

【样例输入】

3 2

3 2 1 2

3 1 1 10

【样例输出】

YES 5 1 2

【样例输入】

2 1

1 2 3 7

【样例输出】

NO

【数据规模】

对于\(100\%\)的数据\(2\le n \le 100,n-1 \le m \le n*(n-1)/2,1\le gcdi, lcmi \le 10^6\)。

题解

这题其实就是一道暴力题,是根据\([a, b] \times (a, b) = a\times b\)。已知\(a\times b\),暴力枚举\(a\),然后把整张图dfs一遍判断其正确性即可。

不得不说:大力出奇迹!

另外,记得开long long。

题目加强版:CF 60C

代码

#include <cctype>
#include <cstdio>
#include <cstring> typedef long long LL;
#define int long long #define dd c = getchar()
inline void read(int& x)
{
x = 0;
char dd;
bool f = false;
for(; !isdigit(c); dd)
if(c == '-')
x = -x;
for(; isdigit(c); dd)
x = (x<<1) + (x<<3) + (c^48);
if(f) x = -x;
}
#undef dd inline int gcd(int __n, int __m)
{
while (__n)
{
int __t = __m % __n;
__m = __n;
__n = __t;
}
return __m;
} const int maxn = 105; int n, m; int ans[maxn]; struct edge
{
LL cheng;
int t;
int ne;
int gcdd;
} e[maxn*maxn]; int first[maxn];
bool vis[maxn]; int mm;
inline void add_edge(int f, int t, LL cheng, int gcdd)
{
e[++mm].ne = first[f];
e[mm].t = t;
e[mm].cheng = cheng;
e[mm].gcdd = gcdd;
first[f] = mm; e[++mm].ne = first[t];
e[mm].t = f;
e[mm].cheng = cheng;
e[mm].gcdd = gcdd;
first[t] = mm;
} inline bool dfs(int n, int last)//dfs检验答案正确性
{
vis[n] = true;
for(int i = first[n]; i; i = e[i].ne)
{
int to = e[i].t;
if(to == last) continue;
if(vis[to])
{
if((ans[to]*ans[n] != e[i].cheng) || (gcd(ans[to], ans[n]) != e[i].gcdd))
return false;
}
else
{
ans[to] = e[i].cheng / ans[n];
if(gcd(ans[to], ans[n]) != e[i].gcdd)
return false;
if(!dfs(to, n))
return false;
}
}
return true;
} inline void print()
{
puts("YES");
for(int i = 1; i <= n; ++i)
printf("%lld ", ans[i]);
} inline void search()
{
vis[1] = true;
if(!first[1])
{
puts("NO");
return;
}
LL k = e[first[1]].cheng;
for(int i = 1; i*i <= k; ++i)//暴力枚举所有可能情况
if(!(k%i))
{
ans[1] = i;
memset(vis, 0, sizeof(vis));
if(dfs(1, 0))
{
print();
return;
}
ans[1] = k/i;
memset(vis, 0, sizeof(vis));
if(dfs(1, 0))
{
print();
return;
}
}
puts("NO");
} signed main()
{
freopen("math.in", "r", stdin);
freopen("math.out", "w", stdout);
scanf("%lld%lld", &n, &m);
if(n == 1)
{
puts("YES\n1");
return 0;
}
for(int i = 1; i <= m; ++i)
{
int f, t, gcdd, lcmm;
read(f), read(t), read(gcdd), read(lcmm);
add_edge(f, t, (LL)(gcdd*lcmm), gcdd);
}
search();
return 0;
}

20180520模拟赛T1——math的更多相关文章

  1. 【洛谷比赛】[LnOI2019]长脖子鹿省选模拟赛 T1 题解

    今天是[LnOI2019]长脖子鹿省选模拟赛的时间,小编表示考的不怎么样,改了半天也只会改第一题,那也先呈上题解吧. T1:P5248 [LnOI2019SP]快速多项式变换(FPT) 一看这题就很手 ...

  2. 20180610模拟赛T1——脱离地牢

    Description 在一个神秘的国度里,年轻的王子Paris与美丽的公主Helen在一起过着幸福的生活.他们都随身带有一块带磁性的阴阳魔法石,身居地狱的魔王Satan早就想着得到这两块石头了,只要 ...

  3. 20180520模拟赛T3——chess

    [问题描述] 小美很喜欢下象棋. 而且她特别喜欢象棋中的马. 她觉得马的跳跃方式很独特.(以日字格的方式跳跃) 小芳给了小美一张很大的棋盘,这个棋盘是一个无穷的笛卡尔坐标. 一开始\(time=0\) ...

  4. NOIP欢乐模拟赛 T1 解题报告

    小澳的方阵 (matrix.cpp/c/pas) [题目描述] 小澳最近迷上了考古,他发现秦始皇的兵马俑布局十分有特点,热爱钻研的小澳打算在电脑上还原这个伟大的布局. 他努力钻研,发现秦始皇布置兵马俑 ...

  5. [模拟赛] T1 高级打字机

    Description 早苗入手了最新的高级打字机.最新款自然有着与以往不同的功能,那就是它具备撤销功能,厉害吧. 请为这种高级打字机设计一个程序,支持如下3种操作: 1.T x:在文章末尾打下一个小 ...

  6. 2019.2.25 模拟赛T1【集训队作业2018】小Z的礼物

    T1: [集训队作业2018]小Z的礼物 我们发现我们要求的是覆盖所有集合里的元素的期望时间. 设\(t_{i,j}\)表示第一次覆盖第i行第j列的格子的时间,我们要求的是\(max\{ALL\}\) ...

  7. [NOIP2018校模拟赛]T1 阶乘

    题目: 描述 有n个正整数a[i],设它们乘积为p,你可以给p乘上一个正整数q,使p*q刚好为正整数m的阶乘,求m的最小值. 输入 共两行. 第一行一个正整数n. 第二行n个正整数a[i]. 输出 共 ...

  8. [NOIP2018校模拟赛]T1聚会 party

    题目链接: 聚会 分析: 设每个点到1号点的距离为dist_{i},每个点的权值为x_{i},目标点到1号点的距离为dist,权值为x,那么对于每一次查询,我们讨论三种情况: ① 目标家庭在区间左边( ...

  9. 【2019.8.15 慈溪模拟赛 T1】插头(plugin)(二分+贪心)

    二分 首先,可以发现,最后的答案显然满足可二分性,因此我们可以二分答案. 然后,我们只要贪心,就可以验证了. 贪心 不难发现,肯定会优先选择能提供更多插座的排插,且在确定充电器个数的情况下,肯定选择能 ...

随机推荐

  1. BitSet源码

    public class BitSet1 implements Cloneable, java.io.Serializable { // >>>左边补0, << 右边补0 ...

  2. [转帖] db file sequential read及优化

    http://blog.itpub.net/12679300/viewspace-1185623/ db file sequential read及优化 原创 Oracle 作者:wzq609 时间: ...

  3. spring aop 一个挡板例子

    import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.ann ...

  4. ElasticSearch 线程池类型分析之 ExecutorScalingQueue

    ElasticSearch 线程池类型分析之 ExecutorScalingQueue 在ElasticSearch 线程池类型分析之SizeBlockingQueue这篇文章中分析了ES的fixed ...

  5. TP5配置隐藏入口index.php文件

    隐藏的index.php PS:这里说的入口文件指的是公共/ index.php文件,配置文件就在这个目录下 可以去掉URL地址里面的入口文件index.php,但是需要额外配置WEB服务器的重写规则 ...

  6. 解决source insight 4.0 不识别.cc文件的问题

    Options -> File Type Options, File Filter 中加入,*.cc 参考了C++ Primer Plus第五版中文版 P8 C++实现 源代码的扩展名 UNIX ...

  7. 乘法器——Wallace树型乘法器

    博主最近在看乘法器相关的知识,发现现在用的比较多的是booth编码的乘法器和Wallace树型乘法器,当然两者并不是互斥的关系,他们也可以结合使用.在这里给大家介绍一下Wallace树型乘法器,希望能 ...

  8. [翻译] InfluxDB 存储机制解析

    原文地址: https://medium.com/dataseries/analysis-of-the-storage-mechanism-in-influxdb-b84d686f3697 TODO

  9. FusionInsight大数据开发---Hive应用开发

    Hive应用开发 了解Hive的基本架构原理 掌握JDBC客户端开发流程 了解ODBC客户端的开发流程 了解python客户端的开发流程 了解Hcatalog/webHcat开发接口 掌握Hive开发 ...

  10. python中 jsonchema 与 shema 效率比较

    前面几篇文章总结了python中jsonschema与schema的用法,这里测试一下两者的效率: 上代码: import time from jsonschema import validate, ...