D - Beautiful Graph (深搜)
这个题深搜容易解决,结果用了广搜,动手之前还是要想清楚,然后自己的代码写错的情况下,没有重写,而是在原有的基础上,进行修改,结果有个判定的初始化条件放错位置,浪费了一个小时。。。
就是给一个无向图,任务时给点附上1,2,或者3,使得每条边相连的两个顶点数字之和位基数,求一共有多少种方案。
本来准备放自己的代码,算了,还是官方题解比较简洁,有那个for写的很短,而且我写的有好几个函数,检查起来跳来跳去的,以后短的代码还是尽量写在主函数吧
#include <bits/stdc++.h> using namespace std; const int N = int(3e5) + ;
const int MOD = ; int n, m;
vector <int> g[N];
int p2[N];
int cnt[];
int col[N];
bool bad; void dfs(int v, int c){
col[v] = c;
++cnt[c];
for(auto to : g[v]){
if(col[to] == -) dfs(to, - c);
if((col[v] ==col[to]) )
bad = true;
}
}
int main() {
p2[] = ;
for(int i = ; i < N; ++i)
p2[i] = ( * p2[i - ]) % MOD; int tc;
scanf("%d", &tc);
while(tc--){
scanf("%d%d", &n, &m);
for(int i = ; i < n; ++i)
g[i].clear(); for(int i = ; i < m; ++i){
int u, v;
scanf("%d %d", &u, &v);
--u, --v;
g[u].push_back(v);
g[v].push_back(u);
} int res = ;
for(int i = ; i < n; ++i) col[i] = -;
for(int i = ; i < n; ++i){
if(col[i] != -) continue;
bad = false;
cnt[] = cnt[] = ;
dfs(i, );
if(bad){
puts("");
break;
}
int cur = (p2[cnt[]] + p2[cnt[]]) % MOD;
res = (res * 1LL * cur) % MOD;
} if(!bad) printf("%d\n", res);
} return ;
}
D - Beautiful Graph (深搜)的更多相关文章
- 2016弱校联盟十一专场10.3---Similarity of Subtrees(深搜+hash、映射)
题目链接 https://acm.bnu.edu.cn/v3/problem_show.php?pid=52310 problem description Define the depth of a ...
- DFS-BFS(深搜广搜)原理及C++代码实现
深搜和广搜是图很多算法的基础,很多图的算法都是从这两个算法中启发而来. 深搜简单地说就是直接一搜到底,然后再回溯,再一搜到底,一直如此循环到没有新的结点. 广搜简单地说就是一层一层的搜,像水的波纹一样 ...
- HDU--杭电--1195--Open the Lock--深搜--都用双向广搜,弱爆了,看题了没?语文没过关吧?暴力深搜难道我会害羞?
这个题我看了,都是推荐的神马双向广搜,难道这个深搜你们都木有发现?还是特意留个机会给我装逼? Open the Lock Time Limit: 2000/1000 MS (Java/Others) ...
- 利用深搜和宽搜两种算法解决TreeView控件加载文件的问题。
利用TreeView控件加载文件,必须遍历处所有的文件和文件夹. 深搜算法用到了递归. using System; using System.Collections.Generic; using Sy ...
- 2016弱校联盟十一专场10.2---Around the World(深搜+组合数、逆元)
题目链接 https://acm.bnu.edu.cn/v3/problem_show.php?pid=52305 problem description In ICPCCamp, there ar ...
- 2015暑假多校联合---Cake(深搜)
题目链接:HDU 5355 http://acm.split.hdu.edu.cn/showproblem.php?pid=5355 Problem Description There are m s ...
- 深搜+回溯 POJ 2676 Sudoku
POJ 2676 Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17627 Accepted: 8538 ...
- 深搜+DP剪枝 codevs 1047 邮票面值设计
codevs 1047 邮票面值设计 1999年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description ...
- 【wikioi】1049 棋盘染色(迭代深搜)
http://www.wikioi.com/problem/1049/ 这题我之前写没想到迭代加深,看了题解,然后学习了这种搜索(之前我写的某题也用过,,但是不懂专业名词 囧.) 迭代加深搜索就是限制 ...
随机推荐
- tomcat 开机自启
d /usr/lib/systemd/system touch tomcat.service chmod 777 tomcat.service vi tomcat.service [Unit]Desc ...
- JS-find、filter、forEach、map
js这四个方法不会对空数组进行检测,也不会改变原始数组 find()方法主要用来返回数组中符合条件的第一个元素(没有的话,返回undefined) //语法 array.find(function(v ...
- 将jar包安装到本地Maven仓库
1.安装jar包到本地仓库 mvn install:install-file -Dfile=F:\jiefu\lib\alipay-sdk-java20170324180803.jar -Dgroup ...
- 页面阻止某个按键的键盘事件(event.stopPropagation与event.preventDefault的区别及如何兼容IE8)
http://blog.csdn.net/rocklee/article/details/53160656
- 【PAT甲级】1054 The Dominant Color (20 分)
题意: 输入两个正整数M和N(M<=800,N<=600),分别代表一张图片的宽度和高度,接着输入N行每行包括M个点的颜色编号,输出这张图片主导色的编号.(一张图片的主导色占据了一半以上的 ...
- GitHub vs. Bitbucket 不只是功能不同
https://www.oschina.net/translate/bitbucket-vs-github-its-more-than-just-features 让我们回到2005年,Bitkeep ...
- print、println、printf的区别(转载)
printf主要是继承了C语言的printf的一些特性,可以进行格式化输出 print就是一般的标准输出,但是不换行 println和print基本没什么差别,就是最后会换行 System ...
- 使用maven搭建web项目
在pom.xml中添加java ee相关的三个依赖包:<scope> jar的有效范围 provided 表示编译期生效,不会打包发布到 tomcat 中 <properties&g ...
- SpringBoot爬虫小说阅读网站,定时更新小说和抓取功能
SpringBoot 小说爬虫搭建阅读网站 通过jsoup采集数据到mysql数据.redis作为缓存框架,减轻服务器压力.部署在linux,网站UI套纵横中文网! http://47.107.116 ...
- ubuntu 更改pip默认源
mkdir ~/.pip vim ~/.pip/pip.conf [global] timeout = 6000 https://pypi.tuna.tsinghua.edu.cn/simple 保存 ...