SRM 452 DIV2 Problems 1000
来源:互联网 发布:不喜欢穿内裤体验知乎 编辑:程序博客网 时间:2024/04/30 11:29
SRM 452 DIV2
Problems 1000
题目:
http://www.topcoder.com/stat?c=problem_statement&pm=10572&rd=13906
解答:
If there exist loop formed by required edges or node with degree > 2, then the answer is 0.
If there are no loops and no nodes with degree > 2, then the graph will consist of S separate nodes and C chains.
We need to count the number of ways to concatenate them to form a single path. We have (S+C)! ways to order them,
and for each chain we have 2 orientations, so the answer is (S+C)!*2^C
解答非常好理解,问题为判断是否存在环的形式。
#include <iostream>
#include <vector>
#include <string>
using namespace std;
bool vis[100];
int d[100];
vector <string> road;
int n;
class HamiltonPath
{
void dfs(int now)
{
vis[now]=true;
for (int i=0;i<n;i++)
if ((road[now][i]=='Y')&&(!vis[i])) dfs(i);
}
public:
int countPaths(vector <string> roads)
{
road=roads;
memset(vis,false,sizeof(vis));
n=roads.size();
for (int i=0;i<n;i++)
{
int s=0;
for (int j=0;j<n;j++)
if (roads[i][j]=='Y') s++;
if (s>2) return 0;
d[i]=s;
}
int tot1=0,tot2=0;
for (int i=0;i<n;i++)
if ((!vis[i])&&(d[i]<2))
{
dfs(i);
if (d[i]==0) tot1++;
else tot2++;
}
for (int i=0;i<n;i++)
if (!vis[i]) return 0;
long long ans=1;
for (long long i=0;i<tot1+tot2;i++) ans=(ans*(i+1)) % 1000000007;
for (int i=0;i<tot2;i++)
ans=(ans*2) % 1000000007;
return ans;
}
};
一个很精彩的答案,利用深度优先搜索,同时只有度数为1 和0 的点才作为深度优先搜索的起始点,所以当存在内环的时候,没有一个
点有资格成为搜索起始点,然后再判断是否遍历了整个点集空间。思想非常简单,代码书写也非常好!
一定要牢记这句!! You can find cycles using DFS.
- SRM 452 DIV2 Problems 1000
- SRM 397 DIV2 [1000]
- SRM 572 div2 1000
- SRM 649 div2 1000
- TC SRM 557 DIV2 1000
- SRM 598 DIV2 1000 FoxAndFencingEasy
- SRM 606 DIV2 1000 EllysCandyGame
- Topcoder SRM 635 div2 1000
- Topcoder SRM 636 div2 1000
- SRM 682 Div2 1000 SubtreesCounting
- SRM 570 DIV2 1000 CentaurCompany
- SRM 510 DIV2 1000 TheLuckyBasesDivTwo
- SRM 586 DIV2 1000 StringWeightDiv2
- SRM 611 DIV2 1000 ElephantDrinkingEasy
- tc SRM 570 div2 1000 CentaurCompanyDiv2
- TopCoder SRM 634 Div2 Problem 1000 - SpecialStrings
- Topcoder SRM 640 Div2 1000(巧妙数学题)
- srm 653 div2 1000(dp)
- 极限编程(XP,eXtreme Programming)
- Python装饰器
- 如何把安装过程中的数据传到反安装过程中
- java删除文件夹
- 从前
- SRM 452 DIV2 Problems 1000
- Facebook FBML模式下优化研究
- 对于你
- 二进制补码运算公式
- APPLET 和 WEB 事件的交互. 文章出处:DIY部落(http://www.diybl.com/course/1_web/webjs/200877/131922.html)
- 第一篇 csdn 的文章
- ASP.NET中用户输入文本的HTML解析办法
- js中innerHTML与innerTex、outerHTMLt的用法与区别
- eclipse Job