POJ 2132
我早上调了一个早上,下午才发现把e=edge[e].next写成edge[e].next了。。。
这题直接DFS,一个剪枝是,当当前的最大质因数是最小公倍数的因数时,不用搜索
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <string.h>
#define LL __int64
using namespace std;
const int N=30;
struct Edge{
int u,v,w;
int next;
}edge[800];
int tot,n;
LL res;
int head[N];
bool vis[N]; LL gcd(LL a,LL b){
if(b==0) return a;
return gcd(b,a%b);
} LL lcm(LL a,LL b){
return a*b/gcd(a,b);
} void dfs(LL g,int u){
if(u==2){
res=lcm(g,res);
return ;
}
int v; LL tmp;
for(int e=head[u];e!=-1;e=edge[e].next){
v=edge[e].v;
if(!vis[v]){
vis[v]=true;
tmp=gcd(g,edge[e].w);
if(res%tmp)
dfs(tmp,v);
vis[v]=false;
}
}
} void addedge(int u,int v,int w){
edge[tot].u=u;
edge[tot].v=v;
edge[tot].w=w;
edge[tot].next=head[u];
head[u]=tot++;
} int main(){
while(scanf("%d",&n)!=EOF){
int tmp;
memset(head,-1,sizeof(head));
tot=0;
res=1;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
scanf("%d",&tmp);
if(tmp>0){
addedge(i,j,tmp);
}
}
}
memset(vis,false,sizeof(vis));
vis[1]=true;
for(int e=head[1];e!=-1;e=edge[e].next){
vis[edge[e].v]=true;
dfs(edge[e].w,edge[e].v);
vis[edge[e].v]=false;
}
printf("%I64d\n",res);
}
return 0;
}
POJ 2132的更多相关文章
- POJ 2132 暴搜OR Floyd
题意: 给你一个邻接矩阵(n<=25)问所有1到2路径的gcd的lcm是多少. 一些经验(WA/TLE的经验): 1. 无脑暴搜 是会TLE的--. 2. 关于精度 dyf神牛说了:long l ...
- poj 3311 Hie with the Pie
floyd,旅游问题每个点都要到,可重复,最后回来,dp http://poj.org/problem?id=3311 Hie with the Pie Time Limit: 2000MS Me ...
- POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7644 Accepted: 2798 ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22286 ...
- POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37427 Accepted: 16288 Descr ...
- POJ 3254. Corn Fields 状态压缩DP (入门级)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9806 Accepted: 5185 Descr ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- POJ 2255. Tree Recovery
Tree Recovery Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11939 Accepted: 7493 De ...
随机推荐
- 好吧,我承认我喜欢这种多个 StoryBoard 组织的方式,学习了!
下面转载内容非常不错.兴许补充从官方文档疏理出来的脉络,确实非常好的使用方法. tid-270505.html"> tid-270505.html">Storyboar ...
- vim产生的.swap文件
转载自 http://ibeyond.blog.51cto.com/1988404/800138 有时候在用vim打开文件时提示类似以下的信息: E325: 注意发现交换文件 ".expor ...
- Hadoop MapReduce编程 API入门系列之倒排索引(二十四)
不多说,直接上代码. 2016-12-12 21:54:04,509 INFO [org.apache.hadoop.metrics.jvm.JvmMetrics] - Initializing JV ...
- Android View事件分发与传递
在Android中,人们主要通过手指与系统交互.Android把所有的touch事件都被封装成MotionEvent来进行处理,其中包括了手指点击的位置,时间等信息.其事件类型主要包括:ACTION_ ...
- url 域名 主机名
1. url = 协议//主机名(包括服务器的计算机名+域名)/路径 https:// i. cnblogs.com /index.html .com是顶级域名,从右向左,每碰到一个".&q ...
- 开发辅助 | 阿里图标库iconfont入门使用
目前大多数的互联网公司,前端开发和UI设计师配合中,针对设计师给图的效果图,前端开发工程师不再像往常一样对于细小图标进行切图,取而代之的是引用阿里图标库(http://iconfont.cn/):简单 ...
- BZOJ4832: [Lydsy1704月赛]抵制克苏恩(期望DP)
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 913 Solved: 363[Submit][Status][Discuss] Description ...
- Android Studio插件:Android Drawable Importer
Android Drawable Importer 为了在不同分辨率的设备上更好的展示图片的效果,我们往往需要在 res/drawable 中添加不同分辨率的图片.有时我们可能手里只有一份分辨率的图片 ...
- 【Oracle】修改参数的同时添加注释
当修改参数时添加注释,我们会用到如下语句: alter system set parameter=value comment='description'; --修改参数需要有相应权限的用户去执行. 例 ...
- 添加图标:before 和 :after css中用法
#sTitle:after{ position: absolute; top: 2px; font-family: "FontAwesome"; content: "\f ...