Question:
For a graph, each vertex has a certain authority. Each vertex has at most one edge, and each vertex is connected to at least one vertex through the edge.
We need to find such a group so that all vertices in the group are connected by edges without overlap and the vertices have the largest vertices.
Algorithm:
Because the two connected edges cannot overlap, we can see that there are a maximum of four vertices. So violent search ~
The DFS identifies the elements of the four locations and updates ans while judging whether the points are connected to the previous ones.
At the beginning, I always write at three points and four points. I forgot to consider one point and two points... Wa 10 times ..
If you write a sample yourself, the result is not considered as one or two points ..
/*
6 7
1500
1000
100
2000
500
300
1 2
1 3
1 4
3 5
4 5
4 6
5 6
Ans = 3500
*/
#include<cstdio>#include<iostream>#include<cstring>#include<vector>#define maxn 500using namespace std;int a[maxn],mp[maxn][maxn],v,ans;int op[5];bool vis[maxn];int check(int pos,int x){ for(int i=0;i<pos;i++) { if(!mp[op[i]][x]) return 0; } return 1;}void dfs(int pos,int x,int sum){ ans = max(ans,sum); if(pos==4) return; for(int i=x+1;i<=v;i++) { if(vis[i] || !check(pos,i)) continue; op[pos] = i; vis[i] = true; dfs(pos+1,i,sum+a[i]); vis[i] = false; }}int main(){ int e,ta,tb; while(scanf("%d%d",&v,&e)!=EOF) { memset(mp,0,sizeof(mp)); for(int i=1;i<=v;i++) scanf("%d",&a[i]); for(int i=1;i<=e;i++) { scanf("%d%d",&ta,&tb); mp[ta][tb] = mp[tb][ta] = 1; } ans = 0; dfs(0,0,0); printf("%d\n",ans); } return 0;}
UESTC 889 battle for silver (DFS)