我早上调了一个早上,下午才发现把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. Maven—Windows操作系统中安装配置Maven环境

    今天难得的周末,借此难的机会总结一下关于maven的一些操作: 1.在安装maven之前要确认计算机已经安装并配置了JDK. 2.下载maven: maven-3.0.3:http://downloa ...

  2. <vim实用技巧>学习笔记

    第三章插入模式 1.插入模式下的删除  2.返回普通模式                                 3.复制 yt, //复制当前光标到逗号(,)之前的内容 第四章 可视模式 1 ...

  3. 【POJ 1222】 EXTENDED LIGHTS OUT

    [题目链接] http://poj.org/problem?id=1222 [算法] 列出异或方程组,用高斯消元求解即可 [代码] #include <algorithm> #includ ...

  4. Eclipse-Error:笔记-1

    ylbtech-Eclipse-Error:笔记-1 1.返回顶部 1. Whitelabel Error PageThis application has no explicit mapping f ...

  5. [牛客网练习赛 45 F] Magic Slab 解题报告 (最大权闭合子图)

    interlinkage: https://ac.nowcoder.com/acm/contest/847/F description: solution: 最大权闭合子图; 每个单元格看成一个正权点 ...

  6. HTTP请求与请求头

    HTTP 的请求报文分为三个部分 请求行.请求头和请求体,格式如图:一个典型的请求消息头域,如下所示: POST/GET http://download.microtool.de:80/somedat ...

  7. E - Dividing Orange

    Problem description One day Ms Swan bought an orange in a shop. The orange consisted of n·k segments ...

  8. SSRS 报表 如何加参数

    SSRS 报表 如何加参数 连接上以后出现一个问题 就是给报表加上参数以后报表不断刷新,跟上次那个报表刷新是同样的问题.那么下面我们来解决一下. 1. 这是给报表添加默认参数进入页面后就不断的刷新刷新 ...

  9. [hihocoder][Offer收割]编程练习赛49

    相似颜色 #pragma comment(linker, "/STACK:102400000,102400000") #include<stdio.h> #includ ...

  10. codeforces 789 B. Masha and geometric

    链接 B. Masha and geometric depression 题意 给你一个等比数列的首项和公比q,然后给出一个上限l,m个数字,在这个等比数列里,小于l且没有在m个数字里面出现过的可以写 ...