当前位置: 首页 > news >正文

[邻接表DFS]最长链和最大环

http://poj.org/problem?id=3895 (最大环)

http://www.codeforces.com/problemset/problem/116/C (最长链)

两道题,都用的邻接表,这个邻接表上次也说过了,figo教的,next为边内存池吧。。下标和dest与dist对应。最长链就是DFS找到最长的,最深的(DFS每一个点,但是我DFS学的不好,不知道有没有更好的方法,找最大环的时候我刚开始还是用DFS每个点的方法,但是GG了,所以我就研究了一下别人的代码,后来发现不用DFS每一个顶点,只要出现了,DFS条件!vis[i]不满足的情况就说明有环出现,所以只要记录一下子就行了。哦也。

 

 

 

#include<iostream>
#include<cstdio>
#include<iomanip>
#include<algorithm>
#include<cstring>
#include< string>
#include<bitset>
#include<queue>
#include<vector>
#include< string>
#include<cmath>
#include<map>
#define rep(i,n,m) for(int i=(n);i<=(m);++i)
#define re1(i,n) rep(i,1,n)
#define re0(i,n) rep(i,0,n)
#define RE(a) ((a)*(a))
#define SIZE(a) (int((a).size()))
#define vi vector<int>
#define vl vector<ll>
#define vs vector<string>
// time count distance 不能用哦,已经定义了。
using  namespace std;
typedef  long  long ll;
template< class T>
void inline maxi(T &a, const T &b){
    a=max(a,b);
}
template< class T>
void inline mini(T &a, const T &b){
    a=min(a,b);
}
template< class T>
void show( const T &a, int n, int m){
    rep(i,n,m){
        cout<<setw( 6)<<a[i];
    }
    cout<<endl;
}
void shownum( int n, int m){
    rep(i,n,m){
        cout<<setw( 6)<<i;
    }
    cout<<endl;
}
template< class T>
void show(T *a[ 10], int n, int m){
    re0(i,n){
        re0(j,m)
            cout<<a[i]<< '   ';
        cout<<endl;
    }
}
template< class T, class P>
void fill(T &a,P v){
    re0(i,SIZE(a)- 1)
        a[i]=v;
}
const  int maxnum= 2000+ 1;
const  int maxint= 2147483647;
vi G,next,dist,dest,path,d;
vector< bool> vis;
void addD( int  from, int to, int dis= 1){
    next.push_back(G[ from]);
    G[ from]=SIZE(next)- 1;
    dest.push_back(to);
    dist.push_back(dis);
}
int timee,n;
void init(){
    G.assign(maxnum,- 1);
    scanf( " %d ",&n);
    re1(u,n){
         int a;
        scanf( " %d ",&a);
         if(a!=- 1)
            addD(u,a);
    }
    vis.assign(maxnum, false);
    path.assign(maxnum,- 1);
    d.assign(maxnum,- 1);
}
void dfs( int s){
    vis[s]= true;
    timee++;
    d[s]=timee;
     for( int i=G[s];i!=- 1;i=next[i]){
         int v=dest[i];
         if(!vis[v]){
            path[v]=s;
            dfs(v);
        }
    }
}
/*
int findp(int from,int to){
    if(to==-1)
        return -1;
    if(from!=path[to])
        return findp(from,path[to]);
}
void print_path(int from,int to){
    if(to==from){
        cout<<from<<endl;
    }else if(path[to]==-1){
        cout<<"GG"<<endl;
    }else{
        print_path(from,path[to]);
        cout<<to<<endl;
    }
}
*/
int mmain(){
    init();
     /*
    shownum(0,n);
    show(G,0,n);
    show(next,0,SIZE(next)-1);
    show(dest,0,SIZE(next)-1);
    
*/
     int ans=- 1;
    re1(u,n){
        timee= 0;
        fill(vis, false);
         if(!vis[u])
            dfs(u);
        maxi(ans,timee);
    }
    cout<<ans<<endl;
     /*
    if(findp(5,6)!=-1){
        print_path(5,6);
    }else
        cout<<"GG"<<endl;
    
*/
}
#define codeforces CODEFORCES
// #define INPUT CODEFORCES_FILE
// #define MANY_TEST CODEFORCES_MANY_TEST
int main(){
#ifdef codeforces
    #ifdef INPUT
    freopen( " input.txt ", " r ",stdin);
    freopen( " output.txt ", " w ",stdout);
     #endif
    #ifdef MANY_TEST
    re1(wwwwwwwwwwwwwwwwwwwww, 3)
        mmain();
     return  0;
     #endif
#endif
    mmain();

}

 

#include<iostream>

#include<cstdio>
#include<iomanip>
#include<algorithm>
#include<cstring>
#include< string>
#include<bitset>
#include<queue>
#include<vector>
#include< string>
#include<cmath>
#include<map>
#define rep(i,n,m) for(int i=(n);i<=(m);++i)
#define re1(i,n) rep(i,1,n)
#define re0(i,n) rep(i,0,n)
#define re(a) ((a)*(a))
#define SIZE(a) (int((a).size()))
#define v(a) vector<a>
#define vi v(int)
#define vl v(ll)
#define vb v(bool)
// count distance 不能用哦,已经定义了。
using  namespace std;
typedef  long  long ll;
template< class T>
void inline maxi(T &a, const T &b){
    a=max(a,b);
}
template< class T>
void inline mini(T &a, const T &b){
    a=min(a,b);
}
template< class t, class p>
void fill(t &a,p v){
     int n=SIZE(a)- 1;
    re0(i,n)
        a[i]=v;
}
void shownum( int n, int m){
    rep(i,n,m){
        cout<<setw( 6)<<i;
    }
    cout<<endl;
}
template< class t>
void show(t &a, int n, int m){
    rep(i,n,m){
        cout<<setw( 6)<<a[i];
    }
    cout<<endl;
}
template< class t>
void show(t *a[ 10], int n, int m){
    re0(i,n){
        re0(j,m)
            cout<<a[i]<< '   ';
        cout<<endl;
    }
}
const  int maxnum= 4444+ 1;
const  int maxint= 2147483647;
int n,m,ans;
vi G,next,dest,d,timee;
vb vis;
void addd( int  from, int to){
    next.push_back(G[ from]);
    G[ from]=SIZE(next)- 1;
    dest.push_back(to);
}
void init(){
    scanf( " %d%d ",&n,&m);
    G.assign(n+ 1,- 1);
    vis.assign(n+ 1, false);
    d.assign(n+ 1, 1);
    timee.assign(n+ 1, 0);
    re1(u,m){
         int a,b;
        scanf( " %d%d ",&a,&b);
        addd(a,b);
        addd(b,a);
    }
}
void findl( int s, int time){
    vis[s]= true;
    timee[s]=time;
     for( int i=G[s];i!=- 1;i=next[i]){
         int v=dest[i];
         if(!vis[v]){
            findl(v,time+ 1);
        } else{
            maxi(ans,timee[v]-timee[s]);
        }
    }
}
int mmain(){
     int tcase;
    scanf( " %d ",&tcase);
    re1(uuuuuuuuuuuuuuu,tcase){
        init();
        ans=- 1;
         /*
        shownum(0,size(next)-1);
        show(g,0,n);
        show(next,0,size(next)-1);
        show(dest,0,size(next)-1);
        
*/
        re1(u,n){
             if(!vis[u])
                findl(u, 1);
        }
        printf( " %d\n ",ans+ 1);
    }
}
// #define codeforces codeforces
// #define input codeforces_file
// #define many_test codeforces_many_test
int main(){
#ifdef codeforces
    #ifdef input
    freopen( " input.txt ", " r ",stdin);
    freopen( " output.txt ", " w ",stdout);
     #endif
    #ifdef many_test
    re1(wwwwwwwwwwwwwwwwwwwww, 3)
        mmain();
     return  0;
     #endif
#endif
    mmain();

转载于:https://www.cnblogs.com/gggin/archive/2012/12/27/2836577.html

相关文章:

  • 使用for循环对 golang 中结构体数组取值进行修改时,需要注意的问题
  • C#基础 [07] 方法[上]
  • Window7下SourceInsight加载需要字体方法
  • 阿里云高性能AI服务 -- 基于Docker和EGS一键创建高性能Tensorflow分布式训练
  • CBitMap的用法 from http://www.cnblogs.com/toconnection/archive/2012/08/04/mfc.html
  • ES6指北【2】—— 箭头函数
  • MyBatis + winform 配置
  • VS2015 中统计整个项目的代码行数
  • EI收录中国大陆期刊名录(2012年)
  • 2018/02/28
  • 路由反射器(Route Reflector)简介
  • 最优化原理,凸优化
  • Windows7+vs2008进行wince开发的环境配置
  • 太难、太贵、太耗时......这些都是你对CRM工具的误解!
  • 百度搜索结果的URL参数关键词(wd|word|kw|keyword)
  • [译]如何构建服务器端web组件,为何要构建?
  • 《网管员必读——网络组建》(第2版)电子课件下载
  • 【EOS】Cleos基础
  • flask接收请求并推入栈
  • mysql 5.6 原生Online DDL解析
  • node-sass 安装卡在 node scripts/install.js 解决办法
  • tensorflow学习笔记3——MNIST应用篇
  • vue-cli3搭建项目
  • Zepto.js源码学习之二
  • 订阅Forge Viewer所有的事件
  • 动态魔术使用DBMS_SQL
  • 高度不固定时垂直居中
  • 解决iview多表头动态更改列元素发生的错误
  • 利用阿里云 OSS 搭建私有 Docker 仓库
  • 强力优化Rancher k8s中国区的使用体验
  • 什么软件可以剪辑音乐?
  • 想晋级高级工程师只知道表面是不够的!Git内部原理介绍
  • 学习HTTP相关知识笔记
  • 一起参Ember.js讨论、问答社区。
  • 资深实践篇 | 基于Kubernetes 1.61的Kubernetes Scheduler 调度详解 ...
  • ​​​​​​​GitLab 之 GitLab-Runner 安装,配置与问题汇总
  • #pragma data_seg 共享数据区(转)
  • (+3)1.3敏捷宣言与敏捷过程的特点
  • (LeetCode 49)Anagrams
  • (ros//EnvironmentVariables)ros环境变量
  • (ZT) 理解系统底层的概念是多么重要(by趋势科技邹飞)
  • (二)学习JVM —— 垃圾回收机制
  • (附源码)spring boot基于Java的电影院售票与管理系统毕业设计 011449
  • (附源码)spring boot建达集团公司平台 毕业设计 141538
  • (七)微服务分布式云架构spring cloud - common-service 项目构建过程
  • .NET 4.0中的泛型协变和反变
  • .net MySql
  • .net对接阿里云CSB服务
  • .NET平台开源项目速览(15)文档数据库RavenDB-介绍与初体验
  • .net中生成excel后调整宽度
  • /ThinkPHP/Library/Think/Storage/Driver/File.class.php  LINE: 48
  • /usr/lib/mysql/plugin权限_给数据库增加密码策略遇到的权限问题
  • @JsonSerialize注解的使用
  • [ 英语 ] 马斯克抱水槽“入主”推特总部中那句 Let that sink in 到底是什么梗?
  • [20160807][系统设计的三次迭代]