• BZOJ 2208: [Jsoi2010]连通数( DFS )


    n只有2000,直接DFS就可以过了...

    --------------------------------------------------------------------------

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    #include<cctype>
     
    #define rep( i, n ) for( int i = 0; i < n; ++i )
    #define clr( x, c ) memset( x, c, sizeof( x ) )
     
    using namespace std;
     
    const int maxn = 2000 + 5;
     
    struct edge {
    int to;
    edge* next;
    };
     
    edge* pt;
    edge* head[ maxn ];
    edge EDGE[ maxn * maxn ];
     
    void init() {
    pt = EDGE;
    clr( head, 0 );
    }
     
    inline void add_edge( int u, int v ) {
    pt -> to = v;
    pt -> next = head[ u ];
    head[ u ] = pt++;
    }
     
    bool vis[ maxn ];
    int ans = 0;
     
    void dfs( int x ) {
    ans++;
    vis[ x ] = true;
    for( edge* e = head[ x ]; e; e = e -> next ) if( ! vis[ e -> to ] )
       dfs( e -> to );
    }
     
    int main() {
    init();
    int n;
    cin >> n;
    rep( i, n )
       rep( j, n ) {
        char c = getchar();
        while( ! isdigit( c ) ) c = getchar();
        if( c == '1' ) add_edge( i, j );
       }
    rep( i, n ) {
    clr( vis, 0 );
    dfs( i );
    }
    cout << ans << " ";
    return 0;
    }

      

    --------------------------------------------------------------------------

    2208: [Jsoi2010]连通数

    Time Limit: 20 Sec  Memory Limit: 512 MB
    Submit: 1499  Solved: 611
    [Submit][Status][Discuss]

    Description

    Input

    输入数据第一行是图顶点的数量,一个正整数N。 接下来N行,每行N个字符。第i行第j列的1表示顶点i到j有边,0则表示无边。

    Output

    输出一行一个整数,表示该图的连通数。

    Sample Input

    3
    010
    001
    100

    Sample Output

    9

    HINT

    对于100%的数据,N不超过2000。

    Source

  • 相关阅读:
    C#的第一个应用
    中位数和顺序统计量(第9章)
    散列表的学习和探讨(算法导论第11章)
    数据挖掘和机器学习的基本概念
    Weka数据挖掘——选择属性
    Weka数据挖掘——关联
    Weka数据挖掘——聚类
    Weka数据挖掘——分类
    WEKA-过滤器算法学习
    WEKA——数据挖掘与机器学习工具
  • 原文地址:https://www.cnblogs.com/JSZX11556/p/4525457.html
Copyright © 2020-2023  润新知