UVaLive 6623 Battle for Silver (最大值,暴力)

题意:给定一个图,让你找一个最大的子图,在这个子图中任何两点都有边相连,并且边不交叉,求这样子图中权值最大的是多少。

析:首先要知道的是,要想不交叉,那么最大的子图就是四个点,否则一定交叉,然后就暴力就好,数据水,不会TLE的,才100多ms

代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    using namespace std;

    typedef long long LL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 0x3f3f3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 450 + 5;
    const int mod = 1e9 + 7;
    const char *mark = "+-*";
    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, 1, 0, -1};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline int Min(int a, int b){ return a < b ? a : b; }
    inline int Max(int a, int b){ return a > b ? a : b; }
    inline LL Min(LL a, LL b){ return a < b ? a : b; }
    inline LL Max(LL a, LL b){ return a > b ? a : b; }
    inline bool is_in(int r, int c){
        return r >= 0 && r < n && c >= 0 && c < m;
    }
    vector<int> G[maxn];

    int a[maxn];
    int g[maxn][maxn];
    int ans;
    int pre[10];

    void dfs(int u, int d, int val){
        ans = Max(ans, val);
        if(d == 4)  return ;

        for(int i = 0; i < G[u].size(); ++i){
            int v = G[u][i];
            bool ok = true;
            for(int j = 0; j < d; ++j){
                if(!g[v][pre[j]]){ ok = false;  break; }
            }
            for(int j = 0; j < d; ++j)
                if(pre[j] == v){ ok = false;  break; }
            if(!ok)  continue;
            pre[d] = v;
            dfs(v, d+1, val+a[v]);
        }
    }

    int main(){
        while(scanf("%d %d", &n, &m) == 2){
            for(int i = 1; i <= n; ++i){
                scanf("%d", &a[i]);
                G[i].clear();
            }
            memset(g, 0, sizeof(g));
            for(int i = 0; i < m; ++i){
                int u, v;
                scanf("%d %d", &u, &v);
                g[u][v] = g[v][u] = 1;
                G[u].push_back(v);
                G[v].push_back(u);
            }
            ans = 0;
            for(int i = 1; i <= n; ++i){
                pre[0] = i;
                dfs(i, 1, a[i]);
            }
            printf("%d
", ans);
        }
        return 0;
    }

  

原文地址:https://www.cnblogs.com/dwtfukgv/p/5792664.html