UVaLive 6623 Battle for Silver (最大值,暴力)
2016-08-21 14:12
387 查看
题意:给定一个图,让你找一个最大的子图,在这个子图中任何两点都有边相连,并且边不交叉,求这样子图中权值最大的是多少。
析:首先要知道的是,要想不交叉,那么最大的子图就是四个点,否则一定交叉,然后就暴力就好,数据水,不会TLE的,才100多ms
代码如下:
析:首先要知道的是,要想不交叉,那么最大的子图就是四个点,否则一定交叉,然后就暴力就好,数据水,不会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\n", ans); } return 0; }
相关文章推荐
- UVAlive 6623 Battle for Silver(暴力+思路)
- UESTC889&&uvalive6623 Battle for Silver(dfs)
- POJ1964/UVALive 3029 -City Game -求最大子矩形-(暴力枚举/dp)
- UVALive 3890 Most Distant Point from the Sea(凸包最大内接园)
- UVALIVE 4819 最大流
- uvalive 3938 - "Ray, Pass me the dishes!" +动态最大连续和+线段树
- UVa753/POJ1087_A Plug for UNIX(网络流最大流)(小白书图论专题)
- Vampire Numbers - UVALive 5779 暴力打表
- UVALive 4043 Ants(二分图匹配,最大匹配,4级)
- UVALive 6465 Islands in the Data Stream 暴力模拟
- UVa live3415Guardian of Decency(二分最大匹配之最大独立点集)
- UVALive 6163(暴力枚举)
- UVALIVE 4819 最大流
- Uvalive 4865 Data Recovery 最大流
- Uvalive 4865 Data Recovery 最大流
- UVALive 6468 Pisano Periods 坑爹暴力
- UVALive 3415 Guardian of Decency(二分图的最大独立集)
- UVALive 6319 暴力
- UVALive 3415 Guardian of Decency(二分图最大独立集)
- UVALive 6089 Nine (暴力模拟)