UESTC 889 battle for silver (DFS)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.