我早上调了一个早上,下午才发现把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的更多相关文章

  1. POJ 2132 暴搜OR Floyd

    题意: 给你一个邻接矩阵(n<=25)问所有1到2路径的gcd的lcm是多少. 一些经验(WA/TLE的经验): 1. 无脑暴搜 是会TLE的--. 2. 关于精度 dyf神牛说了:long l ...

  2. poj 3311 Hie with the Pie

    floyd,旅游问题每个点都要到,可重复,最后回来,dp http://poj.org/problem?id=3311 Hie with the Pie Time Limit: 2000MS   Me ...

  3. POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798 ...

  4. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

  5. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

  6. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

  7. POJ 3254. Corn Fields 状态压缩DP (入门级)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9806   Accepted: 5185 Descr ...

  8. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  9. POJ 2255. Tree Recovery

    Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11939   Accepted: 7493 De ...

随机推荐

  1. 学习 java netty (一) -- java nio

    前言:近期在研究java netty这个网络框架,第一篇先介绍java的nio. java nio在jdk1.4引入,事实上也算比較早的了.主要引入非堵塞io和io多路复用.内部基于reactor模式 ...

  2. 使用C语言扩展Python3

    使用C语言扩展Python3.在Python3中正确调用C函数. 1. 文件demo.c #include <Python.h> // c function static PyObject ...

  3. 用Webpack构建Vue项目

    开始之前,需要安装node环境.(安装过程在此就不啰嗦了)   1.创建基本结构 首先我们要创建一个空文件夹(我这里叫todos,你可以随便命名)作为项目的根目录. 创建一个没有任何依赖关系的pack ...

  4. django URL多层路由

    一.多层路由 如果django里的app数量越来越多,那项目里的urls文件配置起来将会很麻烦,而且也不利于后续项目的改动和整理 所以看了杨老师的视频https://www.bilibili.com/ ...

  5. 关于iscroll.js插件的使用

    iscroll 作用: 可以让区域滚动效果好看一些 使用: 1. html结构 外面必须包一层盒子,切内部的元素要尽量简单,不然会影响滚动效果 <div id="wrapper&quo ...

  6. python3 常用模块详解

    这里是python3的一些常用模块的用法详解,大家可以在这里找到它们. Python3 循环语句 python中模块sys与os的一些常用方法 Python3字符串 详解 Python3之时间模块详述 ...

  7. **PCD数据获取:Kinect+OpenNI+PCL对接(代码)

    前言: PCL使用点云作为数据格式,Kinect可以直接作为三维图像的数据源产生三维数据,其中的桥梁是OpenNI和PrimeSense.为了方便地使用Kinect的数据,还是把OpenNI获取的基础 ...

  8. (转)基于MVC4+EasyUI的Web开发框架经验总结(3)- 使用Json实体类构建菜单数据

    http://www.cnblogs.com/wuhuacong/p/3669708.html 最近花了不少时间在重构和进一步提炼我的Web开发框架上,力求在用户体验和界面设计方面,和Winform开 ...

  9. Jquery中拿到相同的对应的所有的标签

    在Jquery中相同的ID号不能用$()获得,即使是$().each()也不能获得所有的ID相同的元素,只能获得第一个匹配的元素. 比如: 以上4个div,如果用$("#jevoly&quo ...

  10. Django01 web http 基础

    一.内容回顾 1.python基础 2.网络编程 3.并发编程 4.前端 5.数据库(MySQL) 二.今日概要 1.了解Web应用程序的本质 2.Django简介及安装使用 三.今日详细 1.最简单 ...