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 ...
随机推荐
- java.util.ComparableTimSort中的sort()方法简单分析
TimSort算法是一种起源于归并排序和插入排序的混合排序算法,设计初衷是为了在真实世界中的各种数据中能够有较好的性能. 该算法最初是由Tim Peters于2002年在Python语言中提出的. T ...
- luogu1024 一元三次方程求解
题目大意 已知一元三次方程\(ax^3+bx^2+cx+d=0\): 有且只有3个根 对\(\forall x, x\in[-100,100]\) 对\(\forall x_1,x_2,|x_1-x_ ...
- luogu2014 选课 背包类树形DP
题目大意:有N门功课,每门课有个学分,每门课有一门或没有直接先修课(若课程a是课程b的先修课即只有学完了课程a,才能学习课程b).一个学生要从这些课程里选择M门课程学习,问他能获得的最大学分是多少? ...
- element-UI中table表格的row-click事件怎么获取一行数据的id
<el-table :data="tableData" style="width: 100%" @row-click="openDetails( ...
- HP Z240组建磁盘阵列RAID1
- C++字符串与指针 所有的内容也就这么多了。
1.定义一个字符串数组并初始化,然后输出其中的字符串. #include <iostream> using namespace std;int main(){ char str[]=&qu ...
- 使用ssh和putty操控远程的linux server
windows下没有openssh,今天这里使用openssh-server作为server,windows下使用putty作为client, putty主要流程分以下几步: step 1: 下载pu ...
- cas-client-core单点登录排除不需要拦截的URL
同事提了一个要求,要求对外提供的接口不需要经过单点登录验证,我刚开始想,这简单,提供不需要拦截的url数组,在AuthenticationFilter里面对url进行检查,在此数组内,就不需要拦截. ...
- 禁止button标签提交form表单,变成普通按钮
button有个type属性,属性值可为button.submit.reset button=普通按钮,直接点击不会提交表单submit=提交按钮,点击后会提交表单reset=表单复位 当button ...
- .NET中使用反射访问属性方法
.net所编写的程序集包含两个重要部分:IL(中间语言代码) 和metadata(元数据).我们编写的代码中不是有很多很多的类吗,类有很多很多的成员,在编译代码的时候,元数据表就根据代码把类的所有信息 ...