zoukankan      html  css  js  c++  java
  • CSY版最大团,速度快一倍

    #include <bits/stdc++.h>
    
    using namespace std;
    
    #define REP(i, n)                 for(int i(0); i <  (n); ++i)
    #define rep(i, a, b)              for(int i(a); i <= (b); ++i)
    #define dec(i, a, b)              for(int i(a); i >= (b); --i)
    #define for_edge(i, x)            for(int i = H[x]; i; i = X[i])
    
    #define LL      long long
    #define ULL     unsigned long long
    #define MP      make_pair
    #define PB      push_back
    #define FI      first
    #define SE      second
    #define INF     1 << 30
    
    const int N     =    100000      +       10;
    const int M     =    10000       +       10;
    const int Q     =    1000        +       10;
    const int A     =    30          +       1;
    
    typedef long long ll;
    
    int T;
    int n;
    ll a[Q];
    ll c[Q][Q];
    ll mx[Q],s[Q],ans;
    ll f[Q][Q];
    int i,j,k;
    
    int dfs(int cur,int tot)
    {
        if(!cur){
            if(tot>ans) return ans=tot,1;
            return 0;
        }
        for(int i=0,j,u,nxt;i<cur;i++)
        {
            if(cur-i+tot<=ans) return 0;
            u=f[tot][i],nxt=0;
            if(mx[u]+tot<=ans) return 0;
            for(int j=i+1;j<cur;j++) 
            if(c[u][f[tot][j]]) f[tot+1][nxt++]=f[tot][j];
            if(dfs(nxt,tot+1)) return 1; 
        }
        return 0;
    }
    int main(){
        
    
        for (scanf("%d", &T); T--; ){
            scanf("%d", &n);
            memset(c, 0, sizeof c);
            rep(i, 1, n) scanf("%lld", a + i);
            rep(i, 1, n - 1) rep(j, i + 1, n){
                if (a[i] % a[j] != 0 && a[j] % a[i] != 0){
                    c[i-1][j-1] = c[j-1][i-1] = 1;
                }
            }
    
        /*    rep(i, 1, n){
                rep(j, 1, n) printf("%d ", c[i][j]);
                putchar(10);
            }*/
            memset(mx,0,sizeof(mx));
            memset(s,0,sizeof(s));
            ans=0;
            for(i=n-1;~i;dfs(k,1),mx[i--]=ans) 
            for(k=0,j=i+1;j<n;j++) 
            if(c[i][j]) f[1][k++]=j;
            printf("%lld
    ",ans);
        }
        return 0;
    
    }
  • 相关阅读:
    引入包时“”与<>的区别
    Scanners
    一个SQLite数据库的LIKE查询和IN集合查询的代码实例
    @synchronized线程同步互斥锁
    makeKeyAndVisible
    NSString 数字判断
    UILabel UITextField UITextView
    (转载)JAVA SE6.0 中使用SplashScreen
    转帖(http://hi.baidu.com/johnehuang/blog/item/e1e96782a4b04e97f703a6dc.html)
    ce
  • 原文地址:https://www.cnblogs.com/hnqw1214/p/6505133.html
Copyright © 2011-2022 走看看